PSCustomobject回归奇怪的输出

时间:2018-04-06 10:49:14

标签: powershell powershell-v2.0 powershell-v3.0

我有一个脚本应该在三台服务器上运行测试连接和test-netconnection,现在我正在用“localhost”进行测试

事情是我希望它至少会做两次“测试连接”并且确实如此,但是输出很奇怪(从逻辑上看是合理的,但是从输出的角度看它是不)

这是代码: 如果我使用count参数并将其设置为1次,则输出是正确的,它显示一个pc进行测试,如果我将其设置为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

还在第二个中显示奇怪的输出我正在添加Erroraction参数 (但我会留下另一篇文章) 我怎样才能将这些双“local-pc”输出转换成一个?

非常感谢您的帮助

2 个答案:

答案 0 :(得分:3)

根据评论,您的问题是[0]最终包含两个对象,每个对象对应一个测试结果。您可以使用数组索引运算符(例如Select -First 1)来选择特定对象的结果,也可以使用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 ,而是将结果整理为变量,然后在最后使用该变量。

以下是您的代码中的修订:

    unbinder = ButterKnife.bind(this, view);
    initialize();
    loadSkillsData();

答案 1 :(得分:0)

这将为每次'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  
}