Powershell:循环变量
Powershell: Loop over variable
我正在尝试查询从一台服务器到多台远程服务器的端口,但我无法弄明白。
查询一台服务器的话还不错,但我想在 $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
如有任何帮助,我们将不胜感激。
您在 $Destination 中传递了多个服务器,因此您需要遍历它们。相反,您只需将整个 $Destination 添加到 $Object
在伪代码中你需要像
$Results += foreach ($destServer in $ destination) {
Invoke-Command {
{same code you use currently}
}
}
问题是您 运行 是 $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,使用管道将结果直接传递给 Out-GridView 的格式化方法会稍微简洁一些,而不是先将它们全部收集到一个数组中。
我正在尝试查询从一台服务器到多台远程服务器的端口,但我无法弄明白。
查询一台服务器的话还不错,但我想在 $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
如有任何帮助,我们将不胜感激。
您在 $Destination 中传递了多个服务器,因此您需要遍历它们。相反,您只需将整个 $Destination 添加到 $Object
在伪代码中你需要像
$Results += foreach ($destServer in $ destination) {
Invoke-Command {
{same code you use currently}
}
}
问题是您 运行 是 $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,使用管道将结果直接传递给 Out-GridView 的格式化方法会稍微简洁一些,而不是先将它们全部收集到一个数组中。