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

PavCap Website Availability Report

" Foreach($Entry in $Result) { if($Entry.StatusCode -eq "200") { $Outputreport += "" $Outputreport += "" } elseif($Entry.StatusCode -eq "401") { $Outputreport += "" #" $Outputreport += "" #AddInfo = "Site is ok as it failed on Authentication" } else { $Outputreport += "" $Outputreport += "" #AddInfo = "red" } #$Outputreport += "" } $Outputreport += "
URLStatusCodeStatusDescriptionAdditional InfoResponseLengthTimeTaken
$($Entry.uri)$($Entry.StatusCode)$($Entry.StatusDescription)$($Entry.ResponseLength)$($Entry.timetaken)
$($Entry.AddInfo)
$($Entry.uri)$($Entry.StatusCode)$($Entry.StatusDescription)Website is up but it failed on Authentication!$($Entry.ResponseLength)$($Entry.timetaken)
$($Entry.uri)$($Entry.StatusCode)$($Entry.StatusDescription)Website is not up - please check!$($Entry.ResponseLength)$($Entry.timetaken)
$($Entry.uri)$($Entry.StatusCode)$($Entry.StatusDescription)$($Entry.AddInfo)$($Entry.ResponseLength)$($Entry.timetaken)
" } $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 = "PMR Website Availability Report

PavCap Instance Availability Report

" Foreach($Entry1 in $Result1) { if($Entry1.InsStatus -eq "Started") { $Outputreport += "" } else { $Outputreport += "" } $Outputreport += "" } $Outputreport += "
Instance NameInsStatus
$($Entry1.InsName)$($Entry1.InsStatus)
" $Outputreport +="

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.

" } $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"
powershell windows-task-scheduler