PowerShell script to check the status of a URL

后端 未结 6 664
不知归路
不知归路 2020-12-04 18:36

Similar to this question here I am trying to monitor if a set of website links are up and running or not responding. I have found the same PowerShell script over the Interne

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 18:56

    You must update the Windows PowerShell to minimum of version 4.0 for the script below to work.

    
    [array]$SiteLinks = "http://mypage.global/Chemical/test.html"
    "http://maypage2:9080/portal/site/hotpot/test.json"
    
    
    foreach($url in $SiteLinks) {
    
       try {
          Write-host "Verifying $url" -ForegroundColor Yellow
          $checkConnection = Invoke-WebRequest -Uri $url
          if ($checkConnection.StatusCode -eq 200) {
             Write-Host "Connection Verified!" -ForegroundColor Green
          }
    
       } 
       catch [System.Net.WebException] {
          $exceptionMessage = $Error[0].Exception
          if ($exceptionMessage -match "503") {
             Write-Host "Server Unavaiable" -ForegroundColor Red
          }
          elseif ($exceptionMessage -match "404") {
             Write-Host "Page Not found" -ForegroundColor Red
          }
    
    
       }
    }
    
    

提交回复
热议问题