代码之家  ›  专栏  ›  技术社区  ›  WebsGhost

如何在(after pipeline)ForEach Object cmdlet中实现if语句?

  •  0
  • WebsGhost  · 技术社区  · 7 年前

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

    现在我需要实现一个 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 回复  |  直到 7 年前
        1
  •  0
  •   Ansgar Wiechers    7 年前

    在我看来 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."
    }
    

    然而,就个人而言,我更喜欢保持可ping主机和不可ping主机的输出格式相同(增加一列用于“在线”状态)。用于该用途 -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`)。