Powershell - Check on Remote Process, if done continue

后端 未结 2 1388
深忆病人
深忆病人 2021-01-12 18:22

As part of a backup operation, I am running the 7zip command to compress a folder into a single .7z file. No problems there as I am using the InVoke-WMIMethod.<

2条回答
  •  时光说笑
    2021-01-12 19:07

    You can invoke the Wait-Process cmdlet on the remote computer with the Invoke-Command cmdlet. Example:

    $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
    

    Since you mentioned using Invoke-Command is not an option, another option is polling. Example:

    $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"
    

提交回复
热议问题