Timeout Get-WMIObject cmdlet

后端 未结 6 1304
星月不相逢
星月不相逢 2020-12-06 15:00

I run a script which performs many WMI-querys - but the cmdlet hangs if the server doesn\'t answer.. Is there any way I can make this (or any other cmndlet for that matter)

6条回答
  •  悲&欢浪女
    2020-12-06 15:24

    when creating the job using get-wmiobject assign that job to a variable, then that variable can be piped into get-job for status or receive-job for results

    $ThisJob = start-job -scriptblock {param ($Target) Get-WmiObject -Class Win32_Service -ComputerName $Target -AsJob} -ArgumentList $server
    $Timer = [System.Diagnostics.Stopwatch]::StartNew()
    While ($ThisJob | Get-Job | where {$_.State -imatch "Running"}){
        If ($Timer.Elapsed.Seconds -ge 5) {
            echo "five seconds has passed, removing"
            $ThisJob | Get-Job | Remove-Job -Force
            } # end if
        echo "still running"
        Start-Sleep -Seconds 3
        } # end while
    
    $Results = $ThisJob | where {$_.State -inotmatch "failed"} | receive-job
    $Timer.Stop | out-null
    

提交回复
热议问题