I have the following PowerShell Script, found and modified some time ago, however after checking and testing it in the past, I put this project away and wanted to check again now. However, the script should check a given website and tell how long it took to reach etc, but when I want to check with an internal website it is not working and constantly showing the cells with red color and error 200, however, external website like google or Facebook are measurable by the script.
Here is the script:
$URLListFile = "C:\Users\***\Desktop\websites.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
Foreach($Uri in $URLList) {
$time = try{
$request = $null
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliseconds
}
catch
{
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file C:\Users\***\Desktop\Test.htm
Invoke-Expression C:\Users\***\Desktop\Test.htm
Also attaching the picture about the result. Would you be able to help me why internal websites can not be measured, how should this script be modified so it can work? Website Availability Report
error
Okay, so adding this line solved my issue, as the webpage is not accepting SSL 1.0 but 1.2 only connection. [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
Related
I utilize a 51-line PowerShell script below working great when web sites use anonymous authentication but gives false result when they require authentication, even on single sign-on sites letting you right in, because the script logs the first HTTP 401 rather than re-trying a second time and logging the HTTP 200 result, because it simply moves on to next site in the list. Since every authentication-enabled web site always sends HTTP 401 first, I need the script to pause one second giving the client time to automatically grab credentials from the directory server and pass them back to the web site which will give an HTTP 200 and report out only the HTTP 200 result, and not the HTTP 401 result of the first try. Since my script below already has loop logic cycling through a URL list with the "foreach" command, I do not know how to do a nested loop re-trying the HTTP 401-responding sites a second time in order to try to get the HTTP correct 200 result to accurately represent the outcome of what actually happens when internal people access the single sign-on enabled sites.
## The URI list to test
$URLListFile = "C:\URLList.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
Foreach($Uri in $URLList) {
$time = try{
$request = $null
## Request the URI, and measure how long the response took.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliseconds
}
catch
{
<# If the request generated an exception (i.e.: 500 server
error or 404 not found), we can pull the status code from the
Exception.Response property #>
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
#Prepare email body in HTML format
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file C:\Scripts\Test.htm
Invoke-Expression C:\Scripts\Test.htm
I created the following script to check the website status and the IIS instance status. When I manually runs it from PowerShell ISE with Adminstrator, it works fine but when I triggered from Task Scheduler, it just didn’t worked! And it was due to GET-WEBSITESTATE as this cmdlet required to run from PS ISE Administrator.
My question, what is the command need to setup in Task Scheduler?
Currently, i setup the task schedule with the following:
Program/Script: Powershell.exe
Add arguments: powershell -executionpolicy bypass -file "D:\PS\CheckPMRWebpage\Code\Monitor PMR webpage 6-1.ps1
## The URI list to test
$URLListFile = "D:\PS\CheckPavcapWebpage\Code\PavCapurlList.DAT"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
Foreach($Uri in $URLList) {
$time = try{
$request = $null
## Request the URI, and measure how long the response took.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliseconds
}
catch
{
<# If the request generated an exception (i.e.: 500 server
error or 404 not found), we can pull the status code from the
Exception.Response property #>
$request = $_.Exception.Response
$time = -1
}
# $lStatusCode = [int] $request.StatusCode
# if ($lStatusCode -eq 401){
# $linfo = "The site is ok."
# }
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
##################################################
#Prepare body in HTML format for URL
##################################################
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>PMR Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> PavCap Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>Additional Info</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD></TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -eq "200")
{
$Outputreport += "<TR bgcolor=lightgreen>"
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center></TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
elseif($Entry.StatusCode -eq "401")
{
$Outputreport += "<TR bgcolor=yellow>" #<TD></TD><TD align=center></TD><TD align=center></TD><TD align=center>$($Entry.AddInfo)</TD><TD align=center></TD><TD align=center></TD></TR>"
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>Website is up but it failed on Authentication!</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
#AddInfo = "Site is ok as it failed on Authentication"
}
else
{
$Outputreport += "<TR bgcolor=red>"
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>Website is not up - please check!</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
#AddInfo = "red"
}
#$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.AddInfo)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file D:\PS\CheckPavcapWebpage\siteavailabilityrpt.html
#Invoke-Expression D:\PS\CheckWebpage_and_IIS\siteavailabilityrpt.html
######################################
# The Instances Name list to check
######################################
$InsNameFile = "D:\PS\CheckPavcapWebpage\Code\PavCapInsName.dat"
$InsList = Get-Content $InsNameFile -ErrorAction SilentlyContinue
$Result1 = #()
Foreach($Ins in $InsList){
$InsNameState = (Get-WebsiteState -Name $Ins).value
$result1 += [PSCustomObject] #{
#Time = Get-Date;
InsName = $ins;
InsStatus = $InsNameState;
}
}
##################################################
#Prepare body in HTML format for Instance
##################################################
if($result1 -ne $null)
{
$Outputreport = "<HTML><TITLE>PMR Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> PavCap Instance Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>Instance Name</B></TD><TD><B>InsStatus</B></TD></TR>"
Foreach($Entry1 in $Result1)
{
if($Entry1.InsStatus -eq "Started")
{
$Outputreport += "<TR bgcolor=lightgreen>"
}
else
{
$Outputreport += "<TR bgcolor=red>"
}
$Outputreport += "<TD align=center>$($Entry1.InsName)</TD><TD align=center>$($Entry1.InsStatus)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
$Outputreport +="<footer><p>This is a system generated email, please do not reply to this email, thank you. For time being, the email addresses are controlling from the script until solution found.</p></footer>"
}
$Outputreport | out-file D:\PS\CheckPavcapWebpage\siteavailabilityrpt.html -Append
$htmlfilename = "D:\PS\CheckPavcapWebpage\siteavailabilityrpt.html"
############################################################################
## Email to users (hardcoding email address on the top of scirpt - declare
###########################################################################
$htmlbodyreport = Get-Content "$htmlfilename" -Raw
Send-MailMessage -to $lrecipients -from $lsender -Subject $lemailsubject -BodyAsHtml -body $htmlbodyreport -SmtpServer "abc.bab.cbb.com"
I have a PS script which monitors all websites listed in text file. As of now i i am running it as a job so whenever any of website goes down it throws an alert on registered mail ID with the HTML file as an attachment consisting of all website status. What i want now that it only pick the those websites which goes down and throes me an alert. Can someone help me here please.
Script:-
$URLListFile = "C:\Users\Desktop\URL.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
Foreach($Uri in $URLList) {
$time = try{
$request = $null
## Request the URI, and measure how long the response took.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliseconds
}
catch
{
<# If the request generated an exception (i.e.: 500 server
error or 404 not found), we can pull the status code from the
Exception.Response property #>
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
TimeTaken = $time;
}
}
#Prepare email body in HTML format
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>TimeTaken</B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
send-mailmessage -to "abc#gmail.com" -from "abc#gmail.com" -subject "Test mail" -SmtpServer XXX####.com -Attachments "C:\Users\Desktop\Test.htm"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)<TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport | out-file C:\Users\Desktop\Test.htm
$Outputreport += "</Table></BODY></HTML>"
}
I can think of two ways to do this. First I assume you are trying to capture each item from $results where the status code is anything other than a success code. If this assumption is correct then you already have most of the solution in your code. The following line:
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
send-mailmessage -to "abc#gmail.com" -from "abc#gmail.com" -subject "Test mail" -SmtpServer XXX####.com -Attachments "C:\Users\Desktop\Test.htm"
}
is identifying those records, so you can simply grab them here in the loop. Like this:
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
send-mailmessage -to "abc#gmail.com" -from "abc#gmail.com" -subject "Test mail" -SmtpServer XXX####.com -Attachments "C:\Users\Desktop\Test.htm"
$failedCodeUri += $Entry.uri
}
In the above snippet the variable $failedCodeUri must be declared with the scope necessary to be accessible inside and outside of the foreach loop. This can be done as simply as declaring the variable on a line immediately preceding the beginning of the foreach loop. It also must be a type that can utilize the += operator. This can be done with a simple array like this.
$failedCodeUri = #()
Of course if you need to capture more than just the uri you will need a more sophisticated data type than a simple array of strings.
The other possible solution depends on the 'where' method being available on your $results object. If it is available you can simply do this:
$failedCodeUri = $results.where({$_.StatusCode -ne "200"})
This last technique would not be used inside the loop. Either before or after your foreach loop you can use this line of code to return all of the desired results.
A few things to note about this technique:
Within the 'where' code block the condition is being checked for each item in the result set. You reference the current item with $_ using the dot operator to access a property or method that exists for that item.
In the above example the entire item, with all of its properties will be returned. If you only want the uri you can do something like this:
$failedCodeUri = $results.where({$_.StatusCode -ne "200"}).uri
I have a script which reads a file of URL's and then performs a webrequest to check the status and latency of a list of site portals. We present this in a html table on an internal webserver. Our urls are not very meaningful to our business users I would like to create a new column in the table which shows the name of the site (http://10.x.x.x:8080/portal would be named 'Manchester')
Preferably reading in another file of names as each line of both files will match up
I have been doing this as a vanity project when I have a bit of free time in work but have limited knowledge.
'## The URI list to test
$URLListFile = "H:\Scripts\web.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
$a = Get-date
Foreach($Uri in $URLList) {
$time = try
{
$request = $null
## Request the URI, and measure how long the response took.
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliseconds
}
catch
{
<# If the request generated an exception (i.e.: 500 server
error or 404 not found), we can pull the status code from the
Exception.Response property #>
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
$SResult += [PSCustomObject] #{
Store = $Store;
}
}
#Prepare email body in HTML format
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Stores Web Portal Status</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Stores Web Portal Status $($a)</H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR bgcolor=green>"
}
if($Entry.timetaken -ge "2600.000")
{
$Outputreport += "<TR bgcolor=yellow>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file H:\Scripts\Test.htm'
I recommend changing the source file to a CSV format that stores both the URI and the Name side-by-side. In concept, it would look something like this:
$csvText = #"
Uri,SiteName
http://10.0.0.1/foo,Foo
http://10.0.0.2/bar,Bar
"#
$siteRecords = ConvertFrom-Csv $csvText
$siteRecords
In practice, you would use Import-Csv to get the data from your file, and then you would have access to both properties when iterating.
$siteRecords = Import-Csv -Path $pathToYourCsvFile
foreach ($record in $siteRecords)
{
$record.Uri
$record.SiteName
}
I have a lot of websites to monitor their up/down status, possible errors, ping and the other things that I managed to get with a script. My idea is the following: This script will run with task scheduler, get the results and send us (from the SQA publications) an email. So, I managed to create the script with success, he gets all I need and generates a html file in the C: drive. My problem is that after I get the result the function which sends the email isn't sending the email. I don't get any error messages, debug is fine, SMTP and all configurations are correct. But it won't send the email with the html file attached!
The code is this:
$URLListFile = "C:\URLList.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
Foreach($Uri in $URLList) {
$time = try{
$request = $null
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliSeconds
}
catch
{
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Website Report Status</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Report Status </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B> Code </B></TD><TD><B> Status </B></TD><TD><B> Duration </B></TD><TD><B> MS (Ping) </B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file C:\URLReport.htm
Invoke-Expression C:\URLReport.htm
$EmailFrom = "noreply#domain.com"
$EmailTo = "destinyemail#domain.com"
$EmailSubject = "URL Report"
$emailbody = " body message "
$SMTPServer = "smtpserver.company.com"
$emailattachment = "C:\URLReport.htm"
function send_email {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'html')
$mailmessage.Attachments.Add($attachment)
$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.Send($mailmessage)
}
EDIT4: > ($SmtpServer, 587) Beeing " 587 " the port that our smtp server uses.
Since you're using Powershell v3, you should be using Send-MailMessage instead of dealing with System.Net.
$URLListFile = "C:\URLList.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
$Result = #()
Foreach($Uri in $URLList) {
$time = try{
$request = $null
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
$result1.TotalMilliSeconds
}
catch
{
$request = $_.Exception.Response
$time = -1
}
$result += [PSCustomObject] #{
Time = Get-Date;
Uri = $uri;
StatusCode = [int] $request.StatusCode;
StatusDescription = $request.StatusDescription;
ResponseLength = $request.RawContentLength;
TimeTaken = $time;
}
}
if($result -ne $null)
{
$Outputreport = "<HTML><TITLE>Website Report Status</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Report Status </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B> Code </B></TD><TD><B> Status </B></TD><TD><B> Duration </B></TD><TD><B> MS (Ping) </B></TD</TR>"
Foreach($Entry in $Result)
{
if($Entry.StatusCode -ne "200")
{
$Outputreport += "<TR bgcolor=red>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file C:\URLReport.htm
Invoke-Item C:\URLReport.htm
$EmailFrom = "noreply#domain.com"
$EmailTo = "destinyemail#domain.com"
$EmailSubject = "URL Report"
$emailbody = " body message "
$SMTPServer = "smtpserver.company.com"
$emailattachment = "C:\URLReport.htm"
Send-MailMessage -Port 587 -SmtpServer $SMTPServer -From $EmailFrom -To $EmailTo -Attachments $emailattachment -Subject $EmailSubject -Body $emailbody -Bodyashtml;