Solved

Script to check status of all websites on a webserver

Posted on 2011-09-15
11
327 Views
Last Modified: 2012-06-27
Hello,

I am looking for a script that will output (to a txt file) the HTTP status results of all websites hosted on a particular webserver. Is there an easy script that could be create during VB, Powershell or the like?
0
Comment
Question by:Charlie_Melega
  • 6
  • 5
11 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36545664
This is a great start:

http://dmitrysotnikov.wordpress.com/2008/05/29/monitor-web-site-availability/

Here's the code:

##########################################################
# Test-Site - script to test web site availability
# and notify in case of any issues
# (c) Dmitry Sotnikov
# http://dmitrysotnikov.wordpress.com
##########################################################

function Test-Site {
    param($URL)
    trap{
        "Failed. Details: $($_.Exception)"
        $emailFrom = "my.email@address.com"
        # Use commas for multiple addresses
        $emailTo = "my.email@address.com,another.admin@address.com"
        $subject = "$URL down"
        $body = "$URL site is down. Details: $($_.Exception)"
        $smtpServer = "192.168.1.1"
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($emailFrom, $emailTo, $subject, $body)
        exit 1
    }
    $webclient = New-Object Net.WebClient
    # The next 5 lines are required if your network has a proxy server
    $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    if($webclient.Proxy -ne $null)     {
        $webclient.Proxy.Credentials = `
                [System.Net.CredentialCache]::DefaultNetworkCredentials
    }
    # This is the main call
    $webclient.DownloadString($URL) | Out-Null
}

Test-Site "http://powergui.org"


You can also do a 5 minute interval test on the site with an email sending when they go down.  And you can test multiples:

For ($i;$i -lt 10;$i++){
Test-Site "http://powergui.org"
Test-Site "http://www.google.com"
Test-Site "www.thisisnotavalidsite.com"
Start-Sleep 300 #seconds
$i = 1
}

HTH,

Dale Harris
0
 

Author Comment

by:Charlie_Melega
ID: 36567735
Thanks for the post. I have been trying to edit this for my needs but as I am not a VB Script expert, it's been a bit tough. For my needs, I do not need the email action. I need to write each web site status output from that single web server to a text file.

Thanks for any additional thoughts.

Charlie
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36567818
Okay we can do that too.  Here's the way we would write it:

##########################################################
# Test-Site - script to test web site availability
# and notify in case of any issues
# (c) Dmitry Sotnikov
# http://dmitrysotnikov.wordpress.com
##########################################################

function Test-Site {
    param($URL)
        trap{
        $Success = $False
        "Failed. Details: $($_.Exception)"
        "$URL site is down. Details: $($_.Exception)" >> "ErrorLog.txt"
        exit 1
        }
    $webclient = New-Object Net.WebClient
    # The next 5 lines are required if your network has a proxy server
    $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    if($webclient.Proxy -ne $null)     {
        $webclient.Proxy.Credentials = `
                [System.Net.CredentialCache]::DefaultNetworkCredentials
    }
    # This is the main call
    $webclient.DownloadString($URL) | Out-Null
}

This basically just takes out the code for the email and appends it to a text file located in the same directory as you're in when you're running Powershell.  It will output nothing if it's up.

This is provided you actually have each URL for your web sites in your single web server.

And just so you're aware, this is written with Powershell, but it can be copy and pasted directly into your Powershell console and it will become a function that is in memory temporarily.

If you need more info on Powershell and where to start:
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/A_4327-PowerShell-Where-do-I-start.html

HTH,

Dale Harris
0
 

Author Comment

by:Charlie_Melega
ID: 36568593
Thanks very much Dale for the quick feedback. I copied the above into a .ps1 file and all seemed to work correctly but I did not receive any output. In reviewing the script, it doesn’t appear I need to provide any variable input and that "errorlog.txt" should have generated into the folder where I ran the powerShell script. Here is an image below with websites stopped. Are there any user required input to this script?:

 pshell test
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36568701
Once the function is created, you have to use a command to make it work:

Test-Site is the function.

#Example
Test-Site "http://powergui.org"

HTH,

Dale Harris
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Charlie_Melega
ID: 36569841
So does the powerShell script create the “Test=Site” function. If so, I don’t see evidence of this.  Apologies but I am not quite sure why I can't run this as a PowerShell script as shown in the graphic.   After setting the correct execution policy, the script appears to process. I've run PowerShell scripts before using the syntax shown so I assume this script cannot be run as a simple .ps1 file?
0
 
LVL 16

Accepted Solution

by:
Dale Harris earned 500 total points
ID: 36569915
You can do the following:

#Script to be saved as .ps1 follows
##########################################################
# Test-Site - script to test web site availability
# and notify in case of any issues
# (c) Dmitry Sotnikov
# http://dmitrysotnikov.wordpress.com
##########################################################

function Test-Site {
    param($URL)
        trap{
        $Success = $False
        "Failed. Details: $($_.Exception)"
        "$URL site is down. Details: $($_.Exception)" >> "ErrorLog.txt"
        exit 1
        }
    $webclient = New-Object Net.WebClient
    # The next 5 lines are required if your network has a proxy server
    $webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    if($webclient.Proxy -ne $null)     {
        $webclient.Proxy.Credentials = `
                [System.Net.CredentialCache]::DefaultNetworkCredentials
    }
    # This is the main call
    $webclient.DownloadString($URL) | Out-Null
}

For ($i;$i -lt 10;$i++){
Test-Site "http://powergui.org"
Test-Site "http://www.google.com"
Test-Site "www.thisisnotavalidsite.com"
Start-Sleep 300 #seconds
$i = 1
}

########################
#End Script

This will loop over and over again every 5 minutes and append to the text file created in your directory you're running the script from (C:\Scripts)

HTH,

Dale Harris
0
 

Author Comment

by:Charlie_Melega
ID: 36570225
Excellent,  I assume if I remove the 2 lines.....

Start-Sleep 300 #seconds
$i = 1

.....this will eliminate the 5 minute pause on this task?  

And lastly, I noticed that the script is designed to check specific web sites. Is it possible for the script to "dynamically"  check for the status of all websites on that web server? Essentially, there would be no need to specify any particular web site as the script would check status of all by default. I have been looking at some the links you provided trying to edit the script to support this.

Thanks again for the tremendous help.
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 36570490
To answer your question, yes, if you take that out, there's no delay.

If you want to go the route of getting all website dynamically, there's a Microsoft IIS 7 Snapin to run for Powershell:
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=7436

I haven't tried it myself.

HTH,

Dale Harris
0
 

Author Closing Comment

by:Charlie_Melega
ID: 36573470
Excellent assistance, Thank You Dale.
0
 

Author Comment

by:Charlie_Melega
ID: 36717301
I do have a follow up to this item. Everything is fully functional an integrated but the one issue we are running into is that the output file (errorlog.txt) is actually creating a .txt file which is using UTF encoding. I have a separate function that tries to read this file but fails as it is not in ASCII encoding format. Is there any edit that can be made to the line in this Powershell script;

 $URL site is down. Details: $($_.Exception)" >> "ErrorLog.txt"

so that the errorlog.txt file is created in ASCII format?


Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now