Using the Test-Connection in an enterprise environment, I often miss the -a parameter of the old ping command, that tells ping to try to resolve IP address to HostName.
So I sat down and created a proxy function for Test-Connection, adding a Resolve parameter. It’s quite simple actually, it’s checks the ComputerName parameter, and if the data is a valid IP address tries to resolve using the Net.Dns class. It then takes the resolved name and injects it back into the ComputerName parameter before calling the original Test-Connection command.
There is one drawback of using proxies with the same name as existing commands though, and that is that Get-Help with not work as you are used to. There is two ways of “fixing” this problem:
- Call Get-Help like this:
Get-Help Test-Connection -Category Function
- Add the following line to your PowerShell profile:
(Get-Command Microsoft.PowerShell.Management\Test-connection).Visibility = 'Private'
Code:
| function Test-Connection { | |
| [CmdletBinding(DefaultParameterSetName='Default', HelpUri='http://go.microsoft.com/fwlink/?LinkID=135266', RemotingCapability='OwnedByCommand')] | |
| param( | |
| [Parameter(ParameterSetName='Default')] | |
| [Parameter(ParameterSetName='Source')] | |
| [switch] | |
| ${AsJob}, | |
| [System.Management.AuthenticationLevel] | |
| ${Authentication}, | |
| [Alias('Size','Bytes','BS')] | |
| [ValidateRange(0, 65500)] | |
| [int] | |
| ${BufferSize}, | |
| [Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)] | |
| [Alias('CN','IPAddress','__SERVER','Server','Destination')] | |
| [ValidateNotNullOrEmpty()] | |
| [string[]] | |
| ${ComputerName}, | |
| [ValidateRange(1, 4294967295)] | |
| [int] | |
| ${Count}, | |
| [Parameter(ParameterSetName='Source')] | |
| [ValidateNotNullOrEmpty()] | |
| [pscredential] | |
| [System.Management.Automation.CredentialAttribute()] | |
| ${Credential}, | |
| [Parameter(ParameterSetName='Source', Mandatory=$true, Position=1)] | |
| [Alias('FCN','SRC')] | |
| [ValidateNotNullOrEmpty()] | |
| [string[]] | |
| ${Source}, | |
| [System.Management.ImpersonationLevel] | |
| ${Impersonation}, | |
| [Parameter(ParameterSetName='Source')] | |
| [Parameter(ParameterSetName='Default')] | |
| [ValidateRange(-2147483648, 1000)] | |
| [int] | |
| ${ThrottleLimit}, | |
| [Alias('TTL')] | |
| [ValidateRange(1, 255)] | |
| [int] | |
| ${TimeToLive}, | |
| [ValidateRange(1, 60)] | |
| [int] | |
| ${Delay}, | |
| [Parameter(ParameterSetName='Quiet')] | |
| [switch] | |
| ${Quiet}, | |
| [Parameter()] | |
| [switch] | |
| ${Resolve}) | |
| begin | |
| { | |
| try { | |
| $outBuffer = $null | |
| if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) | |
| { | |
| $PSBoundParameters['OutBuffer'] = 1 | |
| } | |
| if ($PSBoundParameters['Resolve']) { | |
| $null = $PSBoundParameters.Remove('Resolve') | |
| foreach ($item in $ComputerName) { | |
| try { | |
| $item -match [IPAddress]$item | |
| [string[]]$ComputerNameEx += ([System.Net.Dns]::GetHostEntry($item)).HostName | |
| } | |
| catch { | |
| [string[]]$ComputerNameEx += $item | |
| } | |
| } | |
| $PSBoundParameters['ComputerName'] = $ComputerNameEx | |
| } | |
| $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Test-Connection', [System.Management.Automation.CommandTypes]::Cmdlet) | |
| $scriptCmd = {& $wrappedCmd @PSBoundParameters } | |
| $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin) | |
| $steppablePipeline.Begin($PSCmdlet) | |
| } catch { | |
| throw | |
| } | |
| } | |
| process | |
| { | |
| try { | |
| $steppablePipeline.Process($_) | |
| } catch { | |
| throw | |
| } | |
| } | |
| end | |
| { | |
| try { | |
| $steppablePipeline.End() | |
| } catch { | |
| throw | |
| } | |
| } | |
| <# | |
| .ForwardHelpTargetName Test-Connection | |
| .ForwardHelpCategory Cmdlet | |
| #> | |
| } |