Troubleshooting Pre- and Post-Scripts for Veeam
Writing powershell and testing them is sometimes hard an annoying. Especially if you have a working script which runs when you start it manually, but getting an error message from VBR but don’t now why this happens.
First idea was easy. Add an exit code into my script, but after trying this I was not able to see this exit codes here in VBR. Ok it seems I have to dig deeper. I checked the “C:\ProgramData\Veeam\Backup\Jobname\Job.Jobname.log” file and searched for ps1.
Executing custom command [powershell.exe], arguments [ -ExecutionPolicy ByPass -Command " try {& 'C:\your\scriptpath\name.ps1 -para1 test -para2 test' -ErrorAction Stop } catch { exit 1 } "]
From this line on the logfile we can see, that every exit code we would use in the script would not reflect in VBR job log. Every time the script exits with another exit code than 0 this command catches the error and report an exit 1 to VBR.
Because my error only happend when I was running the script as a pre-job script I would prefer to see what happens on the “cli” when running the script from VBR. This is directly not possible because script output to console is not recorded to VBR job log.
In my case and at most customers the VBR Server is running with LocalSystem Account and we need to use psexec to start the script in the same security context as VBR itself. I started a Command Prompt in Admin Mode and did this:
C:\Users\Administrator>c:\install\psexec\PsExec64.exe -i -s powershell.exe -noexit -ExecutionPolicy ByPass -Command " & 'C:\scripts\yourscript.ps1" -ErrorAction Stop"
Using the parameters -i -s for psexec64.exde allows us to run a binary in LocalSystem Context. In this case of cause we want to start a powershell.exe with a script but the window will close as fast as hell so I’m not able to see that happend in this window. But for this we can use the -noexit parameter for powershell.exe. This prevent closing the window and you now can read the error message.
Happy Debugging ;-)