Skip to content

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

@scriptingstudio

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}
}

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