PSCustomobject 返回奇怪的输出
PSCustomobject returrning strange output
我有一个脚本,应该 运行 三台服务器上的测试连接和测试网络连接,现在我正在用 "localhost"
进行测试
事情是我想要它至少使 "test-connection" 的两倍并且确实如此,但是输出很奇怪(从逻辑的角度来看是合理的,但是从输出的角度来看认为不是)
代码如下:
如果我使用 count 参数并将其设置为 1 次,输出是正确的,它显示了一台进行测试的电脑,如果我将其设置为 2 次,则输出是混乱的
$computers="localhost"
foreach ($pc in $computers){
$test_connection = Test-Connection -ComputerName $pc -Count 2
$test_netconnection = Test-NetConnection $pc -Port 1433
#}else{ Write-Host "can't reach $pc"}
[pscustomobject] @{
LocalPC =$test_connection.PSComputerName
'Tested-Server' =$test_netconnection.ComputerName
Bytes =$test_connection.buffersize
Time =$test_connection.ResponseTime
RemotePort =$test_netconnection.RemotePort
TcpTestSucceeded =$test_netconnection.TcpTestSucceeded
}| ft -AutoSize #end of Customobject
}
#}#end of foreach loop
pause
输出:
WARNING: TCP connect to (::1 : 1433) failed
WARNING: TCP connect to (127.0.0.1 : 1433) failed
LocalPC Tested-Server Bytes Time RemotePort TcpTestSucceeded
------- ------------- ----- ---- ---------- ----------------
{LEVL-01, LEVL-01} localhost {32, 32} {0, 0} 1433 False
t 在我添加 Eroraction 参数的第二个中也显示了奇怪的输出
(但我会把它留给另一个 post)
我怎样才能将这些双重 "local-pc" 输出转换为一个?
非常感谢您的帮助
这将为每个 'ping' 尝试创建一个单独的对象:
$computers="localhost"
foreach ($pc in $computers)
{
$test_netconnection = Test-NetConnection $pc -Port 1433
Test-Connection -ComputerName $pc -Count 2 |
ForEach-Object {
[pscustomobject] @{
LocalPC =$_.PSComputerName
'Tested-Server' =$test_netconnection.ComputerName
Bytes =$_.buffersize
Time =$_.ResponseTime
RemotePort =$test_netconnection.RemotePort
TcpTestSucceeded =$test_netconnection.TcpTestSucceeded
}
} | ft -AutoSize #end of Customobject
}
根据评论,您的问题是 $test_connection
最终包含两个对象,每个对象对应一个测试结果。您可以使用数组索引运算符 select 特定对象的结果,例如 [0]
或者您可以使用 Select -First 1
获得第一个结果。我推荐后者,因为如果结果恰好为空,则抛出异常的可能性较小。
作为进一步的建议,您可以 return 通过使用 Measure-Object -Average
进行这两项测试所花费的时间的平均结果。我还建议您不要在循环中使用 Format-Table
,而是将结果整理到一个变量中,然后在最后将该变量用于该变量。
以下是您的代码中的那些修改:
$computers="localhost"
$Result = foreach ($pc in $computers){
$test_connection = Test-Connection -ComputerName $pc -Count 2
$test_netconnection = Test-NetConnection $pc -Port 1433
[pscustomobject] @{
LocalPC = ($test_connection | Select -First 1).PSComputerName
'Tested-Server' = $test_netconnection.ComputerName
Bytes = ($test_connection | Select -First 1).Bytes
Time = ($test_connection | Measure-Object -Average ResponseTime).Average
RemotePort = $test_netconnection.RemotePort
TcpTestSucceeded = $test_netconnection.TcpTestSucceeded
}
}
$Result | Ft -AutoSize
我有一个脚本,应该 运行 三台服务器上的测试连接和测试网络连接,现在我正在用 "localhost"
进行测试事情是我想要它至少使 "test-connection" 的两倍并且确实如此,但是输出很奇怪(从逻辑的角度来看是合理的,但是从输出的角度来看认为不是)
代码如下: 如果我使用 count 参数并将其设置为 1 次,输出是正确的,它显示了一台进行测试的电脑,如果我将其设置为 2 次,则输出是混乱的
$computers="localhost"
foreach ($pc in $computers){
$test_connection = Test-Connection -ComputerName $pc -Count 2
$test_netconnection = Test-NetConnection $pc -Port 1433
#}else{ Write-Host "can't reach $pc"}
[pscustomobject] @{
LocalPC =$test_connection.PSComputerName
'Tested-Server' =$test_netconnection.ComputerName
Bytes =$test_connection.buffersize
Time =$test_connection.ResponseTime
RemotePort =$test_netconnection.RemotePort
TcpTestSucceeded =$test_netconnection.TcpTestSucceeded
}| ft -AutoSize #end of Customobject
}
#}#end of foreach loop
pause
输出:
WARNING: TCP connect to (::1 : 1433) failed
WARNING: TCP connect to (127.0.0.1 : 1433) failed
LocalPC Tested-Server Bytes Time RemotePort TcpTestSucceeded
------- ------------- ----- ---- ---------- ----------------
{LEVL-01, LEVL-01} localhost {32, 32} {0, 0} 1433 False
t 在我添加 Eroraction 参数的第二个中也显示了奇怪的输出 (但我会把它留给另一个 post) 我怎样才能将这些双重 "local-pc" 输出转换为一个?
非常感谢您的帮助
这将为每个 'ping' 尝试创建一个单独的对象:
$computers="localhost"
foreach ($pc in $computers)
{
$test_netconnection = Test-NetConnection $pc -Port 1433
Test-Connection -ComputerName $pc -Count 2 |
ForEach-Object {
[pscustomobject] @{
LocalPC =$_.PSComputerName
'Tested-Server' =$test_netconnection.ComputerName
Bytes =$_.buffersize
Time =$_.ResponseTime
RemotePort =$test_netconnection.RemotePort
TcpTestSucceeded =$test_netconnection.TcpTestSucceeded
}
} | ft -AutoSize #end of Customobject
}
根据评论,您的问题是 $test_connection
最终包含两个对象,每个对象对应一个测试结果。您可以使用数组索引运算符 select 特定对象的结果,例如 [0]
或者您可以使用 Select -First 1
获得第一个结果。我推荐后者,因为如果结果恰好为空,则抛出异常的可能性较小。
作为进一步的建议,您可以 return 通过使用 Measure-Object -Average
进行这两项测试所花费的时间的平均结果。我还建议您不要在循环中使用 Format-Table
,而是将结果整理到一个变量中,然后在最后将该变量用于该变量。
以下是您的代码中的那些修改:
$computers="localhost"
$Result = foreach ($pc in $computers){
$test_connection = Test-Connection -ComputerName $pc -Count 2
$test_netconnection = Test-NetConnection $pc -Port 1433
[pscustomobject] @{
LocalPC = ($test_connection | Select -First 1).PSComputerName
'Tested-Server' = $test_netconnection.ComputerName
Bytes = ($test_connection | Select -First 1).Bytes
Time = ($test_connection | Measure-Object -Average ResponseTime).Average
RemotePort = $test_netconnection.RemotePort
TcpTestSucceeded = $test_netconnection.TcpTestSucceeded
}
}
$Result | Ft -AutoSize