Caption One Caption Two Caption Three Caption Four
Powershell: Converting a String to an Integer
In creating a Powershell script to create a Microsoft Word document from CSV data I came across a little problem. In this snippet of code (the $patientAge and $patientName variables are populated with data from the CSV file) I want to highlight a patient’s name in yellow if they’re a minor:
If ($patientAge -lt 18)
{
$objSelection.font.Shading.BackgroundPatternColor = 65535
}
$objSelection.TypeText($patientName)
And it works fine if the patient age is two digits or less. But, if a patient age is 100 or higher – three digits, in other words – it would trigger as if they were a minor.
The fix was to convert the variable I’m using for the patient age into an integer and run the logic against it instead:
$patientAgeInt=[int]$patientAge
If ($patientAgeInt -lt 18)
{
$objSelection.font.Shading.BackgroundPatternColor = 65535
}
$objSelection.TypeText($patientName)
Boom, problem solved.
Powershell: Convert Word documents to PDF
# This script uses Microsoft Word to convert all .doc and .docx files
# within a folder to PDF files.
#
# Note 1: Word runs invisibly in the background while the script is running.
# Note 2: This script will not overwrite existing PDF files.
# Set the folder where the .doc and .docx files are
$folder = 'c:\path\to\folder\'
# Create an instance of Word
$word = New-Object -ComObject Word.Application
# Find all .doc and .docx files in $folder and
# loop through them, saving them as PDF files.
Get-ChildItem -Path $folder -Filter *.doc? | ForEach-Object {
# Open the doc? file
$file = $word.Documents.Open($_.FullName)
# Change the file extension to .pdf
$pdf = "$($_.DirectoryName)\$($_.BaseName).pdf"
# Save and close the file
# If a PDF file with this name already exists it will NOT be overwritten.
$file.SaveAs([ref] $pdf, [ref] 17)
$file.Close()
}
# Exit Word
$word.Quit()