Powershell Script to monitoring status and send email results

前端 未结 1 551
时光取名叫无心
时光取名叫无心 2021-01-14 09:44

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 scri

1条回答
  •  爱一瞬间的悲伤
    2021-01-14 10:35

    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 = "Website Report Status

    Website Report Status

    " } else { $Outputreport += "" } $Outputreport += "" } $Outputreport += "
    URL Code Status Duration MS (Ping) " Foreach($Entry in $Result) { if($Entry.StatusCode -ne "200") { $Outputreport += "
    $($Entry.uri)$($Entry.StatusCode)$($Entry.StatusDescription)$($Entry.ResponseLength)$($Entry.timetaken)
    " } $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;

    0 讨论(0)
提交回复
热议问题