Friday, September 23, 2016

powershell: script to monitor multiple URLs passively being hosted in a restricted zone

The below scripts is used when you have a server which is on a restricted zone and you need to put passive monitoring for the URLs being hosted on those servers,

Since no inbound access is possible to that restricted zone servers and only outbound possible, the best possible solution I can think of is issuing  a https request to send data to our local server and then we can use that data to monitor the status of the URLs hosted at the restricted zone.

############################################################################# 
## 
## Website Availability Monitoring 
## Created by Ataul Haque
## Date : 23 Sep 2016 
## Version : 1.0 
## Email: ataulhaque@outlook.com  
############################################################################## 
 
 
## The URI list to test 
$hostname = $env:computername
$URLListFile = "C:\ataul\URLList.txt"  
$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 
  }   
  $result += [PSCustomObject] @{ 
  Time = Get-Date; 
  Uri = $uri; 
  StatusCode = [int] $request.StatusCode; 
  StatusDescription = $request.StatusDescription; 
  ResponseLength = $request.RawContentLength; 
  TimeTaken =  $time;  
  } 
 
} 
Foreach($Entry in $Result)
{
$Outputreport += "$($Entry.uri) | $($Entry.StatusCode) | $($Entry.StatusDescription) | $($Entry.ResponseLength) | $($Entry.timetaken) `r`n";
}

#echo "$outputreport";
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$wc = new-object System.Net.WebClient
Invoke-RestMethod -Method Post https://<hostname>/cgi-bin/dir-to-receive-data/$hostname-urlmonitor -Body $Outputreport


No comments:

Post a Comment