From 704473cdb9dde93e4ee54ae7fc65d3cf5ad868e4 Mon Sep 17 00:00:00 2001 From: Adam Bertram Date: Fri, 17 Mar 2017 11:28:19 -0500 Subject: [PATCH] Updated readme --- DNS/Test-DnsNameResolution.ps1 | 84 ++++++++++++++ Networking/Test-NetworkPort.ps1 | 196 +++++++++++++++++--------------- README.md | 15 +-- donate.png | Bin 0 -> 5532 bytes 4 files changed, 188 insertions(+), 107 deletions(-) create mode 100644 DNS/Test-DnsNameResolution.ps1 create mode 100644 donate.png diff --git a/DNS/Test-DnsNameResolution.ps1 b/DNS/Test-DnsNameResolution.ps1 new file mode 100644 index 0000000..a79c50c --- /dev/null +++ b/DNS/Test-DnsNameResolution.ps1 @@ -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 $_ + } +} \ No newline at end of file diff --git a/Networking/Test-NetworkPort.ps1 b/Networking/Test-NetworkPort.ps1 index 343079b..23f368e 100644 --- a/Networking/Test-NetworkPort.ps1 +++ b/Networking/Test-NetworkPort.ps1 @@ -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 } } \ No newline at end of file diff --git a/README.md b/README.md index b61bece..b5b2930 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/donate.png b/donate.png new file mode 100644 index 0000000000000000000000000000000000000000..b2e98596c7ec5f95f0a4ddc515dd379fcecf7682 GIT binary patch literal 5532 zcmd^Dd0bOh7LL_%p=#?^7pBt~2P+Pc6$pXY0!aiU7y$*bR3FKMM6!{*$fn?i1))k6 zl(nd=ipVO;q9C#;C@xSC!HQ66fuc?o5dq)atb$AJ{N~T$M@a6w=Y03vbI=7iSOJfYb2DWV*iu`VFR%-e!7f2guG}DBt_2VG)ha!! z02%-g!3cy65DCR{S^yoV!li-F%Ex#dR%L?t(s9etU#$x$0C4?G-0n52EffNx)#8@RJRGBt`1wOkO@`DJT4v~jg>P>>j}sp!Oj97J|frQPw)wVq$(Gg%H~Z^Hv7-LM4|~Ea>N;aPi)$C_2;3fjCov|w?rm_ z!08A?kPnQPihZ;bA_-66Er3CZg1R9a6G_G-f-8YaGo{c>NhV|x)ffy2>ePc{pveV6 zFdauGnUd59;1P!(C>L-cp}&BK@C9Oln9GMjo{hC9rgVb?5E;Zp-nkCN!h}RIe{&r^ zM5N$bKfO;v z)U#hL_dzUGcyFtT+G3)lXwbE~q8`2_E@Y*+5V2-vKwV7D%!nFw0m48qGbVx(F{6>p zwd$hA(8ck#&ZrIk3$b|z;=5HxW2j1>))Y}#5NZ~`$TWJtIQB{A$78dI>G2~WVI z*)j+eb1IQYq_W6N5_qGUlC23WBFoyG#iWo46VTp^!mwdcO{rw28I=gihe@UqOl_G& z3mXd?7MW$kU`#-<7t0Yy%!Mb!A^@>yP#7OZq1nn{2$9HKB@&@#L+VoI@Fj>u&X-8B zwk~Ga6>fmN5`Vd&dVO7`g3W{6Fkl&=K941nh_I?dqzT>^nW;6I#AMl02`n=zlMJ#1 z8m|qR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