如何在一个(管道之后)ForEach-Object cmdlet中实现if语句?

时间:2018-04-09 10:58:00

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

这里有人帮我编写了一些代码并编写了这个脚本(我的原始脚本有一些问题)。

现在我需要在if内部实现ForEach-Object语句(做这样的事情"如果其中一个服务器不活动Write-Host "blah blah"")不停止,因此try / catch不适合此处。

以下是代码(我尝试使用该代码,但没有成功)。

$computers = "localhost"
foreach ($pc in $computers) {
    $test_netconnection = Test-NetConnection $pc -Port 1433
    Test-Connection -ComputerName $pc -Count 2 -Quiet | ForEach-Object {
        [PSCustomObject]@{
            LocalPC          = $_.PSComputerName
            'Tested-Server'  = $test_netconnection.ComputerName
            Bytes            = $_.buffersize
            Time             = $_.ResponseTime
            RemotePort       = $test_netconnection.RemotePort
            TcpTestSucceeded = $test_netconnection.TcpTestSucceeded
        }
    }
}

这是网络上计算机的正确输出

LocalPC Tested-Server Bytes Time RemotePort TcpTestSucceeded
------- ------------- ----- ---- ---------- ----------------
LEVL-01 localhost        32    0       1433            False
LEVL-01 localhost        32    0       1433            False

这是不在网络上的计算机的输出

    test-Connection : Testing connection to computer 'localh0st' failed: No such host is known
   servers\Foreach-object.ps1:9 char:5
    +     test-Connection -ComputerName $pc -Count 2 |
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceUnavailable: (localh0st:String) [Test-Connection], PingException
        + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand

而不是红色错误,我需要让它显示类似"计算机不在线"而第一个输出是左边的 - 这就是为什么我试图实现" IF"环

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我可以看到try / catch 正好你要问的是什么:

try {
    Test-Connection -Computer $pc -Count 2 -EA Stop | ForEach-Object {
        [PSCustomObject]@{
            LocalPC          = $env:COMPUTERNAME
            'Tested-Server'  = $pc
            Bytes            = $_.buffersize
            Time             = $_.ResponseTime
            RemotePort       = $test_netconnection.RemotePort
            TcpTestSucceeded = $test_netconnection.TcpTestSucceeded
        }
    }
} catch {
    "The computer $pc is not online."
}

但是,我个人更喜欢保持pingable和不可ping的主机的输出格式相同(“online”状态的附加列)。为了使用-ErrorAction SilentlyContinue来抑制错误,请在变量中收集Test-Connection的结果,然后根据该变量是否为空来构建自定义对象。

$test_connection = Test-Connection -Computer $pc -Count 2 -EA SilentlyContinue
if ($test_connection) {
    $test_connection | ForEach-Object {
        [PSCustomObject]@{
            LocalPC          = $env:COMPUTERNAME
            'Tested-Server'  = $pc
            Online           = $true
            Bytes            = $_.buffersize
            Time             = $_.ResponseTime
            RemotePort       = $test_netconnection.RemotePort
            TcpTestSucceeded = $test_netconnection.TcpTestSucceeded
        }
    }
} else {
    [PSCustomObject]@{
        LocalPC          = $env:COMPUTERNAME
        'Tested-Server'  = $pc
        Online           = $false
        Bytes            = $null
        Time             = $null
        RemotePort       = $test_netconnection.RemotePort
        TcpTestSucceeded = $test_netconnection.TcpTestSucceeded
    }
}

作为附注,我不建议使用原始信息可用的派生信息($_.PSComputerName$test_netconnection.ComputerName)($env:COMPUTERNAME,$ pc`)。