作为备份操作的一部分,我正在运行7zip命令将文件夹压缩为单个.7z文件。那里没有问题,因为我正在使用InVoke-WMIMethod

例子:

$zip = "cmd /c $irFolder\7za.exe a $somedirectory.7z $somedirectory"
"InVoke-WmiMethod -class Win32_process -name Create -ArgumentList $zip -ComputerName $remotehost"

我的问题出现在脚本继续执行时,7za.exe进程尚未完成。然后,我尝试从远程系统中复制该项目,但该项目不完整或失败。

有人可以指出我的方向来确定如何识别7za.exe进程是否仍在运行,等到它死了之后再继续执行我的脚本的其余部分吗?

我可以通过...掌握从远程系统中拉出进程的信息。
get-wmiobject -class Win32_Process -ComputerName $remotehost | Where-Object $_.ProcessName -eq "7za.exe"}

不知道如何将其转换为适用于我的问题的信息。

答案更新:(表示感谢@dugas表示感谢)

这将为需要它的人提供一些反馈...
do {(Write-Host "Waiting..."),(Start-Sleep -Seconds 5)}
until ((Get-WMIobject -Class Win32_process -Filter "Name='7za.exe'" -ComputerName $target | where {$_.Name -eq "7za.exe"}).ProcessID -eq $null)

最佳答案

您可以使用Invoke-Command cmdlet在远程计算机上调用Wait-Process cmdlet。例子:

$process = Invoke-WmiMethod -Class Win32_Process -Name create -ArgumentList notepad -ComputerName RemoteComputer

Invoke-Command -ComputerName RemoteComputer -ScriptBlock { param($processId) Wait-Process -ProcessId $processId } -ArgumentList $process.ProcessId

由于您提到的不是使用Invoke-Command的选项,因此另一个选项是轮询。
例子:
$process = Invoke-WmiMethod -Class Win32_Process -Name create -ArgumentList notepad -ComputerName hgodasvccr01
$processId = $process.ProcessId

$runningCheck = { Get-WmiObject -Class Win32_Process -Filter "ProcessId='$processId'" -ComputerName hgodasvccr01 -ErrorAction SilentlyContinue | ? { ($_.ProcessName -eq 'notepad.exe') } }

while ($null -ne (& $runningCheck))
{
 Start-Sleep -m 250
}

Write-Host "Process: $processId is not longer running"

关于Powershell-检查远程进程,如果继续则继续,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18341767/

10-16 06:30