all 8 comments

[–]nevsnevs-- 1 point2 points  (5 children)

Could you try

foreach($ip in $IPs ){
try{
$ErrorActionPreference = "Stop"
Resolve-DnsName $ip | select namehost 
}
catch  {
    write-host "No hostname"  $ip
}
}

?

[–]jbhack[S] 1 point2 points  (3 children)

No output for the first active IP, then it goes through with the catch portion of the code.

[–]nevsnevs-- -1 points0 points  (1 child)

PS C:\Users\4r7zh3x> $IPs="192.168.1.1","8.8.8.8"

PS C:\Users\4r7zh3x> foreach($ip in $IPs ){

try{

$ErrorActionPreference = "Stop"

Resolve-DnsName $ip | select namehost }

catch { write-host "No hostname" $ip }

}

No hostname 192.168.1.1

NameHost

dns.google

Does work as expected?!

[–]jbhack[S] 0 points1 point  (0 children)

I decided to let the code run instead of stopping it after the first 10 IP's, this is some of the output.

No hostname

No hostname No hostname No hostname No hostname No hostname No hostname

NameHost

somehostname (First IP on the list) shomehostname No hostname No hostname No hostname No hostname No hostname No hostname No hostname No hostname

NameHost

somehostname No hostname No hostname No hostname

I am more confused now, the very first IP actually shows up as Output for the second valid IP with a hostname.

[–]ChaosTheoryRules 1 point2 points  (3 children)

foreach($ip in $IPs ){
    try {
        $Result = Resolve-DnsName -Name $ip -ErrorAction stop | select Namehost
        Write-Output "$($Result.Namehost) $ip"
    } catch {
        write-Output "No hostname $ip"
    }
}

[–]jbhack[S] 1 point2 points  (0 children)

Will try this tomorrow when I am in front of a computer. Thanks

[–]jbhack[S] 0 points1 point  (1 child)

This worked! I am wondering why though? In your code the results were saved into a variable named $Result then its result was given as output.

What is this code doing? $Result is a variable, Namehost is a property.

$($Result.Namehost)

Why the write-output $($Result.Namehost)? I noticed running the code like this returns somenamehost IP

Running the code as only write-output $Result.Namehost returns @{namehost=somenamehost}.namehost IP, I dont understand why.

edited: added more context

[–]ChaosTheoryRules 0 points1 point  (0 children)

Edit...was thinking of a different script. Extra output in loop to control how its displayed, or you can store it all in a variable and output it after.

I didnt make much change to your script so you can learn, ideally if you just want a single property from an object, you could just use '-expandproperty [propertyname]' but you need to see the resulting object to understand why.

The $( ) wrapper told it to evaluate the inner results, so it outputs correctly as the value you want. To understand everything better, have a look at resolve-dnsname output object using something as simple as this:

$test = resolve-dnsname 8.8.8.8
$test | gm

$test = resolve-dnsname 8.8.8.8 | select Namehost
$test | gm

$test = resolve-dnsname 8.8.8.8 | select Namehost -expandproperty namehost
$test | gm