Remotely zipping files with PowerShell start-process and invoke-command

What Would Be Cool picture What Would Be Cool · Sep 7, 2011 · Viewed 7.5k times · Source

I want to be able to remote into a system and zip or unzip files there and have the process signal when it is complete. Start-process works with the -wait parameter to run 7z.exe synchronously from PowerShell. When I try to combine that with invoke-command to run the same command remotely, it does not honor the wait parameter and I believe it is killing the process since it returns quickly and never produces a zip file.

[string]$sevenZip = "C:\Program Files\7-zip\7z.exe"
[Array]$arguments = "a", $zipFilename, $dirToZip;

"Starting $sevenZip with $arguments"
Start-Process $sevenZip "$arguments" -Wait
#blocks and waits for zip file to complete

I originally tried the PSCX write-zip & expand-archive, but the latter is not compatible with 64-bit .NET 4.0 configuration. So now I'm trying to call 64-bit 7z.exe through the command line. I'm not receiving any errors. PowerShell reports the job as running state and then complete, and no zip file is produced.

Invoke-Command -ComputerName localhost -FilePath 'C:\Scripts\ZipIt.ps1' -ArgumentList    'd:\TestFolder','d:\promote\TestFile.7z' -AsJob

Appreciate any help or pointers.

Thanks, Gregory

Answer

Efran Cobisi picture Efran Cobisi · Sep 7, 2011

Since Start-Process will be used synchronously here, I would recommend avoiding it and just use the 7z.exe executable:

$sevenZip = "C:\Program Files\7-zip\7z.exe"
&$sevenZip a $zipFileName $dirToZip

Doing so will naturally block your script until 7zip completes its job.