【问题标题】:Powershell: Loop over variablePowershell:循环变量
【发布时间】:2022-10-13 23:54:32
【问题描述】:

我正在尝试查询从一台服务器到多台远程服务器的端口,但我无法弄清楚。

查询一台服务器效果很好,但我想在 $destination 变量中添加多个远程服务器,并让脚本循环遍历它们以一次性给我所有结果。

我找到的脚本:

$Servers = "127.0.0.1"
$Ports   =  "80",
            "445",
            "443"

$Destination = "Computer_1"
$Results = @()
$Results = Invoke-Command $Servers {param($Destination,$Ports)
            $Object = New-Object PSCustomObject
            $Object | Add-Member -MemberType NoteProperty -Name "ServerName" -Value 
$env:COMPUTERNAME
            $Object | Add-Member -MemberType NoteProperty -Name "Destination" -Value $Destination
                Foreach ($P in $Ports){
                    $PortCheck = (Test-NetConnection -Port $p -ComputerName $Destination ).TcpTestSucceeded
                    If($PortCheck -notmatch "True|False"){$PortCheck = "ERROR"}
                    $Object | Add-Member Noteproperty "$("Port " + "$p")" -Value "$($PortCheck)"
                }
            $Object
       } -ArgumentList $Destination,$Ports | select * -ExcludeProperty runspaceid, pscomputername

$Results | Out-GridView -Title "Testing Ports"

$Results | Format-Table -AutoSize

我得到结果:

ServerName Destination   Port 80 Port 445 Port 443 
---------- -----------   -------- -------- -------- 
<local host> Computer_1    True     True     True    

我需要在 $destination 块中添加多个遥控器。我尝试了以下方法:

$Destination = "Computer_1","Computer_2"

但这不是正确的方法,因为我明白了:

ServerName Destination                     Port 80 Port 445 Port 443
---------- -----------                     -------- -------- -------- 
<local host> "Computer_1","Computer_2"     False    False    False      

我需要得到这个结果:

ServerName Destination   Port 80 Port 445 Port 443 
---------- -----------   -------- -------- -------- 
<local host> Computer_1    True     True     True  
<local host> Computer_2    True     True     True  

【问题讨论】:

    标签: powershell


    【解决方案1】:

    问题是您正在为$ports 运行ForEach() 而不是$destination。你想做更多这样的事情:

    $Servers = "127.0.0.1"
    $Ports   =  "80",
                "445",
                "443"
    
    $Destinations = "Computer_1","Computer_2"
    $Results = @()
    $Results = Invoke-Command $Servers {param($Destinations,$Ports)
        ForEach($Destination in Destinations){
            $Object = New-Object PSCustomObject
            $Object | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $env:COMPUTERNAME
            $Object | Add-Member -MemberType NoteProperty -Name "Destination" -Value $Destination
            Foreach ($P in $Ports){
                $PortCheck = (Test-NetConnection -Port $p -ComputerName $Destination ).TcpTestSucceeded
                If($PortCheck -notmatch "True|False"){$PortCheck = "ERROR"}
                $Object | Add-Member Noteproperty "$("Port " + "$p")" -Value "$($PortCheck)"
            }
            $Object
        }
    } -ArgumentList $Destinations,$Ports | select * -ExcludeProperty runspaceid, pscomputername
    
    $Results | Out-GridView -Title "Testing Ports"
    $Results | Format-Table -AutoSize
    

    虽然如果您更熟悉 Powershell,使用 Pipeline 将结果直接传递给 Out-GridView,而不是先将它们全部收集到一个数组中,会有一些更简洁的方法来格式化它。

    【讨论】:

    • 像魅力一样工作<3谢谢
    【解决方案2】:

    您在 $Destination 中传递了多个服务器,因此您需要遍历它们。相反,您只需将整个 $Destination 添加到 $Object

    在伪代码中,您需要类似

    $Results += foreach ($destServer in $ destination) {
       Invoke-Command {
       {same code you use currently}
       }
    }
    

    【讨论】:

    • 感谢您的提醒。
    猜你喜欢
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多