开发者

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

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 t开发者_高级运维he 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


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.


Using 7z.exe directly would do the trick. If you find you need to unzip a password protected file you could use:

$sevenZip = "C:\Program Files\7-zip\7z.exe"
&$sevenZip e -y  "-o$dirToUnZip" -psomepassword $dirToUnZip\$zipFileName 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