Script to call an .exe with parameters

Hi All,

I’m new to both Poweshell and Imgaemagick so please bare with me.

My task is to monitor a folder for new file creation of .jpeg and .jpg.

After file is created copy file to backup folder and then call Imagemagick to resize original file.

How do I call imagemagick and send parameters?

From the command line Imagemagick works as follows

magick convert rose.jpg -resize 50% new_rose.jpg

$BackupPath = "C:\ImageTest\Backup\"
$im_exe = "C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\magick.exe"

If($FileWatcher){$FileWatcher.Dispose();$FileWatcher = $null}

$FileWatcher = New-Object System.IO.FileSystemWatcher -Property @{

NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
Path = 'C:\ImageTest\Source\'

}

Register-ObjectEvent -InputObject $FileWatcher -EventName Created -Action {

#get extension type
$extn = [IO.Path]::GetExtension($event.SourceEventArgs.FullPath)

#look for jpeg and .jpg only
If ($extn.ToLower() -eq '.jpg' -Or $extn.ToLower() -eq '.jpeg'){

#get file name
#Write-Host ($event.SourceEventArgs.Name)

#copy original file to folder
Copy-Item -Path $event.SourceEventArgs.FullPath -Destination $BackupPath

#call imgagemagick pass file to process

}

}

I would simplify this a few different ways and also move the definition of the backup path and image magick executable to ensure it’s defined in the scriptblock. I’ve tested this and it works perfectly.

If($FileWatcher){$FileWatcher.Dispose();$FileWatcher = $null}

$FileWatcher = New-Object System.IO.FileSystemWatcher -Property @{
 
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    Path = 'C:\ImageTest\Source\'

}

Powershell is case sensitive by default so you can drop ToLower. You can also move the file during the conversion process and omit the copy portion. 
Register-ObjectEvent -InputObject $FileWatcher -EventName Created -Action {

    $BackupPath = 'C:\ImageTest\Backup'
    $im_exe = "C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\magick.exe"
    #get extension type
    $extn = [IO.Path]::GetExtension($event.SourceEventArgs.FullPath)
 
    #look for jpeg and .jpg only
    If ($extn -match 'jpg|jpeg'){
 
        #get file name
        Write-Host $event.SourceEventArgs.Name

        #call imgagemagick pass file to process and move in same step
        &$im_exe $event.SourceEventArgs.FullPath -resize 50% "$BackupPath\new_$($event.SourceEventArgs.Name)"
 
    }
 
}

Hi Mcaston15,

Several ways to go about it it seems. You could use the Start-Process cmdlet:

Start-Process -FilePath “C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\magick.exe” -ArgumentList “convert rose.jpg -resize 50% new_rose.jpg”

Or why not using the call operator (&):

& “C:\Program Files\ImageMagick-7.0.10-Q16-HDRI\magick.exe” convert rose.jpg -resize 50% new_rose.jpg

Check this link for more details on how to run executables in Powershell:

https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

Hi Doug,

Thanks for your help. That worked.