Checking website status with Powershell

Posted by – July 28, 2011

After few days searching about applications to help me on check is a site is up or not, I finally create my own solution to accomplish this, I wrote a small powershell script to do that.

With few lines of code I would create very quickly a script that just perform a http request and check the return code, if the return is 200, then website is ok, if not, a message will be displayed to the user.

The entire code can be found in below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function Show-MessageBox ($title, $msg) {    
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [Windows.Forms.MessageBox]::Show($msg, $title, [Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning, [System.Windows.Forms.MessageBoxDefaultButton]::Button1, [System.Windows.Forms.MessageBoxOptions]::DefaultDesktopOnly) | Out-Null    
}

[string] $url = 'http://www.saidosofa.com.br'

[net.httpWebRequest] $req = [net.webRequest]::create($url)
$req.Method = "HEAD"

[net.httpWebResponse] $res = $req.getResponse()

if ($res.StatusCode -ge "200") {
  write-host "`nSite up`n" `
    -foregroundcolor green
}
else {
  Show-MessageBox -title "Warning!" -msg "Site down!!!"
  write-host "`nSite down`n" `
    -foregroundcolor red
}
Share

6 Comments on Checking website status with Powershell

  1. Derek says:

    Can a port number be added to the URL? For example:
    [string] $url = ‘http://www.anywebsite.com:8080′

  2. Charlie says:

    Does this work for multiple sites?

  3. Thomas says:

    The script is accessing the site as a anonym user? How can I change that?

  4. rogerio says:

    You mean change it to do a basic or form auth?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>