Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell running under a service hangs on *.zip CopyHere

I'm running a Windows Service (Hudson) which in turn spawns a PowerShell process to run my custom PowerShell commands. Part of my script is to unzip a file using CopyHere. When I run this script locally, I see a progress dialog pop up as the files are extracted and copied. However, when this runs under the service, it hangs at the point where a dialog would otherwise appear.

Here's the unzip portion of my script.

# Extract the contents of a zip file to a folder
function Extract-Zip {
    param([string]$zipFilePath, [string]$destination)
    if(test-path($zipFilePath))     {   
        $shellApplication = new-object -com shell.application

        $zipFile = get-item $zipFilePath
        $zipFolder = $shellApplication.NameSpace($zipFile.fullname)

        $destinationFile = get-item $destination
        $destinationFolder = $shellApplication.NameSpace($destinationFile.fullname)

        $destinationFolder.CopyHere($zipFolder.Items())
    }
}

I suspect that because its running under a service process which is headless (no interaction with the desktop), its somehow stuck trying to display a dialog.

Is there a way around this?

like image 965
Trinition Avatar asked Jan 12 '10 15:01

Trinition


2 Answers

If it's still actual, I managed to fix this with having CopyHere params equal 1564.

So in my case extract zip function looks like:

    function Expand-ZIPFile{
    param(
    $file, $destination
    )
    $shell = new-object -com shell.application
    $zip = $shell.NameSpace($file)
    foreach($item in $zip.items())
    {
    $shell.Namespace($destination).copyhere($item,1564)
    "$($item.path) extracted"
    }

1564 description can be found here - http://msdn.microsoft.com/en-us/library/windows/desktop/bb787866(v=vs.85).aspx:

(4) Do not display a progress dialog box.
(8) Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
(16) Respond with "Yes to All" for any dialog box that is displayed.
(512) Do not confirm the creation of a new directory if the operation requires one to be created.
(1024) Do not display a user interface if an error occurs.

like image 169
Iurii Avatar answered Sep 28 '22 05:09

Iurii


If this is running on Vista or Windows 7, popping up UI from a service isn't going to be seen by the end user as you suspected. See this paper on Session 0 Isolation. However, does the progress dialog require user input? If not, I wouldn't think that would cause the service to hang. I would look for an option to disable the progress display. If you can't find that, then try switching to another ZIP extractor. PSCX 1.2 comes with an Expand-Archive cmdlet. I'm sure there are also others available.

like image 45
Keith Hill Avatar answered Sep 28 '22 06:09

Keith Hill