powershell-scripts/windows/Install-IPerf3.ps1
Corbin 4e18db759e Bug fixes
- Add `-UseBasicParsing` flag to release request
- Move logs directory creation to server service section
- Fix cleanup task only running when the file doesn't exist
2026-01-15 09:58:29 -05:00

125 lines
5.3 KiB
PowerShell

[CmdletBinding()]
param (
[Parameter()]
[String]
$DestinationPath = 'C:\Program Files\iperf3',
[Parameter()]
[Switch]
$ServerService
)
<#
.SYNOPSIS
Install the latest version of iPerf3
.DESCRIPTION
Download and install the latest windows build of iPerf3 with the server service.
Requires NSSM to install the server service.
.PARAMETER DestinationPath
Specifies the path into which iPerf3 is installed (Default: 'C:\Program Files\iperf3')
.PARAMETER ServerService
Specifies whether the server service should be installed
#>
begin {
$Releases = (Invoke-WebRequest 'https://api.github.com/repos/ar51an/iperf3-win-builds/releases' -UseBasicParsing).Content | ConvertFrom-Json | Where-Object { $_.prerelease -eq $false -and $_.draft -eq $false }
$ReleaseTag = ($Releases | Select-Object -First 1).tag_name
$InstallerUrl = "https://github.com/ar51an/iperf3-win-builds/releases/download/$ReleaseTag/iperf-$ReleaseTag-win64.zip"
# $Asset = ($Releases | Select-Object -First 1).Assets | Where-Object { $_.name -match "(iperf-(?:\d\.?)+-win64\.zip)" }
# $InstallerUrl = $Asset.url
$ArchivePath = Join-Path -Path $env:TEMP -ChildPath ($InstallerUrl | Select-String -Pattern "(iperf-(?:\d\.?)+-win64\.zip)").Matches.Value
# $ArchivePath = Join-Path -Path $env:TEMP -ChildPath $Asset.name
$ExecutablePath = Join-Path -Path $DestinationPath -ChildPath 'iperf3.exe'
# Install iPerf3 executable if it is not already installed
if (Test-Path $ExecutablePath) { $iPerf3Installed = $true; Write-Host 'iPerf3 already installed.' }
if ($ServerService) {
$Service = Get-Service -Name iperf3server -ErrorAction SilentlyContinue
if ($null -ne $Service) {
$ServerServiceInstalled = $true
Write-Host 'Server service already installed.'
} else {
if (-not (Test-Path 'C:\Program Files\nssm') ) {
# if ('nssm' -notin $env:PATH) {
throw 'NSSM is not installed. Please install NSSM before attempting to install the server service.'
}
}
}
if ($iPerf3Installed -and $ServerServiceInstalled) { Write-Host 'Nothing to do.'; exit 0 }
}
process {
if (-not $iPerf3Installed) {
Write-Host "Installing iPerf3..."
# Download
try {
Write-Host "Downloading application..."
$ProgressPreference = 'SilentlyContinue'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
# Use Legacy WebClient for compatibility
(New-Object Net.WebClient).DownloadFile($InstallerUrl, $ArchivePath)
} catch {
throw "Failed downloading application. $_"
}
# Install
try {
Write-Host "Installing application..."
Expand-Archive -Path $ArchivePath -DestinationPath $DestinationPath -Force
# Add the DestinationPath to Path system environment variable
if ($DestinationPath -notin $env:PATH) { [Environment]::SetEnvironmentVariable("Path", $env:Path + ";$DestinationPath\", [System.EnvironmentVariableTarget]::Machine) }
# Add firewall rule allowing application executable
if ('Allow iPerf3' -notin (Get-NetFirewallRule).DisplayName) { New-NetFirewallRule -DisplayName 'Allow iPerf3' -Direction Inbound -Program $ExecutablePath -RemoteAddress Any -Profile Any -Action Allow }
} catch {
Write-Error "Failed to install application. $_"
}
}
if ($ServerService -and -not $ServerServiceInstalled) {
try {
Write-Host "Installing server service..."
$LogPath = Join-Path -Path $DestinationPath -ChildPath 'logs'
$LogName = 'service.log'
$LogFullName = Join-Path -Path $LogPath -ChildPath $LogName
# Create the logs directory if it doesn't already exist
if (-not (Test-Path -PathType Container $LogPath)) { New-Item -ItemType Directory -Path $LogPath | Out-Null }
# Install the iperf3server service
if ((Get-CimInstance Win32_OperatingSystem).OSArchitecture) {
$NssmExecutable = 'C:\Program Files\nssm\win64\nssm.exe'
} else {
$NssmExecutable = 'C:\Program Files\nssm\win32\nssm.exe'
}
& "$NssmExecutable" install iperf3server "$ExecutablePath" "--server --port 5201 --format m --verbose"
& "$NssmExecutable" set iperf3server DisplayName "iPerf3 Server" | Out-Null
& "$NssmExecutable" set iperf3server Description "iPerf3 is a tool for active measurements of the maximum achievable bandwidth on IP networks." | Out-Null
& "$NssmExecutable" set iperf3server AppStdout "$LogFullName" | Out-Null
& "$NssmExecutable" set iperf3server AppStderr "$LogFullName" | Out-Null
Write-Host "Starting service..."
Start-Service -Name iperf3server
} catch {
Write-Error "Failed to install server service. $_"
}
}
}
end {
# Clean up
if (Test-Path -Path $ArchivePath) {
Write-Host "Removing iPerf package download from '$ArchivePath'"
Remove-Item -Path $ArchivePath -Force
}
}