将1个输出表中的2个命令的结果组合在一起

时间:2018-04-04 06:42:52

标签: powershell powershell-v3.0

我需要让下面的脚本显示Test-Connection 3次。

该脚本仅显示Test-NetConnection的步骤。问题是,如何将Test-Connection和Test-NetConnection`组合到这一个表中?

以下是代码:

$computers = "pc1", "pc2"

foreach ($pc in $computers) {
    $test_connection = try {
        Test-Connection $pc -Count 3 -ErrorAction Stop
    } catch {
        Write-Warning "->$pc<- is not responding!"
    }
    if ($test_connection) {
        Test-NetConnection $pc -Port 1433
        [PSCustomObject]@{
            Computername          = $test_connection.PSComputername
            'Problematic-server'  = $test_connection.Address;
            buffersize            = $test_connection.buffersize;
            'My Local IP Adress'  = $test_netconnection.SourceAddress;
            'problematic-servers' = $test_netconnection.ComputerName;
            RemoteAddress         = $test_netconnection.RemoteAddress;
            RemotePort            = $test_netconnection.RemotePort;
            TcpTestSucceeded      = $test_netconnection.TcpTestSucceeded
        } | Format-Table -AutoSize
    }
}

结果如下:

Computername                        Problematic-server                                                                                 buffersize   My Local IP Adress problematic-servers RemoteAddress RemotePort
------------                        ------------------                                                                                 ----------   ------------------ ------------------- ------------- -----
{loclpc-01, localpc-01, localpc-01} System.Object&, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=123456789 Address(int ) {32, 32, 32} blah-blah          blah-blah           192.168.0.0   1433

如果我从表格中移除$test_connection,它会正确执行Test-NetConnection程序,否则会显示这个奇怪的答案。

0 个答案:

没有答案