Skip to content

Test-PSOnePing: System.Net.Networkinformation.Ping details #1

Description

@scriptingstudio

Method .send() takes time to resolve hostname if you pass not ipaddress. So if you pass a dead name you will get an exception.
I would suggest this solution:

if (-not [bool]($ComputerName -as [ipaddress])) {
	try {
		$result = [System.Net.Dns]::BeginGetHostByName($ComputerName, $null, $null)
		$result = if ($result.AsyncWaitHandle.WaitOne($timeout)) { [System.Net.Dns]::EndGetHostByName($result) }
		if (-not $result) {return $false}
		$ComputerName = $result.AddressList.IPAddressToString
	} catch {return $false}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions