views:

458

answers:

2

Problem

Our web host provider is changing the IP address of one of the servers we are on. We have been given a time frame for when the switch will take place, but no exact details. Therefore, our current poor man's check requires a periodic page refresh on a browser to see if our website is still there.

Question

We are all programmers here and this is killing me that any manual checking is required. I would know how to do this in other languages, but want to know if there is a way to write a script in PowerShell to tackle this problem. Does anyone know how I might going about this?

A: 

This will list the IP Address for each network adapter in your system.

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property IPAddress
aphoria
+3  A: 

If you can alert if the page is gone or does not have an expected value, you could use a script like

$ip = 192.168.1.1
$webclient = new-object System.Net.WebClient
$regex = 'regular expression to match something on your page'
$ping = new-object System.Net.NetworkInformation.Ping


do 
{
  $result = $ping.Send($ip)
  if ($result.status -ne 'TimedOut' )
  {
    $page = $webclient.downloadstring("http://$ip")
    if (($page -notmatch $regex) -or ($page -match '404') -or ($page -eq $null))
    { break}
  }
} while ($true)

write-host "The website has moved"
Steven Murawski
Nice! I think we're close here. $ip needs to be a full http address (i.e. http://192.168.1.1). Also, this solution requires a web server to be in place at all times for the given $ip. So when the site is moved, as long as the web server is still running on that server, all should be good I think.
Scott Saad
You could wrap it all in a ping check too, to make sure the host is up before checking for the page. I'll add it to the response.
Steven Murawski