【发布时间】: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