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 $_ 
    $Device.[System.Net.Data.Dns]::GetHostName()
  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

0
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 = @'
BetterNotBeThere
LocalHost
10.0.0.1
127.0.0.1
'@ -split [environment]::NewLine

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

$Results

output ...

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