Here is a simple function in PowerShell to ping a system via WMI query and return the IP address.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function Ping-Host { Param([string]$computername=$(Throw "No system name provided.")) $query="Select * from Win32_PingStatus where address='$computername'" $wmi=Get-WmiObject -query $query write $wmi } $computer = "COMPUTER1" #ping the computer if ($pingResult) { #clear PingResult if it has a left over value Clear-Variable pingResult } $pingResult=Ping-Host $computer Write-Debug "Pinged status code is $($pingResult.Statuscode)" if ($pingResult.StatusCode -eq 0) { Write-Host "Query successful on " $computer " - IP: " $pingResult.ProtocolAddress #get remaining information via WMI Trap { #define a trap to handle any WMI errors Write-Warning ("Problem with {0}" -f $computer.toUpper()) Write-Warning $_.Exception.GetType().FullName Write-Warning $_.Exception.message Continue } } |