Powershell get-websitestate cmdlet在任务计划程序中不起作用

时间:2019-01-08 04:02:18

标签: powershell taskscheduler

我创建了以下脚本来检查网站状态和IIS实例状态。当我使用Adminstrator从PowerShell ISE手动运行它时,它可以正常工作,但是当我从Task Scheduler触发时,它却无法正常工作!这是由于GET-WEBSITESTATE,因为此cmdlet需要从PS ISE管理员运行。 我的问题是,在任务计划程序中需要设置什么命令? 目前,我使用以下命令设置任务计划: 程序/脚本:Powershell.exe 添加参数:powershell -executionpolicy旁路-file“ D:\ PS \ CheckPMRWebpage \ Code \ Monitor PMR网页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"

0 个答案:

没有答案