#Requires -version 2.0 <# Script: Test-Port.ps1 Version: 2.0 Author: Jeffery Hicks http://jdhitsolutions.com/blog http://twitter.com/JeffHicks http://www.ScriptingGeek.com Date: 5/31/2011 Keywords: Comments: A port scanning function that can be used in the pipeline. The underlying .NET classes have useful information. The original version of this script is from http://theadminguy.wordpress.com/2009/04/30/portscan-with-powershell/ "Those who forget to script are doomed to repeat their work." **************************************************************** * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * **************************************************************** #> Function Test-Port { <# .SYNOPSIS Test for an open port .DESCRIPTION This function uses the .NET framework to test if a given port is open on a computer. It will return a custom object to the pipeline. .PARAMETER Computername The name of the computer to test. The default is the localhost. .PARAMETER Ports An array of ports to test. The default is 21,22,23,80,443 and 3389 .PARAMETER WhatIf Simulate what would happen if you ran the function .PARAMETER Confirm Confirm that you want to test the ports .EXAMPLE PS C:\> Test-Port jdhit-dc01 -ports 21,80,389,3389 Port : 21 Open : False TTL : -1 Computername : JDHIT-DC01 RemoteIP : Port : 80 Open : True TTL : 128 Computername : JDHIT-DC01 RemoteIP : 172.16.10.1 Port : 389 Open : True TTL : 128 Computername : JDHIT-DC01 RemoteIP : 172.16.10.1 Port : 3389 Open : True TTL : 128 Computername : JDHIT-DC01 RemoteIP : 172.16.10.1 Scan the computer for the specified ports. .EXAMPLE PS C:\> get-content computers.txt | Test-Port -port 80 | Where {$_.Open} | Out-File Open80.txt Scan all the computers in computers.txt for port 80 and save the results for computers listening on port 80 to a text file. .NOTES NAME : Test-Port VERSION : 2.0 LAST UPDATED: 5/31/2011 AUTHOR : SERENITY\Jeff .LINK http://jdhitsolutions.com/blog/2011/05/test-port-2-0/ .LINK Test-Connection .INPUTS Strings .OUTPUTS Custom object #> [cmdletbinding(SupportsShouldProcess=$True,ConfirmImpact="Low")] Param( [Parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] [ValidateNotNullorEmpty()] [string[]]$Computername=$env:computername, [array]$Ports=@("21","22","23","25","80","443","3389") ) Begin { #set values for Write-Progress Write-Verbose "$(Get-Date) Starting $($myinvocation.mycommand)" Write-Verbose "$(Get-Date) Scannning for ports $($ports -as [string])" $activity="Port Scan" } Process { Foreach ($computer in $computername) { #whatif if ($pscmdlet.ShouldProcess($($computer.ToUpper()))) { $status="Scanning $computer" Write-Verbose "$(Get-Date) $status" $i=0 foreach ($port in $ports) { $i++ Write-Progress -Activity $activity -status $status ` -currentoperation "port $port" -percentcomplete (($i/$ports.count)*100) Try { #create the TCPClient object $tcp=New-Object System.Net.Sockets.TcpClient($computer, $port) -ErrorAction Stop } Catch { Write-Verbose "$(Get-Date) Connection refused" } if ($tcp.client.connected) { [string]$rep=$tcp.client.RemoteEndPoint [string]$ip=$rep.substring(0,$rep.indexof(":")) $PortOpen=$True $TTL=$($tcp.client.ttl) $RemoteIP=$ip } else { Write-Verbose "$(Get-Date) $($computer.ToUpper()) not open on port: $port" $PortOpen=$False $TTL=-1 $RemoteIP=$Null } #end Else #disconnect the socket connection if open if ($PortOpen) { Write-Verbose "$(Get-Date) Disconnecting from $($computer.ToUpper())" $tcp.client.disconnect($False) } #write a custom object to the pipeline New-Object -TypeName PSObject -Property @{ Computername=$Computer.ToUpper() Port=$Port Open=$PortOpen TTL=$TTL RemoteIP=$RemoteIP } } #end foreach $port #dispose and disconnect if ($tcp) { $tcp.close() } } #if shouldprocess } #foreach $computer } #end process End { Write-Progress -Activity $activity -status "Complete" -Completed Write-Verbose "$(Get-Date) Ending $($myinvocation.mycommand)" }#end } #end function #Set an optional alias #Set-Alias -Name tp -Value Test-Port