使用 PowerShell 启动进程和调用命令远程压缩文件
我希望能够远程进入系统并在那里压缩或解压缩文件,并在完成时收到进程信号。 Start-process 与 -wait 参数配合使用,从 PowerShell 同步运行 7z.exe。当我尝试将其与 invoke-command 结合起来远程运行相同的命令时,它不遵守等待参数,并且我相信它正在终止该进程,因为它快速返回并且从不生成 zip 文件。
[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
我最初尝试过 PSCX write-zip & Expand-archive,但后者与 64 位 .NET 4.0 配置不兼容。所以现在我尝试通过命令行调用64位7z.exe。我没有收到任何错误。 PowerShell 将作业报告为运行状态,然后完成,并且不会生成 zip 文件。
Invoke-Command -ComputerName localhost -FilePath 'C:\Scripts\ZipIt.ps1' -ArgumentList 'd:\TestFolder','d:\promote\TestFile.7z' -AsJob
感谢任何帮助或指示。
谢谢, 格雷戈里
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于此处将同步使用 Start-Process,因此我建议避免使用它,而只使用 7z.exe 可执行文件:
这样做自然会阻止您的脚本,直到 7zip 完成其工作。
Since Start-Process will be used synchronously here, I would recommend avoiding it and just use the
7z.exe
executable:Doing so will naturally block your script until 7zip completes its job.
直接使用 7z.exe 就可以了。如果您发现需要解压缩受密码保护的文件,您可以使用:
Using 7z.exe directly would do the trick. If you find you need to unzip a password protected file you could use: