Why am I getting exceptions from my pings?

Hi all.

I'm writing a script that changes all Local Admin passwords to something random. First the script searches AD for computers and then checks if they are alive to receive the password change request.

Here is my code:

Write-Host "*** Changing local administrator passwords on ALL computers in this domain..." -foregroundcolor green
Write-Host "*** Your computer is in the following Active Directory domain: " -foregroundcolor green
$domain = Get-ADDomain -current LocalComputer
Write-Host "*** $domain" -foregroundcolor red

# Enumerate all computers in the current domain
#$computers = Get-ADComputer -Filter {Enabled -eq "true"} -SearchBase $domain

# Create ping object
$ping = new-object System.Net.NetworkInformation.Ping

#Get-Process | ForEach-Object {Write-Host $_.name -foregroundcolor cyan}
#foreach ($computer in $computers)
#Get-ADComputer -Filter {Enabled -eq "true"} -SearchBase $domain |
Get-ADComputer -Filter * -SearchBase $domain |
	Where-Object { $_.Enabled -eq $true } |
	Where-Object { $_.DNSHostName -ne "" } |
	#Where-Object { $_.DNSHostName -eq "test-desktop.lab.local" } |
	ForEach-Object { 
	
		Write-Host "%%% Trying to ping:" $_.DNSHostName
				
		$Reply = $ping.send($_.DNSHostName, 2)
		if($Reply.status -eq "Success") {
			write-host "*** Ping reply from: " -NoNewline -foregroundcolor green
			write-host $_.DNSHostName," ipaddr:" -NoNewline -foregroundcolor cyan
			write-host $Reply.Address -foregroundcolor cyan
			
		} else {
			write-host "--- NO ping reply from: " -NoNewline -foregroundcolor red
			write-host $_.DNSHostName," ipaddr:" -NoNewline -foregroundcolor red
			write-host $Reply.Address -foregroundcolor red
		}
	} 


This sorta works, but I get alot of exceptions, and I don't know how to debug those any further! :-/

Here is my output from this script:

PS C:\temp> .\change-local-admin-passwords.ps1
*** Changing local administrator passwords on ALL computers in this domain...
*** Your computer is in the following Active Directory domain:
*** DC=lab,DC=local
%%% Trying to ping: <testhost1.lab.local>
Exception calling "Send" with "2" argument(s): "An exception occurred during a
Ping request."
At C:\temp\change-local-admin-passwords.ps1:47 char:22
+         $Reply = $ping.send <<<< ($_.DNSHostName, 2)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

--- NO ping reply from: testhost1.lab.local  ipaddr:
%%% Trying to ping: testhost2.lab.local
*** Ping reply from: testhost3.lab.local  ipaddr:10.1.0.105
%%% Trying to ping: server1.lab.local
--- NO ping reply from: server1.lab.local  ipaddr:
%%% Trying to ping: server3.lab.local
*** Ping reply from: server3.lab.local  ipaddr:10.1.0.117
%%% Trying to ping: server5.lab.local
*** Ping reply from: server5.lab.local  ipaddr:10.1.0.118
%%% Trying to ping: workstation1.lab.local
*** Ping reply from: workstation1.lab.local ipaddr:10.6.1.128
%%% Trying to ping: workstation2.lab.local
*** Ping reply from: workstation2.lab.local  ipaddr:10.1.0.31
%%% Trying to ping: workstation3.lab.local
*** Ping reply from: workstation3.lab.local  ipaddr:10.1.1.6
%%% Trying to ping: workstation4.lab.local
*** Ping reply from: workstation4.lab.local  ipaddr:10.1.0.18
%%% Trying to ping: workstation5.lab.local
*** Ping reply from: workstation5.lab.local  ipaddr:10.1.0.89
%%% Trying to ping: server2.lab.local
*** Ping reply from: server2.lab.local  ipaddr:10.1.0.28
%%% Trying to ping: server9.lab.local
Exception calling "Send" with "2" argument(s): "An exception occurred during a
Ping request."
At C:\temp\change-local-admin-passwords.ps1:47 char:22
+         $Reply = $ping.send <<<< ($_.DNSHostName, 2)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

*** Ping reply from: server9.lab.local  ipaddr:10.1.0.28
%%% Trying to ping: x1.lab.local
*** Ping reply from: x1.lab.local  ipaddr:10.1.1.9
%%% Trying to ping: x2.lab.local
*** Ping reply from: x2.lab.local  ipaddr:10.1.0.67
%%% Trying to ping: x10.lab.local
*** Ping reply from: x10.lab.local  ipaddr:10.1.1.20
%%% Trying to ping: y1.lab.local
*** Ping reply from: y1.lab.local  ipaddr:10.1.1.21
%%% Trying to ping: y50.lab.local
*** Ping reply from: y50.lab.local  ipaddr:10.1.0.85
%%% Trying to ping: domain1.lab.local
Exception calling "Send" with "2" argument(s): "An exception occurred during a
Ping request."
At C:\temp\change-local-admin-passwords.ps1:47 char:22
+         $Reply = $ping.send <<<< ($_.DNSHostName, 2)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

*** Ping reply from: domain1.lab.local  ipaddr:10.1.0.85
<END>



Why am I getting these exceptions and how can I find their root cause??? It doesn't seem to have anything to do with the PING request, it goes through and I get a reply!

HELP!

December 15th, 2011 12:30am

What the...? Why does this forum system not use mono-block font for my code pastes? Urrr!

Free Windows Admin Tool Kit Click here and download it now
December 15th, 2011 12:36am

About this behavior - http://social.msdn.microsoft.com/Forums/en-SG/Vsexpressinstall/thread/c616f2cc-daf7-42a6-a387-2eae70bddf8b

Instead of System.Net.NetworkInformation.Ping use Test-Connection.

 

 

December 15th, 2011 12:38am

Thanks! :-D

My new code for scanning the machines:

Get-ADComputer -Filter * -SearchBase $domain |
	Where-Object { $_.Enabled -eq $true } |
	Where-Object { $_.DNSHostName -ne "" } |
	ForEach-Object {
		Trap {
				write-warning "EXCEPTION TRAPPED:"
				write-warning $_.Exception.Message
				Continue
			} if (Test-Connection $_.DNSHostName -erroraction stop -quiet -count 1 -delay 1) {
				write-host $_.DNSHostName,"is UP"
			} else {
				write-host $_.DNSHostName,"is DOWN" -ForegroundColor RED
			}
	}            

Free Windows Admin Tool Kit Click here and download it now
December 15th, 2011 12:25pm

Test-Connection is too slow... System.Net.NetworkInformation.Ping is far more efficient !

For example against more than 1200 hosts accross the world Test-Connection takes about 60 seconds while System.Net.NetworkInformation.Ping do the same job with more accurate results in about 20 seconds...

January 30th, 2014 5:52am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics