PowerShell中Test-NetConnection如何实现分组带表头输出
问题根因
- 错误提示无法屏蔽:
-ErrorAction SilentlyContinue仅能抑制错误流的输出,Test-NetConnection连接超时、名称解析失败的提示默认走警告流,不会被这个参数拦截。另外原脚本存在一处语法错误:MP3 443端口测试的Select-Object -后被强制换行,Property参数被拆分到下一行,会直接触发语法报错。同时脚本中使用交互式命令Enter-PSSession时,本地提前定义的服务器地址变量不会自动同步到远程会话,会触发变量未定义的运行错误。 - 表头仅显示一次:PowerShell默认的输出格式化逻辑会自动合并连续输出的同结构对象,统一渲染成一个表格,仅在第一组对象前打印一次表头,后续同结构结果会直接追加行内容,不会重复输出表头。
修复方案(完全保留原有逐行编写测试条目的逻辑,不引入foreach循环)
- 修复拆分断行的语法错误
- 给所有
Test-NetConnection命令追加-WarningAction SilentlyContinue参数,屏蔽警告流提示 - 同一测试组的所有测试结果先收集到独立的数组变量中,整组测试完成后统一通过
Format-Table输出,强制每组单独渲染表格和表头 - 替换交互式的
Enter-PSSession为脚本可用的Invoke-Command,通过参数传递本地定义的服务器地址变量到远程会话,避免变量未定义错误
修复后的完整脚本如下:
$DP = Read-Host -Prompt 'Input the FQDN for the local DP' $MP1 = 'MP1' $MP2 = 'MP2' $MP3 = 'MP3' $SUP1 = 'SUP1' $SUP2 = 'SUP2' $Client = Read-Host -Prompt 'Input the workstation to test from' Invoke-Command -ComputerName $Client -ArgumentList $DP,$MP1,$MP2,$MP3,$SUP1,$SUP2 -ScriptBlock { param($DP,$MP1,$MP2,$MP3,$SUP1,$SUP2) Write-Host 'Testing connectivity to the local distribution point' $dpGroup = @() $dpGroup += Test-NetConnection -Port 80 $DP -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $dpGroup += Test-NetConnection -Port 443 $DP -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $dpGroup += Test-NetConnection -Port 8005 $DP -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $dpGroup | Format-Table Write-Host 'Testing connectivity to the management points' $mpGroup = @() $mpGroup += Test-NetConnection -Port 10123 $MP1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $mpGroup += Test-NetConnection -Port 80 $MP1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $mpGroup += Test-NetConnection -Port 443 $MP1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $mpGroup += Test-NetConnection -Port 10123 $MP2 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $mpGroup += Test-NetConnection -Port 80 $MP2 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $mpGroup += Test-NetConnection -Port 443 $MP2 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $mpGroup += Test-NetConnection -Port 10123 $MP3 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $mpGroup += Test-NetConnection -Port 80 $MP3 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $mpGroup += Test-NetConnection -Port 443 $MP3 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $mpGroup | Format-Table Write-Host 'Testing connectivity to the software update points' $supGroup = @() $supGroup += Test-NetConnection -Port 80 $SUP1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $supGroup += Test-NetConnection -Port 8530 $SUP1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $supGroup += Test-NetConnection -Port 443 $SUP1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $supGroup += Test-NetConnection -Port 8531 $SUP1 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $supGroup += Test-NetConnection -Port 80 $SUP2 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $supGroup += Test-NetConnection -Port 8530 $SUP2 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $supGroup += Test-NetConnection -Port 443 $SUP2 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $supGroup += Test-NetConnection -Port 8531 $SUP2 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded $supGroup | Format-Table }
如果你习惯手动先输入
Enter-PSSession $Client进入远程会话后逐行跑命令,可以去掉外层的Invoke-Command包裹,进入会话后先手动赋值所有服务器变量,再按组收集结果通过Format-Table输出即可,效果完全一致。
内容的提问来源于stack exchange,提问作者Steve F
相关产品推荐
相关产品推荐

