Scroll Top
Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

Quickly test multiple proxies with Powershell

Recently we were tasked with testing connectivity with Good For Enterprise servers. Since the environment is highly secure it requires proxy to go out. As we had to test connectivity on over 40 servers for multiple proxies and multiple URL's we had to find a simple, yet efficient way to do this. After some quick scripting following script has shown to be very useful in this task.
# Proxy servers in format: https://<url>:<port>. Can be multiple Proxy servers used by comma separation

$proxy_servers = "https://surf.proxy:8080";# , "https://server1.testproxy:8080";

# URL's to be tested thru Proxy.

$urls = "https://xml28.good.com";, "https://xml29.good.com";  #,"https://xml30.good.com"; ,"https://gti01.good.com","https://www.good.com", "https://upl01.good.com", "https://ws.good.com";


# ComputerName of tested machine
Write-Host "Testing script on machine: " $env:COMPUTERNAME

function DoWork {   
    # Test URL's without PROXY first

    foreach ($url in $urls) {

        TestWithoutProxy -URL $url

    }

    # Test URL's with Proxy 

    foreach ($proxy_serv in $proxy_servers) {

        foreach ($url in $urls) {

            TestProxy -ProxyServer $proxy_serv -URL $url

        }

    }

}

function TestWithoutProxy {

    param (

        [Parameter(Mandatory=$true,

            ValueFromPipeline=$true,

            ValueFromPipelineByPropertyName=$true,

            Position=0)]

        [Alias("U")] [string]$url

        )

        $WebClientWithoutProxy = New-Object System.Net.WebClient

        Write-Host -NoNewLine "Testing without proxy server " -ForegroundColor Yellow

        Write-Host -NoNewLine " [*] URL test without Proxy -" $url -ForegroundColor White 

        Try { 

            $contentWithouProxy = $WebClientWithoutProxy.DownloadString($url)

            Write-Host -NoNewline " [+] Opened $url successfully without Proxy"

        }catch {

            Write-Host -NoNewLine " [-] Unable to access $url without Proxy" -ForegroundColor Red 

        }

        Write-Host " [*]"

}

function TestProxy {

    param (

        [Parameter(Mandatory=$true,

            ValueFromPipeline=$true,

            ValueFromPipelineByPropertyName=$true,

            Position=0)]

        [Alias("CN")][string[]]$ProxyServer, [Parameter(Position=1)]

        [Alias("U")] [string]$url

        )

    $proxy = new-object System.Net.WebProxy($ProxyServer)
    $WebClient = new-object System.Net.WebClient
    $WebClient.proxy = $proxy

    #Write-Host ""
    Write-Host -NoNewLine "Testing proxy server: " $ProxyServer -ForegroundColor Yellow 
        Write-Host -NoNewline " [*] URL test -" $url -ForeGroundColor White

        Try {

           $content = $WebClient.DownloadString($url)

           Write-Host -NoNewLine " [+] Opened $url successfully" -ForegroundColor Green "- Amount of chars found on website" $content.Length

        } catch {

           Write-Host -NoNewLine  " [-] Unable to access $url" -ForegroundColor Red 

        } 
    Write-Host " [*]"

}

DoWork 

Running it should give you a nice, quick overview of a test. Proxy Test Script Output

Posty powiązane

Zostaw komentarz

You must be logged in to post a comment.