0

I have researched this question quite a bit and have found others with similar issues, but no fix that works for us.

We have built a power shell script to monitor a folder for new images, check their color profile with exiftool and convert when needed with image magick. All of this functionality works well enough when the script is run in ISE, but will not work if the script is executed from a .exe or ran manually.

I have read that FileSystemWatcher requires the script be ran in STA which I have attempted to do a few times by including -STA in the target for the .exe shortcut. Doing so proves unsuccessful.

#----------------------------------------------------------------------------

Unregister-Event -SourceIdentifier FileCreated

Clear

#----------------------------------------------------------------------------

$description = Get-WmiObject -Class Win32_OperatingSystem |Select Description
$description = $description -Replace "@{Description=",""
$description = $description -Replace "}",""

$assetsFolder = "E:\" + $description + "\Capture One Session\Output"
$conversionStage = "C:\Profile_Pro\conversionStage"

#----------------------------------------------------------------------------

New-Item -ItemType directory -Force -Path "$assetsFolder" | Out-Null
New-Item -ItemType directory -Force -Path "$assetsFolder" | Out-Null

#----------------------------------------------------------------------------

$filter = "*.*"

$srgb = "sRGB IEC61966-2.1"

$isTif = "tif"

#----------------------------------------------------------------------------

$monitor = New-Object IO.FileSystemWatcher $assetsFolder, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

#----------------------------------------------------------------------------

$onCreated = Register-ObjectEvent $monitor Created -SourceIdentifier FileCreated -Action {

#----------------------------------------------------------------------------

    Add-Type -AssemblyName System.Windows.Forms

#----------------------------------------------------------------------------

    $path = $event.SourceEventArgs.FullPath
    $name = $event.SourceEventArgs.Name

    Write-Host $path
    Write-Host $name

    If ($name -Match $isTif)
        {
        $BOX = [System.Windows.Forms.MessageBox]::Show('TIFF FILE DETECTED. PLEASE CHECK RECIPE.', 'Warning', 'ok', 'Warning')

        $path = $path -Replace ".tif.tmp",".tif"

        Start-Sleep -s 2

        Remove-Item "$path" -Force
        }
    Else
        {
        $colorSpace = C:\Profile_Pro\tools\exiftool.exe -T -ProfileDescription $path
        $profileConvert = 'C:\Profile_Pro\tools\sRGB_Color_Space_Profile.icm'

        If ($colorSpace -Match $srgb) 
            {
            }
        Else
            {
            Start-Sleep -s 3

            $name = $name -Replace ".jpg.tmp",".jpg"
            $path = $path -Replace ".jpg.tmp",".jpg"

            Move-Item -Path $path -Destination $conversionStage -Force

            convert $conversionStage\$name -profile $profileConvert $path

            Start-Sleep -s 1

            Remove-Item "$conversionStage\$name" -Force

#----------------------------------------------------------------------------

            $BOX = [System.Windows.Forms.MessageBox]::Show('COLOR PROFILE ERROR. PLEASE CHECK RECIPE.', 'Warning', 'ok', 'Warning')

#----------------------------------------------------------------------------

            $TO = $env:USERNAME + "@biz.com"

            $Outlook = New-Object -ComObject Outlook.Application
            $Mail = $Outlook.CreateItem(0)
            $Mail.To = $TO
            $Mail.Subject = "COLOR PROFILE DEFECT: " + $name
            $Mail.importance = 2
            $Mail.Body = "A COLOR PROFILE DEFECT HAS BEEN DETECTED WITH FILE:`r`n`r`n$name`r`n`r`nTHIS FILE IS FIXED.`r`n`r`nPLEASE CHECK YOUR PROCESS RECIPE."
            $Mail.Send()
            }
        }
    }

#----------------------------------------------------------------------------

#Unregister-Event -SourceIdentifier FileCreated

#----------------------------------------------------------------------------

Any ideas to keep this script running as a .exe or outside of ISE would be greatly appreciated.

Garrett
  • 617
  • 12
  • 30
  • 1
    _but will not work if the script is executed from a .exe or ran manually._ What does that mean. You have a large chuck of code here. Debugging is not something we can easily do in _your_ environment. You also have interactive elements here, like forms and outlook, that would not work if not being run in an interactive session i.e. task. – Matt Apr 12 '17 at 19:16
  • Are there errors when you try to run the script outside of ISE? Do you have the right execution policy set? Also, after PowerShell 2.0 STA is default. – Christopher Apr 12 '17 at 19:20
  • @Matt I mean that if I create a shortcut to target the PS1 script or right click the PS1 and run with power shell I get nothing. Sorry for being vague, every individual part of the script works correctly (including forms and outlook). I am not following why the script would work perfectly well when ran in ISE but not in the other methods i have mentioned. I have found that others using filesystemwatcher have experienced this as well. – Garrett Apr 13 '17 at 20:23
  • @Christopher I do not get errors. Could you provide any more detail on execution policy? I am not familiar with this. In the meantime i will research that as a possible issue. Thanks – Garrett Apr 13 '17 at 20:28
  • Are you running in STA mode? Check with `$host.Runspace.ApartmentState`. More info at these links [psh studio](https://www.sapien.com/blog/2015/03/09/powershell-studio-knowing-when-to-use-sta-mode/) and [technet](https://social.technet.microsoft.com/Forums/en-US/d799fd4f-6126-4c68-88ae-fcfea32a0602/my-script-will-only-run-in-ise-not-from-powershellexe?forum=winserverpowershell). May or may not be your issue but it would be a quick win if you just need to change the run mode. – sadtank Apr 13 '17 at 22:56
  • @primohacker Yes it should be. As Christopher stated Power Shell 2.0 and up are STA by default. Will look further into that thanks for the suggestion. – Garrett Apr 14 '17 at 15:08
  • @Christopher Checking execution policy yielded these results: Scope ExecutionPolicy----- --------------- MachinePolicy Unrestricted, UserPolicy Unrestricted, Process Undefined, CurrentUser Undefined, LocalMachine Undefined, Do you think the undefined portions may be a problem? – Garrett Apr 14 '17 at 16:03
  • @Garrett No, I don't think the execution policy is the problem. Does the powershell exe run? Or does the script execute and error and close itself? Creating error handling with verbose logging options is probably recommended for a heavy-lifting production script such as this. Do you know what the $LastExitCode is when running from a script? Is there any type of error checking anywhere in your full script? As previously mentioned, people here cannot recreate or debug your code for you. Does the user running the script have permissions set for the other programs? – Christopher Apr 14 '17 at 17:17
  • @Garrett Also take a look here: http://stackoverflow.com/questions/12860169/filesystemwatcher-works-in-the-powershell-ise-but-not-when-run?rq=1 – Christopher Apr 14 '17 at 17:19

0 Answers0