Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

powershell alert when ping time drops

Hi guys, we have a switching mechanism on our servers whereby the networks switch speeds varying on location. These times can vary but is it possible to email when the ping time drops, which would indicate the network switch. I have the following script below but doesnt seem to work correctly. also is it possible to loop until the condition is met

thanks


$ping = ping server
if ($ping -gt 1000)
{
Send-MailMessage -To user@company.com -From "user@company.com" -SmtpServer smtp.domain.com -Subject "Ready"
}
0
cwstad2
Asked:
cwstad2
  • 2
  • 2
2 Solutions
 
Dale HarrisCommented:
Using this snippet:
$Comp = "localhost"
$ping = get-wmiobject -Query "select * from win32_pingstatus where Address='$comp'"

You should be able to do a simple check against ping time:

if ($ping.responsetime -gt 1000)
{
//Do something
}

HTH,

Dale Harris
0
 
QlemoC++ DeveloperCommented:
"Ping time drops" - does that mean the time is greater or less than a limit? Your code fragment would trigger if the time needed for a ping is more than 1 second.
$Ping = Test-Connection -Count 1 -TTL 2000 -EA SilentlyContinue Server
if (!$Ping -or $Ping.ResponseTime -gt 1000)
{
  Send-MailMessage -To user@company.com -From "user@company.com" -SmtpServer smtp.domain.com -Subject "Ready"
}

Open in new window

which will also trigger if the target does not answer.
0
 
cwstad2Author Commented:
Hi guys, when its on one network it's ~ 800 ms and then on VPN  < 100.  Qlemo i changed it to less than 100 but it doesnt loop until this condition is met. can this contiunually loop until that condition is met?

thanks
0
 
QlemoC++ DeveloperCommented:
'f course it is, but you didn't say that yet ;-)
while (!($Ping = Test-Connection -Count 1 -TTL 2000 -EA SilentlyContinue Server)
   -or $Ping.ResponseTime -gt 100)
{
  Start-Sleep -seconds 60
}
Send-MailMessage -To user@company.com -From "user@company.com" -SmtpServer smtp.domain.com -Subject "Ready"

Open in new window

This will scan once every minute, until ping time is less or equal 100.
0
 
cwstad2Author Commented:
thats great much appreciated
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now