Files
PowerShell/vCenter Tools/NetworkTester.ps1
T
2026-07-24 17:35:40 -07:00

184 lines
7.1 KiB
PowerShell

param (
[string]$vCenter = "",
[string]$cluster = "",
[string]$VmHost = "",
[string]$vmkPort = "",
[int]$testFrame = 1472,
[switch]$SaveCred = $false,
[switch]$Help = $false
)
# Variables
$fileVersion = "1.2.6"
$fileDate = "2025-09-15"
# Display Script Banner
Write-Host "--------------------------------------------------" -ForegroundColor White
Write-Host "Host Network Testing Script" -ForegroundColor Green
Write-Host "Version $($FileVersion) ($($FileDate))" -ForegroundColor Green
Write-Host "Written by Charlie Silverman" -ForegroundColor Green
Write-Host "--------------------------------------------------" -ForegroundColor White
if ($Help) {
Write-Host
Write-Host "Purpose: Perform multiple ping tests simultaneously from an ESXi host."
Write-Host "Command Syntax:"
Write-Host " NetworkTester.ps1 (-vcenter [vcenter host name]) (-cluster ""[cluster]"") (-VmHost ""[host]"") (-vmkPort ""[vmkPort]"")" -foregroundcolor green
Write-Host " (-testFrame [framesize]) (-savecred) (-help)`n" -foregroundcolor green
Write-Host "Command Line Options:"
Write-Host
Write-Host -NoNewLine " -vCenter " -foregroundcolor Green
Write-Host "- The FQDN or IP address of a vCenter server."
Write-Host -NoNewLine " -cluster " -foregroundcolor Green
Write-Host "- The name of the cluster as shown in vCenter."
Write-Host -NoNewLine " -VmHost " -foregroundcolor Green
Write-Host "- [Optional] The name of a host to test. If not specified, all hosts in the cluster will be tested."
Write-Host -NoNewLine " -vmkPort " -foregroundcolor Green
Write-Host "- The VMkernel port on the host to test."
Write-Host -NoNewLine " -testFrame " -foregroundcolor Green
Write-Host "- The TCP frame size to use for the test. Default: 1500."
Write-Host -NoNewLine " -savecred " -foregroundcolor Green
Write-Host "- Configure and store vCenter server credentials. This will only store the credentials under"
Write-Host " the current logged in user."
Write-Host -NoNewLine " -help " -foregroundcolor Green
Write-Host "- Display command help."
Write-Host
Write-Host "If any of the information is not provided, you will be prompted. If a credential has been previously stored, it will"
Write-Host "be used again unless the -savecred option is specified."
Write-Host
Write-Host "Each host in the selected cluster will perform ping tests to all IP addresses assigned to the selected vmKernel port"
Write-Host "on all hosts in the cluster."
Write-Host
exit
}
$quiet = Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
if ($vCenter -eq "") {
$vCenter = Read-Host "Enter name of vCenter Server"
}
if ($SaveCred) {
$credential = Get-Credential -Message "Enter credentials for vCenter server $vCenter"
$credential | Export-Clixml -path $env:USERPROFILE\kispingtest-$vcenter.cred
Write-Host "Saving credentials."
}
if (Test-Path -path $env:USERPROFILE\kispingtest-$vcenter.cred -PathType Leaf) {
$credentials = Import-Clixml -path $env:USERPROFILE\kispingtest-$vcenter.cred
Write-Host "Using stored credentials"
} else {
$credentials = Get-Credential -Message "Enter credentials for vCenter server $vCenter"
if (!$credentials) {
Write-Host "ERROR: No credentials were entered" -foregroundcolor yellow
}
}
try {
Write-Host -NoNewLine "Connecting to vCenter $vcenter..."
$vcconn = Connect-VIServer $vcenter -Credential $credentials -ErrorAction SilentlyContinue
Write-Host " Success" -ForegroundColor green
} catch {
Write-Host " Failure" -ForegroundColor Yellow -BackgroundColor Red
exit
}
if ($cluster -eq "") {
$Clusterlist = Get-Cluster | Sort-Object
While (($cluster -eq "") -or (-not $cluster)) {
Write-Host "`nCluster Selection:`n"
for ($x = 0; $x -lt $Clusterlist.length; $x++) {
Write-Host "$(([string]($x+1)).PadLeft(3,' '))" -NoNewLine -ForegroundColor Green
Write-Host ": " -NoNewLine -ForegroundColor Gray
Write-Host "$($Clusterlist[$x].name)" -ForegroundColor Yellow
}
$Clusternum = Read-Host "`nSelect Cluster Number"
$Clusternum = [int]$Clusternum - 1
$cluster = $Clusterlist[$Clusternum]
}
$mycluster = get-cluster $cluster
} else {
$mycluster = get-cluster $cluster
}
$firsthost = $mycluster | Get-VMHost | Select-Object -first 1
$vmks = $firsthost | Get-VMHostNetworkAdapter | Where-Object {$_.Name -like "vmk*"} | Sort-Object Name
While (($vmkPort -eq "") -or (-not $vmkPort)) {
Write-Host "`nVMKernel Port Selection:`n"
for ($x = 0; $x -lt $vmks.length; $x++) {
$myvmk = $vmks[$x];
$mask = [ipaddress]$myvmk.SubnetMask
$binary = [convert]::ToString($mask.Address, 2)
$mask_length = ($binary -replace 0,$null).Length
Write-Host "$(([string]($x+1)).PadLeft(3,' '))" -NoNewLine -ForegroundColor Green
Write-Host ": " -NoNewLine -ForegroundColor Gray
Write-Host "$($myvmk.Name) - $($myvmk.PortGroupName) ($($myvmk.IP)/$($mask_length))" -ForegroundColor Yellow
}
$vmknum = Read-Host "`nSelect VMKernel Port Number for Testing"
$vmkPort = $vmks[$vmknum - 1].Name
$myvmk = $vmks[$vmknum - 1]
}
if ((-not(Test-Path variable:myvmk) -or ($myvmk -ne $null))) {
$myvmk = $firsthost | Get-VMHostNetworkAdapter | Where-Object {$_.Name -like "$vmkPort"}
$mask = [ipaddress]$myvmk.SubnetMask
$binary = [convert]::ToString($mask.Address, 2)
$mask_length = ($binary -replace 0,$null).Length
Write-Host "Using $($myvmk.Name) ($($myvmk.PortGroupName)) for Testing"
}
$targetlist = get-cluster $mycluster | get-vmhost | Get-VMHostNetworkAdapter | Where-Object {$_.Name -like "$myvmk"} | Select-Object -Property IP | Sort-Object IP
if ($VmHost -ne "") {
try {
$hostlist = Get-VMhost $VmHost -ErrorAction Stop
} catch {
Write-Host "ERROR - Host $VmHost not found." -ForegroundColor Yellow -BackgroundColor Red
exit*
}
if ($hostlist[0].parent.name -ne $cluster) {
Write-Host "ERROR - Host $VmHost not part of cluster $cluster" -ForegroundColor Yellow -BackgroundColor Red
exit
}
} else {
$hostlist = $mycluster | Get-VMHost | Sort-Object Name
}
for ($x = 0; $x -lt $hostlist.length; $x++) {
$myhost = $hostlist[$x]
$myip = $myhost | Get-VMHostNetworkAdapter | Where-Object {$_.Name -like "$myvmk"} | Select-Object -Property IP
$hostname = $myhost.name
$tcpstack = $myvmk.ExtensionData.Spec.NetStackInstanceKey
Write-Host "Testing Host $hostname on $($myvmk.PortGroupName) with a frame size of $testFrame ($myip)"
$esxcli = get-esxcli -vmhost (get-vmhost $myhost) -v2
for ($y = 0; $y -lt $targetlist.length; $y++) {
$params = (get-esxcli -v2 -vmhost (get-vmhost $myhost)).network.diag.ping.CreateArgs()
$params.host = $targetlist[$y].IP
$params.interface = $vmkPort
$params.size = $testFrame
$params.df = $true
$params.count = 1
if ($tcpstack -ne "defaultTcpipStack") {
$params.netstack = $tcpstack
}
$result = ($esxcli.network.diag.ping.Invoke($params)).summary
$response = $vmkPort + "->" + $result.HostAddr + "(" + $testFrame + ") "
if ($result.Received -gt 0) {
$test = "Good"
} else {
$test = "Bad"
}
Write-Host " - $($hostname): $response" -NoNewLine
If ($test -eq "Good") {
Write-Host "$test" -ForegroundColor Green
} else {
Write-Host "$test" -ForegroundColor Red
}
}
}
Disconnect-VIServer $vCenter -confirm:$false