Powershell - Check on Remote Process, if done continue

后端 未结 2 1387
深忆病人
深忆病人 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 18:57

    You should be able to do it with a do... while loop that just sleeps until the process is finished.

     do { 
        "waiting"
        start-sleep 10 
        } while (gwmi -class win32_process -ComputerName $remotehost | Where ProcessName -eq "7za.exe")
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
提交回复
热议问题