So to run PowerShell jobs, I typically use a PSJob.cmd that executes POWERSHELL.EXE pointing to the passed in script name and redirect output using old DOS semantics (> file.log 2>&1). Well this worked fine for single threaded PowerShell scripts. However, when I started using Start-Job to get a lot of work done faster, I started getting file handle exceptions and some really weird behavior. After a couple hours trying different things, thinking it was my Start-Job script, I decided to change the way I redirected output and switched to use Out-File.
Well, long, painful story brought short is that use Out-File, not DOS output redirection. For your enjoyment, this is my PSJob.cmd script I use to kick off scripts from our enterprise scheduler.
I know, amazing right! ;)
Well, long, painful story brought short is that use Out-File, not DOS output redirection. For your enjoyment, this is my PSJob.cmd script I use to kick off scripts from our enterprise scheduler.
- POWERSHELL -NonInteractive -NoLogo -NoProfile -ExecutionPolicy ByPass -Comand "%~dp0%~1.ps1 | Out-File %~dp0%~1.log -Encoding ASCII -Force"
- EXIT /B %ERRORLEVEL%
Nice kicker Adam
ReplyDeleteDoes the rdirection support in framework 3 work for you? This may work better if you use -verbose and -debug.
... -Comand "%~dp0%~1.ps1 *> %~dp0%~1.log
Seanchoq