PowerShell script not executing properly as shutdown script

by mdwheaton at 2013-04-22 11:45:43

I have a script to unzip files that I’m testing as a shutdown script. I have the property to visibly display script output set to true as well so I can see if it’s being run. The script doesn’t execute properly when it’s used as a shutdown script.

The script runs as expected from the command line. Also runs correctly when testing the script using psexec to run as SYSTEM.

Here’s the script. The output follows the script. All the output lines display properly. The script also sleeps at the end as expected. However, the zip file does NOT get extracted. The script goes right through that line and doesn’t even throw an exception. Again, this script runs perfectly from the command line.

I’m stumped as to why all the rest of the script works, but the unzip part doesn’t.

Write-Host "Hello"
Write-Host "1"
$ZipFile = $args[0]
Write-Host "2 - $ZipFile"
$TargetDir = $args[1]
Write-Host "3 - $TargetDir"
new-item $TargetDir -itemtype directory -ErrorAction SilentlyContinue
Write-Host "4"
$shell_app=new-object -com shell.application
Write-Host "5"
$zip_file = $shell_app.namespace($ZipFile)
Write-Host "6"
$destination = $shell_app.namespace($TargetDir)
Write-Host "7"
Write-Host "8"
Start-Sleep -s 10
exit 0
Catch [Exception]
Write-Host "Generic Exception"
Write-Host $
| Select *
exit 1

The output I get is :

2 - c:\somedir\somezip.zip
3 - c:\somedir
by poshoholic at 2013-04-23 21:15:28
Here’s one suggestion for finding hard-to-find errors like this: at the top of your script, invoke the following:
Then, later in your script, after you think the zip extraction is complete, check if $Error contains anything and try to write it to disk so that you can see what non-terminating errors occurred when you run it as a shutdown script.