PowerShell query server two ports

Hi Guys,

I would like to share a useful script that may help you in some scenarios of testing server’s port, in my case you can see that i am querying 443 and 80 from C:\Servers.txt file.

I am going to use on Test-NetConnection command to get this info, for more information:
https://docs.microsoft.com/en-us/powershell/module/nettcpip/test-netconnection?view=win10-ps

 

$Servers = get-content C:\Servers.txt
$Ports = "443", "80"
$Results = @()
foreach ($server in $Servers) 
{
$Object = New-Object PSCustomObject 
$Object | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $env:COMPUTERNAME
$Object | Add-Member -MemberType NoteProperty -Name "Destination" -Value $server
foreach ($port in $ports) 
{ 
$Portcheck = (Test-NetConnection -Port $port -ComputerName $server).TcpTestSucceeded
If ($PortCheck -notmatch "True|False") {$PortCheck = "ERROR"}
$Object | Add-Member Noteproperty "$("Port " + "$port")" -Value "$($PortCheck)" 
} 
$results = $results + $Object 
}#end foreach
$results | export-csv C:\ServerDetails.csv

Leave a Comment

Your email address will not be published.

Comment moderation is enabled. Your comment may take some time to appear.

In the news
Load More