Facing new challenges everyday

Checking website status with Powershell

July 28th, 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
}
DZoneGoogle BookmarksFacebookEvernoteLinkedInDeliciousShare

2 Comments »

  1. Derek says

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

    May 23rd, 2013 | #

  2. rogerio says

    Yes, sure.

    May 27th, 2013 | #

Leave a comment

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad:
*

RSS feed for these comments. | TrackBack URI

Visitors Around the World

Polls

How Is My Site?

View Results

Loading ... Loading ...

Categories

Meta

Links

Advertising

hosted by easy2use.net