After a recent internet outage caused by the drive that hosted the PAC file becoming unavailable I need to monitor the availability of the PAC file. Using powershell on thre main polling engine I've written a script that checks availabilty of the PAC file. It works just fine there.
When I try to create a SAM monitor it fails at the testing phase and outputs the results that says there was no HTTP response. What's going on?
# Define the URL of the PAC file
$pacFileUrl = "http://redacted.local/pacfile.dat"
# Define a timeout for the web request (in milliseconds)
$timeout = 10000 # 10 seconds
# Try to check the PAC file's availability
try {
Write-Host "Checking availability of PAC file at $pacFileUrl..."
# Create a WebRequest object
$request = [System.Net.HttpWebRequest]::Create($pacFileUrl)
$request.Method = "HEAD"
$request.Timeout = $timeout
# Get the response
$response = $request.GetResponse()
# Cast the response to HttpWebResponse to access the StatusCode
$httpResponse = [System.Net.HttpWebResponse]$response
# Output the actual numeric status code
$statusCode = [int]$httpResponse.StatusCode
Write-Host "PAC file is available at $pacFileUrl (HTTP Status Code: $statusCode)" -ForegroundColor Green
#$statusCode = $response.StatusCode
$Up_Down= '0'
$Message = "PAC file wpad.dat Available: $StatusCode"
# Close the response
$httpResponse.Close()
} catch [System.Net.WebException] {
# Handle web exceptions (e.g., 404, 500 errors)
$errorResponse = $_.Exception.Response
if ($errorResponse -is [System.Net.HttpWebResponse]) {
$httpResponse = [System.Net.HttpWebResponse]$errorResponse
$statusCode = [int]$httpResponse.StatusCode
Write-Host "Error: Unable to access PAC file. HTTP Status Code: $statusCode" -ForegroundColor Red
#$statusCode = $httpresponse.StatusCode
$Up_Down = '1'
$Message = "PAC Unavailable: $StatusCode"
$httpResponse.Close()
} else {
Write-Host "Error: Unable to access PAC file. No HTTP response received." -ForegroundColor Red
$statusCode = $httpresponse.StatusCode
$Up_Down = '1'
$Message = "PAC Unavailable: $StatusCode"
}
} catch {
# Handle other exceptions
Write-Host "An unexpected error occurred: $_" -ForegroundColor Red
$statusCode = $httpresponse.StatusCode
$Up_Down = '1'
$Message = "PAC Unavailable: $StatusCode"
}
Write-Host "Statistic.Up_Down: $Up_Down";
Write-Host "Message.Up_Down: $URI $message";
Write-Host "Statistic.Status_Code: $statusCode";
Write-Host "Message.Status_Code: URL Status Code";
#Write-Host "Statistic.Response_Time: $ResponseTime";