Powershell script to replace the Z-WebLoad tab of the analyzer app

We do not have java widely deployed in our environment. With the z-analyzer app dependent on java it made thing a bit difficult. Wrote a PowerShell script that does everything the WebLoad tab does. Figured someone else out there may find it useful.

<#
.SYNOPSIS
Measurs how long it takes to load a web site or sites and detects if the client is behind a Zscaler cloud security gateway.
.DESCRIPTION
Creates two functions (Get-ZscalerGWInfo aka get Zscaler Gateway Information and get-websiteloadtimes).
Get-ZscalerGWInfo parses the site http://ip.zscaler.com for the nodename, datacenter, and clientip information.
Get-WebsiteLoadTimes measures how long the invoke-WebRequest function takes to load a web site. 
.LINK
Thanks to port135 for the code that was used to create the Get-WebsiteLoadTimes function.
https://port135.com/2019/09/11/how-to-measure-website-speed-in-your-internal-network/
.PARAMETER Site
Input must be in the form of an IP address or FQDN. Should be compatible with most TLDs. If not specified the defaults will be used.
.PARAMETER PageLoadAttempts
Specifies how may times to try and load the page. Default is 5.
.PARAMETER OutputToFile
Default is to display on screen. Will write out a filename if one is given in this parameter.
.INPUTS
System.String, System.Int32
.OUTPUTS
PSObjects containing the results. Option to lso saves a file to the working directory the script is run from.
.EXAMPLE
PS C:\> Get-PageLoadTimes.ps1 
Detects if the client is behind a Zscalers secure gateway and if it is displays the info about it.
Tests loading Wikipedia, Facebook, Amazon, Instagram, and Intuit 5 times and displays the results.
.EXAMPLE
PS C:\> Get-PageLoadTimes -s amazon.com -a 10 -o amazon.txt
Detects if the client is behind a Zscalers secure gateway and if it is displays the info about it.
Tests loading Amazon's web site 10 times and outputs the result to the file named amazon.txt.
#>

#Requires -version 4
[CmdletBinding()]
  param(
    [Parameter(ValueFromPipeline)]
    [Alias("s")]
    [Array]$Site = ('http://en.wikipedia.org/', 'https://facebook.com',  'https://amazon.com', 'https://instagram.com', 'http://intuit.com'),

    [Parameter(ValueFromPipeline)]
    [Alias("a")]
    [ValidateRange(1,1000)]
    [int]$PageLoadAttempts = 5, #Default to 5 attempts with a min of 1 and max of 1000
    
    [Parameter(ValueFromPipeline)]
    [Alias("o")]
    [string]$OutputToFile = "" #If specified will right the output to a file of the given name in the working directory the script was run from.
)

# Establish global variables.
$Global:WebsiteLoadTimeResults = New-Object Collections.Generic.List[String]
$Global:ZSResults = New-Object Collections.Generic.List[String]

Function script:Get-ZscalerGWInfo {
    $ZSIP = Invoke-WebRequest -Uri http://ip.zscaler.com
    $ZSIP = $ZSIP.InputFields.value -split ":"
    $ZSIP = $ZSIP -split ","
    $ZSIP = $ZSIP.trim('"')
    $ZSDatacenter = $ZSIP[9]
    $ZSNodeName = $ZSIP[5]
    $ZSClientIP = $ZSIP[13]
    $ZSClientIP = $ZSClientIP -replace ('"',"")
    $ZSClientIP = $ZSClientIP -replace ("}","")
    $Global:ZSResults.Add("Client is using the $ZSDatacenter Zscaler datacenter.")
    $Global:ZSResults.Add("Client is using $ZSNodeName as their Zscaler node.")
    $Global:ZSResults.Add("Zscaler is seeing the clients IP address as $ZSClientIP.")
} #End Get-ZscalerGWInfo

Function script:Get-WebsiteLoadTimes {
    ForEach ($url in $Site) {
        $totalSeconds=0
        $individualRequests = ""
 
        For ($i=0; $i -lt $PageLoadAttempts; $i++)  {
 
            $timeTaken = Measure-Command -Expression {
                $site = Invoke-WebRequest -Uri $url
            }
 
            $roundedSecond = [Math]::Round($timeTaken.TotalSeconds, 4)
            $totalSeconds += $roundedSecond
            $individualRequests += $roundedSecond.toString() +  ", "
 
            Start-Sleep -m 200
        }
 
        $averageSeconds = $totalSeconds / $PageLoadAttempts
        $Global:WebsiteLoadTimeResults.Add("For Site $url")
        $Global:WebsiteLoadTimeResults.Add("Average of $totalRequest requests is $averageSeconds")
        $Global:WebsiteLoadTimeResults.Add("Individual requests: $individualRequests")
        $Global:WebsiteLoadTimeResults.Add("")
    }
} #End Get-WebsiteLoadTimes


Get-WebsiteLoadTimes
Get-ZscalerGWInfo

If ($OutputToFile -eq "") {
    Write-Output $Global:ZSResults
    Write-Output ""
    Write-Output $Global:WebsiteLoadTimeResults
}
Else {
    $Global:ZSResults | Out-File -FilePath .\$OutputToFile 
    "" | Out-File -FilePath .\bob.txt -Append
    $Global:WebsiteLoadTimeResults | Out-File -FilePath .\$OutputToFile -Append
}
3 Likes

Thanks for this Bob! I’m sure this will help a lot of people.