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!