I pinged the servers and it is working (it shows me the ip address that could be pinged) but I want it to show the hostname and the ip address. I tried to incorporate [System.Net.Data.Dns]::GetHostName(), but I don't know where to put it. I am a beginner using PowerShell. I also tried to used -and, but it doesn't work.
I understand how to do it python I just don't know how to translate it to PowerShell.

$columnC = "n1-1mon-i3fp04","n1-1mon-i3fp06","n1-1mon-i3fp07","n1-r-1mon-i3fp09","n1-r-1mon-i3fp10","n1-1mon-i3fp08","n1-1mon-i3fp03","n1-1mon-i3fp02","n1-1mon-i3fp111"
$columnC | % $_ {$Device = Resolve-DnsName -Name $_ 
  if (test-connection $Device.("IPAddress")) {write-host Device.("IPAddress") "Ping succeeded." -foreground green} 
   else {write-host $Device.("IPAddress") "Ping failed." -foreground red}}

The result shows an error message like the syntax is wrong. I want it to show both ip address and the hostname.

1 Answers

Lee_Dailey On

[edit - Theo pointed out that GetHostByName has been deprecated in favor of GetHostEntry. when i tested that, it gave more consistent results, so i swapped them.]

this will get the ComputerName, HostName, and Online status. then save those into a custom object, send the object to the $Results collection, and - finally - show what is in the collection. [grin]

# fake reading in a text file
#    in real life use Get-Content
$ComputerList = @'
'@ -split [environment]::NewLine

$Results = foreach ($CL_Item in $ComputerList)
        $HostName = [System.Net.Dns]::GetHostEntry($CL_Item).HostName 
        $HostName = '__Not Found__'
        ComputerName = $CL_Item
        HostName = $HostName
        Online = Test-Connection -ComputerName $CL_Item -Count 1 -Quiet


output ...

ComputerName     HostName      Online
------------     --------      ------
BetterNotBeThere __Not Found__  False
LocalHost        [MySysName]    True         __Not Found__  False        [MySysName]    True