I am trying to create the script which will resolve the IP address to DNS name and then ping output computer number:
$name = Read-Host "Please enter IP address"
$comp = Resolve-DnsName $name | select NameHost -First 1
The output is
NameHost
COMPPL01-NB0128.domain.com ```
When I try to format the output, it gives me
@{NameHost=COMPPL01-NB0128.domain.com}
So then I can't ping the result using the command test connection
Test-Connection -ComputerName $comp
As I understood, test-connection cmdl is trying to ping the whole string NameHost=COMPPL01-NB0128.domain.com
How can I select only the computer name from that output and ping it? So far it gives me an error message:
Test-Connection : Testing connection to computer '@{NameHost=COMPPL01-NB0128.domain.com}' failed: A non-recoverable error occurred during a database lookup At line:1 char:1
Test-Connection -ComputerName $comp
CategoryInfo : ResourceUnavailable: (@{NameHost=COMPPL01-NB0128.domain.com}:String) [Test-Connection], PingException
- FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
CodePudding user response:
There are two options:
Resolve-DnsName 8.8.8.8 | Select-Object -ExpandProperty NameHost
You can use Expand property to get host name value alone.(Resolve-DnsName 8.8.8.8).NameHost
Using the Dot operator to get its properties.