When I am testing scripts with wmi queries during business hours, they randomly fail on random servers.
Meaning, I need to put in "retry 3 times" logic in order to confirm if the device I am querying is actually having an RPC problem such as a stalled / stopped WMI service.
What is the best way to retry such queries? I feel that Im over doing in the example below...
Do {
$WMIErrorCounter=0
Try {
$Service = Get-WmiObject -Class Win32_Service -ComputerName $Computer -EnableAllPrivileges -ErrorAction Stop | Select Caption, State, PathName | ? {$_.PathName -like "*sqlservr.exe*" -and $_.Caption -notlike "*$*" -and $_.Caption -notlike "*#*"}
}
Catch {
$WMIErrorCounter++
$ErrorMessage
IF ($WMIErrorCounter -eq 3){$WMIError = $True}
Else {$WMIError = $False}
}
If ($WMIError){
$ErrorMessage = "WMI Connection Failed - $Error[0].FullyQualifiedErrorId"
$SQLProperties = SetOutput -ComputerName $Computer -ErrorMessage $ErrorMessage
New-Object -TypeName psobject -Property $SQLProperties
}
$WMIErrorCounter = 3
} Until ($WMIErrorCounter = 3)