【问题标题】:PowerShell - Adjust output header groupsPowerShell - 调整输出标头组
【发布时间】:2022-07-12 00:28:03
【问题描述】:

所以,我有一个简单的脚本,它针对多个不同的服务器和端口运行 test-netconnection,以验证 SCCM 客户端是否具有必要的连接性。我在一开始就收集了几个变量,我静态设置了一些其他变量,因为它们不是特定于站点的,将用于所有测试。

但问题是我想在每个部分之前在屏幕上打印一个描述符,然后是输出表,只是为了让输出更容易一目了然。但它根本没有这样做,我不知道如何让它做到这一点。所以基本上我想要这样的东西:

Testing connectivity to the local distribution point
ComputerName    RemoteAddress   RemotePort        TCPTestSucceeded
--------------  -----------     ----------------  ---------------- 
DPserver        192.168.1.1     80                True

Testing Connectivity to the Management Points
ComputerName    RemoteAddress   RemotePort        TCPTestSucceeded
--------------  -----------     ----------------  ---------------- 
MPserver        192.168.1.1     80                True

但相反,我得到了这个:

Testing connectivity to the local distribution point

WARNING: TCP connect to (10.30.22.72 : 8005) failed
ComputerName                 RemoteAddress RemotePort TcpTestSucceeded
------------                 ------------- ---------- ----------------
DPserver                     10.30.22.72           80             True

Testing connectivity to the management points
MPserver1                    10.88.166.167      10123             True

部分问题是出现了错误,即使我将 -ErrorAction 设置为 SilentlyContinue,然后部分问题是标题只出现一次。我猜我必须以某种方式对结果进行分组,以使每个组的标题都出现,但我不知道该怎么做。

有人可以帮忙吗?这是我现在拥有的完整脚本(为了发布而更改了静态服务器名称):

$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'

Enter-PSSession $Client

Write-Host 'Testing connectivity to the local distribution point'
Test-NetConnection -Port 80 $DP -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 443 $DP -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 8005 $DP -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded

Write-Host 'Testing connectivity to the management points'
Test-NetConnection -Port 10123 $MP1 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 80 $MP1 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 443 $MP1 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 10123 $MP2 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 80 $MP2 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 443 $MP2 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 10123 $MP3 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 80 $MP3 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 443 $MP3 -Erroraction SilentlyContinue | Select-Object -

Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Write-Host 'Testing connectivity to the software update points'
Test-NetConnection -Port 80 $SUP1 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 8530 $SUP1 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 443 $SUP1 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 8531 $SUP1 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 80 $SUP2 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 8530 $SUP2 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 443 $SUP2 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded
Test-NetConnection -Port 8531 $SUP2 -Erroraction SilentlyContinue | Select-Object -Property ComputerName,RemoteAddress,RemotePort,TCPTestSucceeded

Exit-PSSession

另外,我知道我可以为某些部分执行 for-each 循环,但我想先让它按原样工作,然后再找出 for-each 循环。 (我是一个非常新手脚本编写者。)

【问题讨论】:

    标签: powershell


    【解决方案1】:

    没有错误,只有警告,您可能也想将它们静音。

    在代码顶部使用下面。

    $WarningPreference='SilentlyContinue'

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多