Advertisement
tankcr

getwmi timer

Oct 18th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $Timeout = 30
  2. $RetryInterval = 3
  3. $wmi = $null
  4. try
  5. {
  6.     $timer = [Diagnostics.Stopwatch]::StartNew()
  7.     while (($timer.Elapsed.TotalSeconds -lt $Timeout) -and ($wmi =Get-WmiObject -ComputerName $Server -Namespace root\cimv2 -class Win32_OperatingSystem)) {
  8.     Start-Sleep -Seconds $RetryInterval
  9.     $totalSecs = [math]::Round($timer.Elapsed.TotalSeconds,0)
  10.     Write-Verbose -Message "Still waiting for action to complete after [$totalSecs] seconds..."
  11.     }
  12.     $timer.Stop()
  13.     if ($timer.Elapsed.TotalSeconds -gt $Timeout -and ($wmi -eq $null)) {
  14.         throw 'Action did not complete before timeout period.'
  15.     } else {
  16.         Write-Verbose -Message 'Action completed before timeout period.'
  17.     }
  18. }
  19. catch
  20. {
  21.     Write-Error -Message $_.Exception.Message
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement