Categories: Windows

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.

This post was last modified on March 20, 2016 12:24

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

2 months ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

3 months ago

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

7 months ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

12 months ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

1 year ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

1 year ago