Saturday, September 13, 2008

Talking PowerShell Ping-Script

Tonight I wrote a short Powershell script to have PowerShell go through a list of servers and report back which are up.  Just for fun I thought I would add in a couple of lines to make PowerShell tell me which servers are down by using the voice functions available and by highlighting the down servers with a red background.

I know, it's not groundbreaking but I like it!



Here is the code.

cls
$Voice = new-object -com SAPI.SpVoice

function Ping-Host {
BEGIN {}
PROCESS {

$results = gwmi -query "SELECT * FROM Win32_PingStatus WHERE Address = '$computer'"

if ($results.StatusCode -eq 0) { 

Write-Host "$computer is Pingable"
} else {
$Voice.Speak( "Alert Alert Alert $computer is down", 1 )
Write-Host "$computer is not Pingable" -BackgroundColor red
}
}
END {}
}
$computers = Get-Content q:\servers.txt
foreach ($computer in $computers) {
if (Ping-Host $computer) {
}
}

The output looks like this:




All i'm doing is listing a bunch of servers in Q:\Servers, then piping each one through a ping and outputing on screen the servers that respond and the ones that don't go to the screen with a red background and to the voice thingy that talks like in war games.

Right now to me this is the coolest thing I have made in PowerShell.


11 comments:

Anonymous said...

This is pretty cool. Is there any way that you could continuously loop through the list of hosts during a set period of time?

SynJunkie said...

I could create an infinate loop at the end of the script with the following code:

$loop = 1

do {$computers = Get-Content q:\servers.txt
foreach ($computer in $computers) {
if (Ping-Host $computer) {
}
}}
until ($loop -gt 2)



So the results look like this:


cls
$Voice = new-object -com SAPI.SpVoice

function Ping-Host {
BEGIN {}
PROCESS {

$results = gwmi -query "SELECT * FROM Win32_PingStatus WHERE Address = '$computer'"

if ($results.StatusCode -eq 0) {

Write-Host "$computer is Pingable"
} else {
$Voice.Speak( "Alert $computer is down", 1 )
Write-Host "$computer is not Pingable" -BackgroundColor red
}
}
END {}
}

$loop = 1

do {$computers = Get-Content q:\servers.txt
foreach ($computer in $computers) {
if (Ping-Host $computer) {
}
}}
until ($loop -gt 2)

Eddie said...

Dig the script... Now i am trying to figure out where i can tell it to export to a txt or csv file. Any help would be appreciated!

Thanks!

SynJunkie said...

Alex

I think that if you add a pipe and tee-object followed by a path it will print to screen and to an output file.

so after the wite host part of the script.............

| tee-object -filepath c:\file.txt

I'll have a play and see if i can get it sorted.

KamiCrazy said...

cls


function Ping-Host {
BEGIN {}
PROCESS {


$results = gwmi -query "SELECT * FROM Win32_PingStatus WHERE Address = '$computer'"
$date = get-date

if ($results.StatusCode -eq 0) {


Write-Output "$date $computer is Pingable" | out-file -append -filepath C:\vpnlog.txt
Write-Host "$computer is Pingable"
} else {
Write-Output "$date $computer is not Pingable" | out-file -append -filepath C:\vpnlog.txt
Write-Host "$computer is not Pingable" -BackgroundColor red
}
}
END {}
}
$computers = Get-Content c:\mpl\routers.txt
foreach ($computer in $computers) {
if (Ping-Host $computer) {
}
}

SynJunkie said...

Ah but where's the talking eh Mr KamiCrazy?

Anonymous said...

Outstanding, very cool.

I am going to put some more inputs and share my version

SynJunkie said...

Cool, i'll be interested to see that.

Cheers

Syn

Anonymous said...

Could anyone tell me how the script sends a mail to me if there is a server down?

Thanks a lot

SynJunkie said...

I have no idea. when I have time i'll look into though and make a post.

Cheers

Lee

Anonymous said...

In my case, I'm using this to ping routers. How can you assign names to these IPs so the voice doesnt read the IP but reads an assigned name?