Author Posts

February 6, 2018 at 11:41 am

I am building a simple module to resize JPG images into thumbnails suitable for Active Directory / GAL. It's based around ImageMagick, basically just a wrapper function for convert.exe file and a few parameters.

My question is, within the function code, how would I specify the path to convert.exe. It lives in the module folder, but this isn't where the function will be invoked, so a relative path isn't applicable. At the moment it is hardcoded as follows:

function Resize-GALImage {

#blah blah, parameters etc.

    foreach ($Item in $Items)
    {
        Write-Verbose "Converting $($Item.FullName)"
        # Todo: fix hard-coded path to convert.exe
        C:\Scripts\GALPhotos\bin\convert.exe -quality $Quality -depth 8 -thumbnail "$($Zoom)x$($Zoom)^" -gravity North -crop $Crop $Item.FullName "$($FullOutputFolderPath)\$($Item.BaseName).jpg"
    }
}

What should appear instead of "C:\Scripts\GALPhotos\bin\" ? Is there some good documentation on use of binary files within modules?

Thanks

February 6, 2018 at 4:27 pm

You could try $PSScriptRoot or $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition. It might not be the complete ready to use path but if you play around with it a little you will be able to transform it to your needs. 😉

August 7, 2018 at 9:37 pm

Here is a Powershell script I use to process files recursively with ImageMagick 7.  It displays a detailed output of files processed so you can monitor how the script is working.

# Configuration
# Enter the full path of the folder that contains the images
$Rootfolder = "C:\temp\rktest1"
$Recursive=$true
# Change these as needed
$fileExtensions = "*.png"
$fileNameSuffix = "_resized" # the text to be appended to the output filename to indicate that it has been modified
$files = $null;
$fileCount = 0
# Check if the root folder is a valid folder. If not, try again.
if ((Test-Path $RootFolder -PathType 'Container') -eq $false) {
Write-Host "'$RootFolder' doesn't seem to be a valid folder. Please try again" -ForegroundColor Red
}
# Get all image files in the folder
if ($Recursive) {
$files = gci $RootFolder -Filter $fileExtensions -File -Recurse
}
# If there are no image files found, write out a message and quit
if ($files.Count -lt 1) {
Write-Host "No image files with extension '$fileExtensions' were found in the folder '$RootFolder'" -ForegroundColor Red
}
# Loop through each of the files and process it
foreach ($image in $files) {
$newFilename = $image.DirectoryName + "\" + $image.BaseName + $fileNameSuffix + $image.Extension
# Note - The $newFilename line must contain a "\" (backslash) for the output files to be created in source subfolders
$imageFullname = $image.FullName
write-host "Processing image: $imageFullname" -ForegroundColor Green
#This line contains the ImageMagick command line
& magick $image.FullName -resize 25% $newFilename
$fileCount++
}
Write-Host "$fileCount images processed" -ForegroundColor Yellow

August 8, 2018 at 7:56 am

Ditto wo what Olaf says here, or in your code you can modify your environment path variable temporarily to run the exe. That environment change only there during the script run. See the PS help files on that topic.

Get-Help environment