Maintaining optimal network performance is crucial in the IT world. One essential aspect of this is ensuring that internet speeds are consistently meeting required standards. IT professionals and Managed Service Providers (MSPs) need reliable methods to measure and verify these speeds.
A PowerShell script that automates this process can save time and ensure accurate, up-to-date information. This blog post will explore a detailed script designed to run an internet speed test using Ookla’s CLI and store the results in a custom field.
Background
The provided script leverages Ookla’s CLI to run internet speed tests on Windows devices, making it highly useful for IT professionals and MSPs. This script addresses a common challenge: testing and verifying internet speed across multiple devices without causing network congestion. By randomizing the test start time within a 60-minute window, it ensures that the tests are spread out, reducing the likelihood of simultaneous testing.
The Script:
#Requires -Version 3.0 <# .SYNOPSIS Runs an internet speed test using Ookla Cli on the target windows device and saves the results to a Multi-Line Custom Field. .DESCRIPTION Runs an internet speed test using Ookla Cli on the target windows device and saves the results to a Multi-Line Custom Field. Script will pick a random time slot from 0 to 60 minutes to run the speed test. This lessens the likely hood that multiple devices are testing at the same time. The default custom field: speedtest .OUTPUTS None .NOTES Minimum OS Architecture Supported: Windows 7, Windows Server 2012 Minimum PowerShell Version: 3.0 Release Notes: Renamed script and added Script Variable support By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use. Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA). .COMPONENT Utility #> [CmdletBinding()] param ( [Parameter()] [string]$CustomField = "speedtest", [Parameter()] [switch]$SkipSleep = [System.Convert]::ToBoolean($env:skipRandomSleepTime) ) begin { if ($env:customFieldName -and $env:customFieldName -notlike "null") { $CustomField = $env:customFieldName } # add TLS 1.2 and SSL3 to allow Invoke-WebRequest to work under older PowerShell versions if ($PSVersionTable.PSVersion.Major -eq 2) { Write-Host "Requires at least PowerShell 3.0 to run." exit 1 } else { try { $TLS12Protocol = [System.Net.SecurityProtocolType] 'Ssl3 , Tls12, Tls11' [System.Net.ServicePointManager]::SecurityProtocol = $TLS12Protocol } catch { Write-Host "Failed to set SecurityProtocol to Tls 1.2, Tls1.1, and Ssl3" } } $CurrentPath = Get-Item -Path ".\" } process { # Random delay from 0 to 60 minutes in 2 minute time slots $MaximumDelay = 60 $TimeChunks = 2 $Parts = ($MaximumDelay / $TimeChunks) + 1 $RandomNumber = Get-Random -Minimum 0 -Maximum $Parts $Minutes = $RandomNumber * $TimeChunks if (-not $SkipSleep) { Start-Sleep -Seconds $($Minutes * 60) } # Get latest version of speedtest cli try { $Cli = Invoke-WebRequest -Uri "https://www.speedtest.net/apps/cli" -UseBasicParsing } catch { Write-Host "Failed to query https://www.speedtest.net/apps/cli for speed test cli zip." exit 1 } # Get the download link $Url = $Cli.Links | Where-Object { $_.href -like "*win64*" } | Select-Object -ExpandProperty href # Build the URL and destination path $InvokeSplat = @{ Uri = $Url OutFile = Join-Path -Path $CurrentPath -ChildPath $($Url | Split-Path -Leaf) } # Download the speedtest cli zip try { Invoke-WebRequest @InvokeSplat -UseBasicParsing } catch { Write-Host "Failed to download speed test cli zip from $Url" exit 1 } # Build the path to speedtest.exe $ExePath = Join-Path -Path $CurrentPath -ChildPath "speedtest.exe" $MdPath = Join-Path -Path $CurrentPath -ChildPath "speedtest.md" if ($(Get-Command -Name "Expand-Archive" -ErrorAction SilentlyContinue).Count) { Expand-Archive -Path $InvokeSplat["OutFile"] -DestinationPath $CurrentPath } else { # Unzip the speedtest cli zip Add-Type -AssemblyName System.IO.Compression.FileSystem if ((Test-Path -Path $ExePath)) { Remove-Item -Path $ExePath, $MdPath -Force -Confirm:$false -ErrorAction Stop } [System.IO.Compression.ZipFile]::ExtractToDirectory($InvokeSplat["OutFile"], $CurrentPath) } $JsonOutput = if ($(Test-Path -Path $ExePath -ErrorAction SilentlyContinue)) { # Run speed test and output in a json format try { Invoke-Command -ScriptBlock { & .\speedtest.exe --accept-license --accept-gdpr --format=json if (0 -ne $LASTEXITCODE) { Write-Error -Message "Failed to run speedtest.exe." } } } catch { Remove-Item -Path $ExePath, $MdPath, $InvokeSplat["OutFile"] -Force -Confirm:$false Write-Error -Message "Failed to run speedtest.exe." exit 1 } } if ($JsonOutput) { # Convert from Json to PSCustomObject $Output = $JsonOutput | ConvertFrom-Json # Output the results $Results = [PSCustomObject]@{ Date = $Output.timestamp | Get-Date ISP = $Output.isp Down = "$([System.Math]::Round($Output.download.bandwidth * 8 / 1MB,0)) Mbps" Up = "$([System.Math]::Round($Output.upload.bandwidth * 8 / 1MB,0)) Mbps" ResultUrl = $Output.result.url PacketLoss = $Output.packetLoss Jitter = $Output.ping.jitter Latency = $Output.ping.latency Low = $Output.ping.low High = $Output.ping.high } | Out-String $Results | Write-Host Ninja-Property-Set -Name $CustomField -Value $Results } Remove-Item -Path $ExePath, $MdPath, $InvokeSplat["OutFile"] -Force -Confirm:$false exit 0 } end { }
Access over 300+ scripts in the NinjaOne Dojo
Detailed Breakdown
The script performs several key tasks:
- Parameter Initialization: Sets up default parameters and checks for environment variables.
- TLS Configuration: Ensures the appropriate security protocols are set for older PowerShell versions.
- Random Delay: Introduces a random delay before running the speed test to avoid network congestion.
- Speedtest CLI Download: Downloads the latest version of Ookla’s CLI.
- Speed Test Execution: Runs the speed test and captures the results in JSON format.
- Result Parsing and Storage: Converts the JSON results to a readable format and stores them in a custom field.
Step-by-Step Breakdown
- Parameter Initialization The script begins by defining parameters. $CustomField specifies where the results will be stored, and $SkipSleep is a switch to skip the randomized delay.
- TLS Configuration This section ensures that the appropriate TLS protocols are enabled, which is necessary for secure web requests.
- Random Delay To prevent network congestion, the script waits for a random time between 0 and 60 minutes before executing the speed test.
- Speedtest CLI Download This part downloads and extracts the latest Ookla CLI.
- Speed Test Execution The script runs the speed test and outputs the results in JSON format.
- Result Parsing and Storage The results are parsed and stored in a custom field.
Potential Use Cases
Imagine an MSP managing multiple client networks. By deploying this script across client machines, the MSP can gather internet speed data without manual intervention. For instance, if a client reports intermittent slow internet speeds, the MSP can review the historical speed test results to identify patterns and address the issue more effectively.
Comparisons
Other methods for testing internet speed might include using web-based tools or standalone applications. While these can be effective, they lack automation and centralized data storage. This PowerShell script offers a streamlined, automated solution that integrates seamlessly with existing systems, providing a significant advantage in terms of efficiency and data management.
FAQs
Q1: Why use a random delay?
A1: The random delay helps prevent multiple devices from running the speed test simultaneously, which could skew results and create network congestion.
Q2: Can this script run on any version of PowerShell?
A2: The script requires at least PowerShell 3.0 due to certain cmdlet dependencies and TLS configurations.
Q3: What if the download of the Ookla CLI fails?
A3: The script includes error handling to exit gracefully and provide an error message if the download fails.
Implications
Accurate internet speed testing can help identify potential network issues, including security-related anomalies such as unexpected drops in speed that could indicate network interference or unauthorized usage. Regular monitoring using this script can provide insights into network performance and help maintain robust security standards.
Recommendations
- Regular Scheduling: Schedule the script to run at regular intervals for continuous monitoring.
- Review Results: Periodically review the collected data to identify trends and address any issues.
- Secure Storage: Ensure that the results are stored securely to prevent unauthorized access.
Final Thoughts
Using this PowerShell script to run internet speed tests offers a powerful tool for IT professionals and MSPs. By automating the process and storing results in a custom field, it simplifies network performance monitoring. NinjaOne can further enhance this capability by integrating the script into its suite of tools, providing a centralized platform for managing and analyzing network performance data.