Thursday, September 22, 2016

powershell: Script to monitor URL status and send the data using Https or 443 port

powershell Script to monitor any URL status and send the data using Https or 443 port to any remote server.

If suppose you have a server in some restricted zone and a particular link is available to that zone only and you need to send the URL status to your local server which is outside the restricted zone and only port available to communicate your local server is port no. 443.
Then you can use this script for monitoring purpose.


Pre-requisites:
Powershell v3 or v4.

#########################################################
#Ataul Haque 22nd Sep 2016

$hostname = $env:computername
function CheckSiteStatus([string]$url)
{
    $req = [System.Net.HttpWebRequest]::Create($url)
    try
    {
        $res = $req.GetResponse()
        $res.StatusCode
    }
    catch
    {
        "Failed"
    }
}

$status = CheckSiteStatus("http://www.indoscopy.com/");
#echo "$status";
$final_status = "Url Status | $status";
#echo "$final_status";

 if($status -ne "OK")
    {
        [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
        $wc = new-object System.Net.WebClient
        Invoke-RestMethod -Method Post https://<domain-name>/cgi-bin/<dir-receiving-data>/$hostname -Body $final_status
    }
    else
    {
         [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
        $wc = new-object System.Net.WebClient
        Invoke-RestMethod -Method Post https://<domain-name>/cgi-bin/<dir-receiving-data>/$hostname -Body $final_status
    }

No comments:

Post a Comment