miércoles, 21 de julio de 2021

PowerShell: comprobar si un puerto está abierto



Cómo comprobar si un puerto está abierto usando PowerShell.



Para comprobar si un puerto está abierto, podemos usar el siguiente script de PowerShell:

Function Test-PortConnection { [CmdletBinding()] # Parameters used in this function Param ( [Parameter(Position=0, Mandatory = $True, HelpMessage="Provide destination source", ValueFromPipeline = $true)] $Destination, [Parameter(Position=1, Mandatory = $False, HelpMessage="Provide port numbers", ValueFromPipeline = $true)] $Ports = "80" ) $ErrorActionPreference = "SilentlyContinue" $Results = @() ForEach($D in $Destination) { # Create a custom object $Object = New-Object PSCustomObject $Object | Add-Member -MemberType NoteProperty -Name "Destination" -Value $D Write-Verbose "Checking $D" -ForegroundColor Yellow ForEach ($P in $Ports) { $Result = (Test-NetConnection -Port $p -ComputerName $D ).PingSucceeded If(!$Result) { $Status = "Failure" } Else { $Status = "Success" } $Object | Add-Member Noteproperty "$("Port " + "$p")" -Value "$($status)" } $Results += $Object # or easier way true/false value # # ForEach ($P in $Ports) # { # $Result = $null # $Result = Test-NetConnection -Port $p -ComputerName $D # $Object | Add-Member Noteproperty "$("Port " + "$p")" -Value "$($Result)" # } # # $Results += $Object # } # Final results displayed in new pop-up window If($Results) { $Results } }

Tras pegar el script en la consola de PowerShell, podemos usarlo de la siguiente manera:

Test-NetConnection -ComputerName hostname -Port 443

Dodne hostname es el hostname de la máquina y 443 el puerto a comprobar.
0

0 comentarios:

Publicar un comentario