forked from adbertram/Random-PowerShell-Work
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
188 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
|
||
<#PSScriptInfo | ||
.VERSION 1.1 | ||
.GUID e2603db1-b42d-4456-84f8-6031a8d247fd | ||
.AUTHOR Adam Bertram | ||
.COMPANYNAME Adam the Automator, LLC | ||
.COPYRIGHT | ||
.TAGS | ||
.LICENSEURI | ||
.PROJECTURI | ||
.ICONURI | ||
.EXTERNALMODULEDEPENDENCIES | ||
.REQUIREDSCRIPTS | ||
.EXTERNALSCRIPTDEPENDENCIES | ||
.RELEASENOTES | ||
#> | ||
|
||
<# | ||
.DESCRIPTION | ||
A script to test if a DNS name can be resolved. | ||
#> | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory)] | ||
[ValidateNotNullOrEmpty()] | ||
[string]$Name, | ||
|
||
[Parameter(Mandatory)] | ||
[ValidateNotNullOrEmpty()] | ||
[string]$Server | ||
) | ||
|
||
$resolvParams = @{ | ||
'Server' = $Server | ||
'DnsOnly' = $true | ||
'NoHostsFile' = $true | ||
'ErrorAction' = 'SilentlyContinue' | ||
'ErrorVariable' = 'err' | ||
'Name' = $Name | ||
} | ||
try | ||
{ | ||
if (Resolve-DnsName @resolvParams) | ||
{ | ||
$true | ||
} | ||
elseif ($err -and ($err.Exception.Message -match '(DNS name does not exist)|(No such host is known)')) | ||
{ | ||
$false | ||
} | ||
else | ||
{ | ||
throw $err | ||
} | ||
} | ||
catch | ||
{ | ||
if ($_.Exception.Message -match 'No such host is known') | ||
{ | ||
$false | ||
} | ||
else | ||
{ | ||
throw $_ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,115 @@ | ||
function Test-NetworkPort | ||
{ | ||
<# | ||
.SYNOPSIS | ||
This function tests for open TCP/UDP ports. | ||
.DESCRIPTION | ||
This function tests any TCP/UDP port to see if it's open or closed. | ||
.NOTES | ||
Known Issue: If this function is called within 10-20 consecutively on the same port | ||
and computer, the UDP port check will output $false when it can be | ||
$true. I haven't figured out why it does this. | ||
.PARAMETER Computername | ||
One or more remote, comma-separated computer names | ||
.PARAMETER Port | ||
One or more comma-separated port numbers you'd like to test. | ||
.PARAMETER Protocol | ||
The protocol (UDP or TCP) that you'll be testing | ||
.PARAMETER TcpTimeout | ||
The number of milliseconds that the function will wait until declaring | ||
the TCP port closed. | ||
.PARAMETER | ||
The number of millieconds that the function will wait until declaring | ||
the UDP port closed. | ||
.EXAMPLE | ||
PS> Test-Port -Computername 'LABDC','LABDC2' -Protocol TCP 80,443 | ||
This example tests the TCP network ports 80 and 443 on both the LABDC | ||
and LABDC2 servers. | ||
|
||
<#PSScriptInfo | ||
.VERSION 1.0 | ||
.GUID ee346973-6b67-4099-9191-cbcc169cf360 | ||
.AUTHOR Adam Bertram | ||
.COMPANYNAME Adam the Automator, LLC | ||
.COPYRIGHT | ||
.TAGS | ||
.LICENSEURI | ||
.PROJECTURI | ||
.ICONURI | ||
.EXTERNALMODULEDEPENDENCIES | ||
.REQUIREDSCRIPTS | ||
.EXTERNALSCRIPTDEPENDENCIES | ||
.RELEASENOTES | ||
#> | ||
[CmdletBinding(DefaultParameterSetName = 'TCP')] | ||
[OutputType([System.Management.Automation.PSCustomObject])] | ||
param ( | ||
[Parameter(Mandatory)] | ||
[string]$ComputerName, | ||
|
||
[Parameter(Mandatory)] | ||
[int]$Port, | ||
|
||
[Parameter()] | ||
[ValidateSet('TCP', 'UDP')] | ||
[string]$Protocol = 'TCP', | ||
|
||
[Parameter(ParameterSetName = 'TCP')] | ||
[int]$TcpTimeout = 1000, | ||
|
||
[Parameter(ParameterSetName = 'UDP')] | ||
[int]$UdpTimeout = 1000 | ||
) | ||
process | ||
|
||
<# | ||
.DESCRIPTION | ||
A script to test if a TCP or UDP port is open. | ||
#> | ||
[CmdletBinding(DefaultParameterSetName = 'TCP')] | ||
[OutputType([System.Management.Automation.PSCustomObject])] | ||
param ( | ||
[Parameter(Mandatory)] | ||
[string]$ComputerName, | ||
|
||
[Parameter(Mandatory)] | ||
[int]$Port, | ||
|
||
[Parameter()] | ||
[ValidateSet('TCP', 'UDP')] | ||
[string]$Protocol = 'TCP', | ||
|
||
[Parameter(ParameterSetName = 'TCP')] | ||
[int]$TcpTimeout = 1000, | ||
|
||
[Parameter(ParameterSetName = 'UDP')] | ||
[int]$UdpTimeout = 1000 | ||
) | ||
process | ||
{ | ||
if ($Protocol -eq 'TCP') | ||
{ | ||
if ($Protocol -eq 'TCP') | ||
$TcpClient = New-Object System.Net.Sockets.TcpClient | ||
$Connect = $TcpClient.BeginConnect($ComputerName, $Port, $null, $null) | ||
$Wait = $Connect.AsyncWaitHandle.WaitOne($TcpTimeout, $false) | ||
if (!$Wait) | ||
{ | ||
$TcpClient = New-Object System.Net.Sockets.TcpClient | ||
$Connect = $TcpClient.BeginConnect($ComputerName, $Port, $null, $null) | ||
$Wait = $Connect.AsyncWaitHandle.WaitOne($TcpTimeout, $false) | ||
if (!$Wait) | ||
{ | ||
$TcpClient.Close() | ||
} | ||
else | ||
{ | ||
$TcpClient.EndConnect($Connect) | ||
$TcpClient.Close() | ||
$result = $true | ||
} | ||
$TcpClient.Close() | ||
$TcpClient.Dispose() | ||
} | ||
elseif ($Protocol -eq 'UDP') | ||
else | ||
{ | ||
$UdpClient = New-Object System.Net.Sockets.UdpClient | ||
$UdpClient.Client.ReceiveTimeout = $UdpTimeout | ||
$UdpClient.Connect($ComputerName, $Port) | ||
$a = new-object system.text.asciiencoding | ||
$byte = $a.GetBytes("$(Get-Date)") | ||
[void]$UdpClient.Send($byte, $byte.length) | ||
#IPEndPoint object will allow us to read datagrams sent from any source. | ||
Write-Verbose "$($MyInvocation.MyCommand.Name) - Creating remote endpoint" | ||
$remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any, 0) | ||
try | ||
{ | ||
#Blocks until a message returns on this socket from a remote host. | ||
Write-Verbose "$($MyInvocation.MyCommand.Name) - Waiting for message return" | ||
$receivebytes = $UdpClient.Receive([ref]$remoteendpoint) | ||
[string]$returndata = $a.GetString($receivebytes) | ||
If ($returndata) | ||
{ | ||
$result = $true | ||
} | ||
} | ||
catch | ||
{ | ||
Write-Verbose "$($MyInvocation.MyCommand.Name) - '$ComputerName' failed port test on port '$Protocol`:$Port' with error '$($_.Exception.Message)'" | ||
} | ||
$UdpClient.Close() | ||
$UdpClient.Dispose() | ||
$TcpClient.EndConnect($Connect) | ||
$TcpClient.Close() | ||
$result = $true | ||
} | ||
if ($result) | ||
$TcpClient.Close() | ||
$TcpClient.Dispose() | ||
} | ||
elseif ($Protocol -eq 'UDP') | ||
{ | ||
$UdpClient = New-Object System.Net.Sockets.UdpClient | ||
$UdpClient.Client.ReceiveTimeout = $UdpTimeout | ||
$UdpClient.Connect($ComputerName, $Port) | ||
$a = new-object system.text.asciiencoding | ||
$byte = $a.GetBytes("$(Get-Date)") | ||
[void]$UdpClient.Send($byte, $byte.length) | ||
#IPEndPoint object will allow us to read datagrams sent from any source. | ||
Write-Verbose "$($MyInvocation.MyCommand.Name) - Creating remote endpoint" | ||
$remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any, 0) | ||
try | ||
{ | ||
$true | ||
#Blocks until a message returns on this socket from a remote host. | ||
Write-Verbose "$($MyInvocation.MyCommand.Name) - Waiting for message return" | ||
$receivebytes = $UdpClient.Receive([ref]$remoteendpoint) | ||
[string]$returndata = $a.GetString($receivebytes) | ||
If ($returndata) | ||
{ | ||
$result = $true | ||
} | ||
} | ||
else | ||
catch | ||
{ | ||
$false | ||
Write-Error "$($MyInvocation.MyCommand.Name) - '$ComputerName' failed port test on port '$Protocol`:$Port' with error '$($_.Exception.Message)'" | ||
} | ||
$UdpClient.Close() | ||
$UdpClient.Dispose() | ||
} | ||
if ($result) | ||
{ | ||
$true | ||
} | ||
else | ||
{ | ||
$false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.