Jump to content

Recommended Posts

Posted

I've got a script which is testing a connection to a URL from a server that has a direct unfiltered (no proxy) web connection - however I'm trying to test the connection to the URL via a proxy.

 

I've got the powershell script working nicely, and if the server can't connect to the URL when the powershell runs it fires off an email.

 

The issue I'm having is that I need to be able to set a proxy in powershell, in which the connection to the URL will fail if the proxy isn't available.

 

I've tried setting my proxy using

netsh winhttp set proxy PROXYSERVER:8080

 

and when I run

netsh winhttp show proxy

 

It shows the proxy as being set correctly. However when I run my script and the proxy isn't available, powershell still connects to the URL directly.

 

 

 

The script I'm running is pretty simple:

"Test Connection via Proxy"
#If connection to domain.com succeeds
if(Test-Connection -Cn domain.com -BufferSize 16 -Count 4 -ea 0 -quiet)

#Then exit
{“PROXY IS UP - NO MESSAGE SENT”}

#Else send the email
ELSE {Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "CHECK PROXY" -SmtpServer smtp.domain.com}

 

 

But here's what I want to achieve with it but have had no success so far (the email sends whether or not the proxy server is actually there):

SET PROXY

"Test Connection via Proxy"
#If connection to domain.com succeeds
if(Test-Connection -Cn domain.com -BufferSize 16 -Count 4 -ea 0 -quiet)

#Then exit
{“PROXY IS UP - NO MESSAGE SENT”} & REMOVE PROXY

#Else send the email
ELSE {REMOVE PROXY & Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "CHECK PROXY" -SmtpServer smtp.domain.com}

 

 

Hope that makes sense. If anyone has a quick fix to get this to work it'd be great!

 

Cheers

Posted

So further research and testing shows that powershell pulls proxy settings from IE on launch and sticks with these.

https://sites.google.com/site/softwaretechforge/Downhome/Programminglanguage/sethttpproxydoesntworkinpowershell

 

A quick way to get around this would be to change the IE proxy settings via batch/ps script first and then launching the powershell script afterwards, however test-connection and ping still don't go via the proxy. Is it possible to run test-connection or ping via proxy if you have direct access to the web? Unfortunately my Google powers are failing me tonight

Posted (edited)

Running update-help in PS when proxy is set in IE but unavailable, results in the command timing out. The command works fine directly when the proxy isn't available which must mean the proxy only applies to HTTP requests.

 

For some reason I was of the impression the test-connection command worked differently to a ping command but it doesn't seem to be the case unfortunately. https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/test-connection

Edited by gtg93
Posted

I've made a few changes after reading a blog about someone trying to achieve the same thing (wish I'd saved the link!), however I'm unable to get it working as I'd expect.

 

The script is:

 

#If connection to censornet succeeds (change proxy name from PROXYSERVER below if needed)
if(Test-Connection -Cn PROXYSERVER -BufferSize 16 -Count 4 -ea 0 -quiet)

#Then run connection test via PROXYSERVER
{
#Set proxy and download URL (change proxy details in first line if needed. URL can also be changed if needed.)
$proxy = new-object System.Net.WebProxy("PROXYSERVER:8080")
$WebClient = new-object System.Net.WebClient
$WebClient.proxy = $proxy
$url = "http://www.microsoft.com"
$content = $WebClient.DownloadString($url)

#If download URL connection is OK, exit.
if ($content -ne 0){“PROXY IS UP - NO MESSAGE SENT”}

#Else, email with error (Change to and from if necessary, as well as site name and SMTP server)
else {Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "CHECK PROXY" -SmtpServer smtp.domain.com}
}

#Else send the email (Change to and from if necessary, as well as site name and SMTP server)
ELSE {Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "CHECK PROXY" -SmtpServer smtp.domain.com}

 

The aim this time is to ping the proxy server, if it doesn't respond to send an email. If it does respond, the aim is to connect to a http URL, then again if no error is returned do nothing, if an error is returned to again send an email.

 

The above is pretty much what I found someone on a blog using, and they claimed they'd had it working - people commenting saying it was working etc... The only changes I've really made is added the else commend commands to be an email rather than creating a log and have removed authentication for the proxy.

 

 

When the script runs on Microsoft.com, it runs as I'd expect returning the "PROXY IS UP - NO MESSAGE SENT" on screen, however if I try it against a random URL (simulating a failed connection) it returns a 503 error within the powershell windows and then again displays the "PROXY IS UP - NO MESSAGE SENT" message.

 

Any thought's on what needs to change? The idea is if any error is returned when connecting to the URL, it'll ping an email off rather than just display the error on screen.

 

Cheers

Posted (edited)
Any thought's on what needs to change?

Does this work for you?

 

$SmtpSettings = @{
   To         = "[email protected]"
   From       = "[email protected]"
   Priority   = "High"
   Subject    = "Check Proxy!"
   SmtpServer = "smtp.example.com"
}

if (Test-Connection 192.168.0.1 -BufferSize 16 -Count 4 -EA 0 -Quiet) {

   $StatusCode = Invoke-WebRequest "https://www.microsoft.com" | Select -ExpandProperty StatusCode

   If ($StatusCode -eq 200) {
       Write-Output "Site is accessible"
   } Else {
       Write-Output "The site may be down"
       Send-MailMessage @SmtpSettings -Body "Cannot access microsoft.com" -ErrorAction Stop
   }

} Else {

   try {
       Write-Output "The proxy may be down"
       Send-MailMessage @SmtpSettings -Body "Cannot ping proxy" -ErrorAction Stop
   } catch {
       Write-Warning $_.Exception.Message
   }

}

Edited by Arthur
  • Thanks 1
Posted

Thanks!

 

I have had to add

 -proxy "http://censornet:8080" 

to the Invoke-WebRequest to put the command via the proxy (the server the script runs from has a direct connection to the web).

 

I'm unable to simulate any real issues with the proxy during the day, however trying to connect to a page that doesn't exist gets me a 404 error (as you'd expect) and no email. This isn't an issue if the script will work as wanted when the proxy is having issues however so I'll try after school today.

 

Thanks again!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...