The other week, I posted an example of how to get a website status using PowerShell. I have now extended this script to iterate through an XML file to get the statuses of all websites. You could even amend the script to run as a scheduled task and save the results to a log file of some description.
The XML file (SitesToCheck.ps1xml) looks as follows:
<Sites>
<Site SiteURL="http://www.google.com" UserName="" Password="" Domain=""/>
<Site SiteURL="http://www.webtechy.co.uk" UserName="" Password="" Domain=""/>
<Site SiteURL="http://www.microsoft.com" UserName="" Password="" Domain=""/>
<Site SiteURL="http://www.eden-apartments.co.uk" UserName="" Password="" Domain=""/>
</Sites>
And the PowerShell (GetWebStatuses.ps1) is as follows:
#############################################################
# Title: Get Web Statuses Script
# Version: 1.0
# Author: Ben Weeks
#############################################################
#############################################################
# Modules
#############################################################
# N/A
#############################################################
# Aliases
#############################################################
# N/A
#############################################################
# Global Variables
#############################################################
$global:cfgSitesFile = "SitesToCheck.ps1xml"
#############################################################
# Functions
#############################################################
function GetStatus([string]$SiteURL, [string]$Username, [string]$Password, [string]$Domain) {
try {
[System.Net.HttpWebRequest] $WebRequest = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($SiteURL)
if ($Username) {
# then a username exists in the config file
Write-Host "Using $UserName, $Password, $Domain in config file ..."
$WebRequest.Credentials = New-Object System.Net.NetworkCredential($Username, $Password, $Domain)
} else {
$WebRequest.Credentials = [System.Net.CredentialCache]::DefaultCredentials
}
[System.Net.HttpWebResponse] $WebResponse = [System.Net.HttpWebResponse] $WebRequest.GetResponse()
$HostName = $SiteURL -replace("http://","")
try {
$HostEntryDetails = [System.Net.Dns]::GetHostEntry($HostName)
foreach ($IP in $HostEntryDetails.AddressList)
{
Write-Host("IP = $IP");
}
} catch {
# Probably a machine name and no associated IP address.
}
$Server = $WebResponse.Server
$ResponseCode = $WebResponse.StatusCode
Write-Host "Response code for $SiteURL ($Server) is $ResponseCode" -ForegroundColor Green
} catch [Net.WebException] {
$ErrorMessage = $_.Exception.Message
Write-Host "The following error occurred: $ErrorMessage" -ForegroundColor Red
}
}
#############################################################
# Script body
#############################################################
$cfg = [xml] ( gc $global:cfgSitesFile )
$Sites = $cfg.Sites.Site
foreach ($Site in $Sites) {
try {
$SiteURL = $Site.SiteURL
$Username = $Site.Username
$Password = $Site.Password
$Domain = $Site.Domain
Write-Host "Checking $SiteURL ..."
GetStatus $SiteURL $Username $Password $Domain
} catch [Net.WebException] {
$ErrorMessage = $_.Exception.Message
Write-Host "The following error occurred: $ErrorMessage" -ForegroundColor Red
}
}
Also see attached files.
|
|
||||
|
This Month
Month Archive
About Me
Login
|
Using PowerShell to Get Multiple Website Statuses Iteratively
No comments found.
|
Search
Recent Articles
Favourite blogs
|
||
|
||||
