Monitoring with PowerShell: Monitoring internet speeds

It seems like I’m having a week of requests. This one was requested by my friends at Datto. One of their clients wanted to have the ability to run speed-tests and have their RMM system generate alerts whenever the speed drops. I’ve made the following PowerShell script that uses the CLI utility from speedtest.net. This utility gives us some nice feedback to work with;

  • It returns the external IP & the internal IP for the interface used.
  • The current ISP.
  • The download and upload speed.
  • The Jitter,Latency, and packet loss of the connection.
  • and the server it uses, plus the actual speedtest.net URL so you can compare the results.

So, I’ve made the script use two different monitoring methods; one is absolute and based and the values you’ve entered. The other is a percentage based monitor that alerts if the difference between the current speedtest and the previous one is more than 20%.

The script

The script downloads the Speedtest utility from the speedtest website. You can always replace this URL with your own host if you’d like.

######### Absolute monitoring values ########## 
$maxpacketloss = 2 #how much % packetloss until we alert. 
$MinimumDownloadSpeed = 100 #What is the minimum expected download speed in Mbit/ps
$MinimumUploadSpeed = 20 #What is the minimum expected upload speed in Mbit/ps
######### End absolute monitoring values ######

#Replace the Download URL to where you've uploaded the ZIP file yourself. We will only download this file once. 
#Latest version can be found at: https://www.speedtest.net/nl/apps/cli
$DownloadURL = "https://bintray.com/ookla/download/download_file?file_path=ookla-speedtest-1.0.0-win64.zip"
$DownloadLocation = "$($Env:ProgramData)\SpeedtestCLI"
try {
    $TestDownloadLocation = Test-Path $DownloadLocation
    if (!$TestDownloadLocation) {
        new-item $DownloadLocation -ItemType Directory -force
        Invoke-WebRequest -Uri $DownloadURL -OutFile "$($DownloadLocation)\speedtest.zip"
        Expand-Archive "$($DownloadLocation)\speedtest.zip" -DestinationPath $DownloadLocation -Force
    } 
}
catch {  
    write-host "The download and extraction of SpeedtestCLI failed. Error: $($_.Exception.Message)"
    exit 1
}
$PreviousResults = if (test-path "$($DownloadLocation)\LastResults.txt") { get-content "$($DownloadLocation)\LastResults.txt" | ConvertFrom-Json }
$SpeedtestResults = & "$($DownloadLocation)\speedtest.exe" --format=json --accept-license --accept-gdpr
$SpeedtestResults | Out-File "$($DownloadLocation)\LastResults.txt" -Force
$SpeedtestResults = $SpeedtestResults | ConvertFrom-Json

#creating object
[PSCustomObject]$SpeedtestObj = @{
    downloadspeed = [math]::Round($SpeedtestResults.download.bandwidth / 1000000 * 8, 2)
    uploadspeed   = [math]::Round($SpeedtestResults.upload.bandwidth / 1000000 * 8, 2)
    packetloss    = [math]::Round($SpeedtestResults.packetLoss)
    isp           = $SpeedtestResults.isp
    ExternalIP    = $SpeedtestResults.interface.externalIp
    InternalIP    = $SpeedtestResults.interface.internalIp
    UsedServer    = $SpeedtestResults.server.host
    ResultsURL    = $SpeedtestResults.result.url
    Jitter        = [math]::Round($SpeedtestResults.ping.jitter)
    Latency       = [math]::Round($SpeedtestResults.ping.latency)
}
$SpeedtestHealth = @()
#Comparing against previous result. Alerting is download or upload differs more than 20%.
if ($PreviousResults) {
    if ($PreviousResults.download.bandwidth / $SpeedtestResults.download.bandwidth * 100 -le 80) { $SpeedtestHealth += "Download speed difference is more than 20%" }
    if ($PreviousResults.upload.bandwidth / $SpeedtestResults.upload.bandwidth * 100 -le 80) { $SpeedtestHealth += "Upload speed difference is more than 20%" }
}

#Comparing against preset variables.
if ($SpeedtestObj.downloadspeed -lt $MinimumDownloadSpeed) { $SpeedtestHealth += "Download speed is lower than $MinimumDownloadSpeed Mbit/ps" }
if ($SpeedtestObj.uploadspeed -lt $MinimumUploadSpeed) { $SpeedtestHealth += "Upload speed is lower than $MinimumUploadSpeed Mbit/ps" }
if ($SpeedtestObj.packetloss -gt $MaxPacketLoss) { $SpeedtestHealth += "Packetloss is higher than $maxpacketloss%" }

if (!$SpeedtestHealth) {
    $SpeedtestHealth = "Healthy"
}

And that is it! So this monitoring component will be uploaded to the ComStore soon. As always, Happy PowerShelling!

