PowerShell script not executing properly as shutdown script

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5835

    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"
    try
    {
    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"
    $destination.Copyhere($zip_file.items())
    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 :

    Hello
    1
    2 - c:\somedir\somezip.zip
    3 - c:\somedir
    4
    5
    6
    7
    8

    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:
    $Error.Clear()
    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.

You must be logged in to reply to this topic.