Powershell get-websitestate cmdlet not working at Task Scheduler
22:39 07 Jan 2019
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 = "PMR Website Availability Report
"
$Outputreport +=""
}
$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"
powershellwindows-task-scheduler
Your Answer
lock_outline
You need to log in to submit an answer.
Privacy & Cookie Consent
We use cookies to ensure the best experience on our website. This includes analytics, personalization, and marketing purposes. Some cookies are essential for the website to function properly.
By clicking "Accept", you consent to our use of cookies. You can read more about how we use cookies and how you can change your preferences in our Privacy Policy.