22 thoughts on “Monitoring with PowerShell: Monitoring internet speeds

  1. Justin M Monk

    At line:24 char:66
    + … ts = & “$($DownloadLocation)\speedtest.exe” –format=json –accep …
    + ~~~~~~~~~~~
    Unexpected token ‘format=json’ in expression or statement.
    At line:24 char:27
    + … SpeedtestResults = & “$($DownloadLocation)\speedtest.exe” –forma …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The ‘–‘ operator works only on variables or on properties.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

    Reply
  2. Jeremiah

    So i’m getting

    + … ts = & “$($DownloadLocation)\speedtest.exe” –format=json –accep …
    + ~~~~~~~~~~~
    Unexpected token ‘format=json’ in expression or statement.

    and

    + … SpeedtestResults = & “$($DownloadLocation)\speedtest.exe” –forma …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The ‘–‘ operator works only on variables or on properties.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

    What am I missing?

    Reply
  3. Alberto Laudadio

    Hi Kelvin
    I am new with scripts in Powershell and I was trying your script, but it failed.
    I suppose that the & text in the line 24 is a typo and I deleted.
    Then I got an error in the same line: The ‘–‘ operator works only on variables or on properties.
    So I change de ” to the end of the line.
    But then the two “ConvertFrom-Json” commands failed: “Invalid JSON primitive: C.”
    Thanks in advance

    Reply
    1. Kelvin Tegelaar Post author

      Hi Tony, The post above has been updated. The previous issue was that wordpress changed line 24. Line 24 should be “$SpeedtestResults = & “$($DownloadLocation)\speedtest.exe” –format=json –accept-license –accept-gdpr”. it seems OK to me now, are you having issues?

      Reply
  4. DerekinCA

    Hello, this looks great but I am receiving this error when I try to run this script. Can you tell what might be causing this? Thank you.

    speedtest.exe : [2020-02-26 16:17:43.827] [error] Configuration – SSL connect error (UnknownException)
    At line:24 char:21
    + … stResults = & “$($DownloadLocation)\speedtest.exe” –format=json –ac …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: ([2020-02-26 16:…knownException):String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    [2020-02-26 16:17:43.827] [error] Configuration – Cannot retrieve configuration document (0)
    [2020-02-26 16:17:43.827] [error]
    ConfigurationError – Could not retrieve or read configuration (Configuration)
    [
    2020-02-26 16:17:43.827] [error] ConfigurationError – Could not retrieve or read configuration (Configuration)
    {“type”:”log”,”timestamp”:”2020-02-27T00:17:43Z”,”message”:”Configuration – Could not retrieve or read configuration (ConfigurationError)”,”level”:”error”}

    Reply
    1. Kelvin Tegelaar Post author

      These errors come from the server you’re trying to connect to. Most likely you are either outbound blocking specific ports or you’re connecting through a proxy. You can try running the script again or selecting your own server.

      Reply
  5. Bill Shannon

    I’m wondering if there is a way to have the details of the report pumped into an email at the end of the script? I’ve tried assigning the $SpeedtestObj object to the $body of an email, but the resulting email shows “System.Collections.Hashtable” instead of the desired output that I get when put “$SpeedtestObj” at the end of the script.

    Reply
    1. Kelvin Tegelaar Post author

      Hi Bill, Try setting your body to: ($SpeedtestObj | out-string) if you want it to send just the text version. You could also convert to it HTML with ($Speedtestobj | convertto-html -frag)

      Reply
  6. Daniel Curry

    Thank you for the speed test tool. It is almost what I need. Then again, I’m not sure what I need is possible.

    What I need is a way to capture the test results so I can demonstrate to the ISP that they are NOT delivering the speeds being paid for. I am using Autotask RMM to run this for a client who is having issues with their ISP.

    Reply
    1. Daniel Curry

      Thanks.

      I added:

      “$(Get-Date -Format s): $($SpeedtestObj.downloadspeed) , $($SpeedtestObj.uploadspeed)” >> results.txt

      as the last line of the script. This will give me a simple cummulative log that I can graph and use to demosntrate the performance issues we have at this client location.

      Reply
  7. Kyle King

    I got the following results when running through Syncro RMM:

    Directory: C:\ProgramData

    Mode LastWriteTime Length Name
    —- ————- —— —-
    d—– 5/20/2020 11:55 AM SpeedtestCLI
    error> ==============================================================================
    error>
    error> You may only use this Speedtest software and information generated
    error> from it for personal, non-commercial use, through a command line
    error> interface on a personal computer. Your use of this software is subject
    error> to the End User License Agreement, Terms of Use and Privacy Policy at
    error> these URLs:
    error>
    error> https://www.speedtest.net/about/eula
    error> https://www.speedtest.net/about/terms
    error> https://www.speedtest.net/about/privacy
    error>
    error> ==============================================================================
    error>
    error> License acceptance recorded. Continuing.
    error>

    Anyone else getting this? Did they updated the speedtest program maybe?

    Reply
    1. Kelvin Tegelaar Post author

      This is expect behavior – The actual content is stored in the variable $speedtestresult and $speedtest health. you’ll have to send those to your RMM alert.

      Reply
  8. Daniel Curry

    Hello, again.

    I’m having a slight issue with this program at one particular network.

    This tool is showing their network speed to average between 100 and 200 Mb/s download speed. But they have 1Gb speed from Comcast/XFinity. Manually testing on the website shows an average of 900Mb/s, which is acceptable.

    Is there a switch I need to use?

    Thanks

    Daniel

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.