WMI hangs and how to avoid them


Once again I was asked to query a couple of thousand servers for some basic information and once again there were some servers that turn into black holes when I run a wmi query against them. They don’t error, they don’t respond and they stop my script dead in its tracks.
I had to go back into my archives to figure out how I solved this the last time and here it is.
These are functions of Powershell version II.


###### Start Posh Script ######## 
 # Use the test-port function to make sure that the RPC port is listening 
  
function Test-Port {   
 Param(    
	[string] $srv,    
	$port=135,    
	$timeout=3000,    
	[switch]$verbose    
	)    
 # Test-Port.ps1    
 # Does a TCP connection on specified port (135 by default)    
 $ErrorActionPreference = SilentlyContinue	    
 # Create TCP Client    
	$tcpclient = new-Object system.Net.Sockets.TcpClient    
 # Tell TCP Client to connect to machine on Port    
	$iar = $tcpclient.BeginConnect($srv,$port,$null,$null)    
 # Set the wait time    
	$wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)    
 # Check to see if the connection is done    
	if(!$wait)    
 {    
 # Close the connection and report timeout    
	$tcpclient.Close()    
	if($verbose){Write-Host Connection Timeout	}    
	Return $false    
 }    
 else    
 {    
 # Close the connection and report the error if there is one    
	$error.Clear()    
	$tcpclient.EndConnect($iar) | out-Null    
	if(!$?){if($verbose){write-host $error[0]};$failed = $true}    
	$tcpclient.Close()    
 }    
 # Return $true if connection Establish else $False    
	if($failed){    
	return $false    
	} else {    
	return $true    
	}    
} # http://technet.microsoft.com/en-us/library/ff730959.aspx
  
# First, make sure the computer is Pingable,
  
If (Test-Connection -computername $server -Quiet -count 1){ 
 # Use the test-port function to make sure that the RPC port is listening 
  
 $a = Test-Port $server   
	If ($a) {    
 # Run your WMI query as a JOB 
	$WMIJob = Get-WmiObject Win32_OperatingSystem -Comp $server -AsJob    
	Wait-Job -ID $WMIJob.ID -Timeout 20 # the Job times out after 20 seconds. 
	$os = Receive-Job $WMIJob.ID
  
	if ($os -ne $null) {   
		switch ($os.version) {    
			5.1.2600 {$osVer = "XP/IIS5.1"}    
			5.2.3790 {$osVer = "Server 2003/IIS6"}    
			6.0.6001 {$osVer = "Server 2008/IIS7"}    
			6.1.7600 {$osVer = "Server 2008 R2/IIS7.5"}    
		 } 

	Else {      
		 $b = $error | select Exception      
		 $E = $b -split (":")      
		 $x = $E[1]      
		 $Error.Clear()      
		 $d = $x.StartsWith(" Access")      
		 If ($d){      
		 Add-Content -Path $strFile "$server `tAccess Denied"
		 }      
		 Else {Add-Content -Path $strFile "$server `t$x"	       
		 }      
		 }
  ############# End POSH Script ###############

# Access denied brings TMI along with it, so I shortened it to just “Access Denied”
# Otherwise give me the whole error. I am writing out my results to a text file, hence
# the Add-Content lines.
Hope this helps you as much as it did me.

8 thoughts on “WMI hangs and how to avoid them

  1. Pingback: WMI hangs and how to avoid them | The Old Dogs Scripting Blog | Print Server Reviews

  2. Pingback: UUID mit Powershell auslesen | Tutorials from tor.eu

Leave a comment