blog: Sick of WMI timeouts stopping your script? Try Test-Host!
You can see more details about that script here: Test-Port (kinda like portqry without verbose output)
With v2 just on the horizon I wanted to see what I could improve using Powershell v2 and decided to re-write the code (found below.)
Some of the new features are:
- Rename to Test-Host
- Built in Help and examples (biggy)
- Parameter Binding
- Added more logging with -verbose switch
This can be very useful if you ever have the need to do a task on a number of Computers (specifically WMI.) For example, If I needed to get all the machines running x64 in my domain I could something like this:
$computers = Get-QADComputer -search $OU | %{$_.dnshostname} | Test-Host -tcp 135 $query = "SELECT * FROM win32_processor WHERE addresswidth='64'" Get-WMIObject -query $query -comp $computers -asJob
Here is the code for Test-Host
Can download here: Test-Host.ps1 (from Poshcode.org)
function Test-Host { [CmdletBinding()] Param( [Parameter(ValueFromPipeline=$true,Mandatory=$True)] [string]$Server, [Parameter()] [int]$TCPPort, [Parameter()] [int]$timeout=1000, [Parameter()] [string]$property ) Begin { function TestPort { Param($srv,$tport,$tmOut) Write-Verbose " [TestPort] :: Start" Write-Verbose " [TestPort] :: Setting Error state = 0" $Error\3ActionPreference = "SilentlyContinue" Write-Verbose " [TestPort] :: Creating [system.Net.Sockets.TcpClient] instance" $tcpclient = New-Object system.Net.Sockets.TcpClient Write-Verbose " [TestPort] :: Calling BeginConnect($srv,$tport,$null,$null)" $iar = $tcpclient.BeginConnect($srv,$tport,$null,$null) Write-Verbose " [TestPort] :: Waiting for timeout [$timeout]" $wait = $iar.AsyncWaitHandle.WaitOne($tmOut,$false) # Traps trap { Write-Verbose " [TestPort] :: General Exception" Write-Verbose " [TestPort] :: End" return $false } trap [System.Net.Sockets.SocketException] { Write-Verbose " [TestPort] :: Exception: $($_.exception.message)" Write-Verbose " [TestPort] :: End" return $false } if(!$wait) { $tcpclient.Close() Write-Verbose " [TestPort] :: Connection Timeout" Write-Verbose " [TestPort] :: End" return $false } else { Write-Verbose " [TestPort] :: Closing TCP Sockett" $tcpclient.EndConnect($iar) | out-Null $tcpclient.Close() } if($?){Write-Verbose " [TestPort] :: End";return $true} } function PingServer { Param($MyHost) Write-Verbose " [PingServer] :: Pinging $MyHost" $pingresult = Get-WmiObject win32_pingstatus -f "address='$MyHost'" Write-Verbose " [PingServer] :: Ping returned $($pingresult.statuscode)" if($pingresult.statuscode -eq 0) {$true} else {$false} } } Process { Write-Verbose "" Write-Verbose " Server : $Server" if($TCPPort) { Write-Verbose " Timeout : $timeout" Write-Verbose " Port : $TCPPort" if($property) { Write-Verbose " Property : $Property" if(TestPort $Server.$property -tport $TCPPort -tmOut $timeout){$Server} } else { if(TestPort $Server -tport $TCPPort -tmOut $timeout){$Server} } } else { if($property) { Write-Verbose " Property : $Property" if(PingServer $Server.$property){$Server} } else { Write-Verbose " Simple Ping" if(PingServer $Server){$Server} } } Write-Verbose "" } }
tshell :: May.26.2009 :: Active Directory, All :: No Comments »

