Skip to content

Commit

Permalink
Updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
adbertram committed Mar 17, 2017
1 parent 34b4359 commit 704473c
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 107 deletions.
84 changes: 84 additions & 0 deletions DNS/Test-DnsNameResolution.ps1
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 $_
}
}
196 changes: 102 additions & 94 deletions Networking/Test-NetworkPort.ps1
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
}
}
15 changes: 2 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
# Random PowerShell Work
[![licence badge]][licence]
[![stars badge]][stars]
[![forks badge]][forks]
[![issues badge]][issues]

[licence badge]:https://img.shields.io/badge/license-MIT-blue.svg
[stars badge]:https://img.shields.io/github/stars/adbertram/Random-PowerShell-Work.svg
[forks badge]:https://img.shields.io/github/forks/adbertram/Random-PowerShell-Work.svg
[issues badge]:https://img.shields.io/github/issues/adbertram/Random-PowerShell-Work.svg

[licence]:https://github.com/adbertram/Random-PowerShell-Work/blob/master/LICENSE.md
[stars]:https://github.com/adbertram/Random-PowerShell-Work/stargazers
[forks]:https://github.com/adbertram/Random-PowerShell-Work/network
[issues]:https://github.com/adbertram/Random-PowerShell-Work/issues
[![Donate](./donate.png)](https://www.paypal.me/adbertram/10)

> If you get some use out of my scripts, drop me a few bucks.
This is a conglomeration of PowerShell scripts that I've written over the years. If you'd like more PowerShell awesomeness check out my blog at [Adam, the Automator](http://www.adamtheautomator.com). This is where I write about automation, lots of PowerShell and post regularly about time-saving tricks with PowerShell.

Expand Down
Binary file added donate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 704473c

Please sign in to comment.