Proxy Server Checker v1.0

This PowerShell script runs a basic test of functionality for a proxy server. It sets up a web request via a specified proxy server address, then retrieves the HTTP Response Code. If the code is returned as HTTP 200 OK, it’ll add 200 to the final status code. At the end, it’ll compare how many sites it was passed to how many OK responses it received to determine if the proxy server is functioning correctly.

I wrote this script to work with our monitoring system so that we could test the functionality of a proxy server. Passing it three sites to test and having it report OK if it received a 600 OK, WARN if 200 or 400, or go CRITICAL if 0. This proved to be a more reliable test than just a connectivity test, as the proxy server may be contactable, but not actually processing requests. This test proves that it is processing requests.

This script is compatible with Powershell 2 and above. I had originally written the script using some cleaner cmdlets, but the server we needed to run it on only had Powershell 2 and upgrading for this script was going to be more hassle than it was worth due to the server needing to be available at all times.

Usage: .\ProxyServerCheck.ps1

Set your proxy server in the $proxySvr variable on line 46, the URLs to check in the $urls variable on line 40 & log file on line 32.

Download Proxy Server Checker v1.0

#################################################################################################
#                                                                                               #
#   Proxy Server Checker v1.0                                                                   #
#                                                                                               #
#   Written by: Mike Oldfield                                                                   #
#   Date: 30/08/2016                                                                            #
#                                                                                               #
#   This PS1 is designed for Uptime Infrastructure Monitor.                                     #
#   The script forcibly sets the proxy server to a server of your choice, then passes a number  #
#   of web requests to the proxy server to test connectivity. If an HTTP 200 OK is received,    #
#   it'll add that number to the OK status. At the end, it'll total up the number expected and  #
#   the number received, then report on whether we were OK or not.                              #
#                                                                                               #
#   Usage: See Help-File function, or run script with no parameters.                            #
#                                                                                               #
#################################################################################################

#############################
#                           #
#         VARIABLES         #
#                           #
#############################

### SCRIPT VARIABLES - These shouldn't need changing

$scriptVer = "v1.0"     # What is this version number?
$host.privatedata.ProgressForegroundColor = "white"     # Set the foreground colour for progress bars
$host.privatedata.ProgressBackgroundColor = "darkgray"  # Set the background colour for progress bars

$date = Get-Date -format d          # Set the current date
$datetime = Get-Date -format g      # Set the current date w/ time
$logfile = "D:\Scripts\Uptime Logs\dproxy-uptime-$($date -replace "/", "-").txt"    # Set the log file path w/date. Replace any / with a - (eg, 28/08/2016 becomes 28-08-2016)

$statusCode = 0     # Set the current status code to 0. We haven't checked anything yet
$urlCount = 0       # Set the current URL number to 0. We haven't checked anything yet

### PROXY CHECK VARIABLES - Change these according to your check

# Set the URLs we want to check
$urls = "http://www.google.com", "http://www.bbc.co.uk", "http://www.microsoft.com"

# Set the expected status code. An OK response is 200, so we want to multiply the number of URLs by 200.
$expStatusCode = $urls.count * 200

# Set the proxy server we want to use
$proxySvr = "DPROXY"

#############################
#                           #
#        SCRIPTY BIT        #
#                           #
#############################

# Setup the logging function
function log($string)
{   
    # Add line $string to the specified log file
    $string | out-file -Filepath $logfile -append
}

# Create a new WebProxy object
$proxy = New-Object System.Net.WebProxy
# Set the proxy address to DPROXY port 8080
$proxy.Address = "http://$($proxySvr):8080"
# Tell the proxy to use default credentials
$proxy.UseDefaultCredentials = $true

# Now we actually do our checks
# For each URL listed above, do some checks
foreach ( $url in $urls ) {

    try {
        
        $urlCount++
        
        Write-Progress -Activity "Proxy Server Checker $scriptVer" -Status "Testing website $urlCount of $($urls.count) via $proxySvr" -CurrentOperation "$url" -PercentComplete (($urlCount / $urls.count) * 100)
        
        # Setup the web request
        $WebRequest = [System.Net.WebRequest]::Create($url)
        # Tell the WebRequest to use our proxy server
        $WebRequest.Proxy = $proxy
        # Go and get the response
        $WebResponse = $WebRequest.GetResponse()
        # Add the HTTP status code returned to the overall status code
        $statusCode += [int]$WebResponse.StatusCode
        # Kill off the WebRequest so that max 2 connections doesn't get breached
        $WebRequest.Abort()
        
    # If we get any errors...   
    } catch [System.Exception] {
    
        # Write the error to the log file
        log("$datetime Error getting $($url): $_`r`n-----------------------------------------------`r`n")
        
    }
    
}

# Check if the status code matches the expected status code
if ( $statusCode -eq $expStatusCode ) {
    
    # Tell us everything is OK
    Write-Host "Status Code: $statusCode OK"

# If the status code wasn't expected, check if it's 0 (eg, nothing worked)
} elseif ( $statusCode -eq 0 ) {

    # Tell us everything is not OK
    Write-Host "Status Code: $statusCode NOT OK"

# If the status code wasn't expected, but wasn't 0 (eg, some worked but some didn't)
} else {

    # Tell us we have a warning. This might be OK, just a site genuinely down.
    Write-Host "Status Code: $statusCode WARNING"

}

%d bloggers like this: