简体   繁体   English

Powershell:循环变量

[英]Powershell: Loop over variable

I am trying to query ports from one server to multiple remote servers but i can't figure it out.我正在尝试从一台服务器查询端口到多台远程服务器,但我无法弄清楚。

Querying to one server wordks good but i want to add multiple remote servers in $destination variable and let the script loop over them to give me all the results in one shot.查询一台服务器的话很好,但我想在 $destination 变量中添加多个远程服务器,并让脚本循环遍历它们以一次性给我所有结果。

the script i found:我找到的脚本:

$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

I get the results:我得到结果:

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

I need to add multiple remote in the $destination block.我需要在 $destination 块中添加多个遥控器。 I have tried the following:我尝试了以下方法:

$Destination = "Computer_1","Computer_2"

But it is not the right way because i get this:但这不是正确的方法,因为我明白了:

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

i need to get this results:我需要得到这个结果:

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

Any help would be appreciated.任何帮助,将不胜感激。

The problem is that you're running a ForEach() for the $ports but not $destination .问题是您正在为$ports而不是$destination运行ForEach() You want to do something more like this:你想做更多这样的事情:

$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

Though if you were more familiar with Powershell, there would be slightly tidier ways of formatting this using the Pipeline to pass your results directly to Out-GridView, rather than collecting them all together into an array first.尽管如果您更熟悉 Powershell,那么使用管道将结果直接传递给 Out-GridView 会稍微更简洁一些,而不是先将它们全部收集到一个数组中。

You pass multiple servers in $Destination, so you need to loop though them.您在 $Destination 中传递了多个服务器,因此您需要遍历它们。 Instead you just add the whole $Destination in to the $Object相反,您只需将整个 $Destination 添加到 $Object

in pseudo code you need something like在伪代码中,您需要类似

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM