[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').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..." # Prep # Create the logs directory if it doesn't already exist if (-not (Test-Path -PathType Container (Join-Path -Path $DestinationPath -ChildPath 'logs'))) { New-Item -ItemType Directory -Path (Join-Path -Path $DestinationPath -ChildPath 'logs') | Out-Null } # 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..." # 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 "$(Join-Path -Path $DestinationPath -ChildPath 'logs\service.log')" | Out-Null & "$NssmExecutable" set iperf3server AppStderr "$(Join-Path -Path $DestinationPath -ChildPath 'logs\service.log')" | Out-Null Write-Host "Starting service..." Start-Service -Name iperf3server } catch { Write-Error "Failed to install server service. $_" } } } end { # Clean up if (-not (Test-Path -Path $ArchivePath)) { Write-Host "Removing iPerf package download from '$ArchivePath'" Remove-Item -Path $ArchivePath -Force } }