The Need:
Today I needed to go through Active Directory and produce a list of all servers, then check which were active. I needed to output this to file this time but more often I will just need to output to the screen, so i decided to make use of the switch statement.
The Script:
cls
write-Host "This script will list all the servers in AD and then ping them."
$output = (Read-Host "Press 1 to output to the screen or 2 to output to file")
Switch ($output) {
1 {
Write-Host "Please Wait....Checking AD now!" -fore Yellow
#Get Servers from AD
$ServerList = @(get-qadcomputer -OSName "Windows Server*")
$Servers = $ServerList | foreach {$_.Name}
#Ping Server
function Ping-Server{
$ping = gwmi -q "SELECT * FROM Win32_Pingstatus WHERE Address = '$serv'"
if($ping.statusCode -eq 0) { write-Host "$serv is up" }
else { Write-Host "$serv is not responding" -fore red }
}
foreach ($serv in ($servers)){Ping-Server}
write-host "Done!"
}
2 {
Write-Host "When complete the files will be saved in C:\Temp"
Write-Host "Please Wait....Checking AD now!" -fore Yellow
#Create 3 blank files
New-Item C:\Temp\Servers-in-AD.txt -Type File -force | out-null
New-Item C:\Temp\Servers-Responding.txt -Type File -force | out-null
New-Item C:\Temp\Servers-Not-Responding.txt -Type File -force | out-null
#Get Servers from AD
$ServerList = @(get-qadcomputer -OSName "Windows Server*"); $Servers = $ServerList | foreach {$_.Name}
$Servers >> C:\Temp\Servers-in-AD.txt
#Ping Server
function Ping-Server{
$ping = gwmi -q "SELECT * FROM Win32_Pingstatus WHERE Address = '$serv'"
if($ping.statusCode -eq 0) { Write-output "$serv is up" >>C:\Temp\Servers-Responding.txt}
else { Write-output "$serv is not responding" >>C:\Temp\Servers-Not-Responding.txt}
}
foreach ($serv in ($servers))
{
Ping-Server
}
write-host "Done!"
#Open output files
Invoke-Item C:\Temp\Servers-in-AD.txt
Invoke-Item C:\Temp\Servers-Not-Responding.txt
Invoke-Item C:\Temp\Servers-Responding.txt
}
Default {Write-Host "You didn't select option 1 or 2" -fore Red}
}
Friday, 15 May 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment