Home > Software engineering >  How to get the IP address for multiple hostnames and export the result to a CSV file using PowerShel
How to get the IP address for multiple hostnames and export the result to a CSV file using PowerShel

Time:08-05

I have a PowerShell script that loop through a list of hostnames in text file and find the corresponding IP then store the result in a csv file. The issue that I'm having is when the host name does not exist the scripts stops. My goal is to either skip the unidentified hostname or write n/a instead.

I tried to use an "if" function or use "continue" but I'm not experience so I was not able to do that. Is there an easy way to resolve the issue?

$Servers = Get-Content -Path "./complist.txt"
$Array = @()
 
Foreach($Server in $Servers)
{
    $DNSCheck = $null
    $Server = $Server.trim()
 
    $DNSCheck = ([System.Net.Dns]::GetHostByName(("$Server")))

    if ($DNSCheck -ne $Null)
 {
    $Object = New-Object PSObject -Property ([ordered]@{ 
      
                "Server name"             = $Server
                #"FQDN"                    = $DNSCheck.hostname
                "IP Address"              = $DNSCheck.AddressList[0]

 
    })
   
    # Add object to our array
    $Array  = $Object
}

 
}
$Array
$Array | Export-Csv -Path "./pingstatus.csv" -NoTypeInformation 

CodePudding user response:

GetHostByName has been deprecated. Use GetHostEntry instead.

Also, adding objects to an array with = is very time and memory consuming as the entire array needs to be rebuilt in memory each time.

Without changing too much of your code, I'd suggest wrapping it in a try{..} catch{..} like

$Servers = Get-Content -Path "./complist.txt"
$Array = foreach($Server in $Servers) {
    $Server = $Server.Trim()
    try {
        $DNSCheck = [System.Net.Dns]::GetHostEntry($Server)
        # just output an object so it gets collected in variable $Array
        [PsCustomObject]@{ 
            'Server name' = $Server  # or $DNSCheck.HostName
            'IP Address'  = ($DNSCheck.AddressList | Where-Object {$_.AddressFamily -eq 'InterNetwork'})[0].IPAddressToString
        }
    }
    catch {
        # either do nothing here to skip this server of output a 'n/a' object
        [PsCustomObject]@{ 
            'Server name' = $Server
            'IP Address'  = 'n/a'
        }
    }
}

# output on screen
$Array | Format-Table -AutoSize

# write to csv
$Array | Export-Csv -Path "./pingstatus.csv" -NoTypeInformation 

or use the Test-Connection cmdlet:

$Servers = Get-Content -Path "./complist.txt"
$array = foreach($Server in $Servers) {
    $Server = $Server.Trim()
    try {
        $conn = Test-Connection -ComputerName $Server -Count 1 -ErrorAction Stop
        $conn | Select-Object @{Name = 'Server name'; Expression = {$_.PSComputerName}},
                              @{Name = 'IP Address'; Expression = {$_.IPV4Address}}
    }
    catch {
        # either do nothing here to skip this server of output a 'n/a' object
        [PsCustomObject]@{ 
            'Server name' = $Server
            'IP Address'  = 'n/a'
        }
    }
}

# output on screen
$Array | Format-Table -AutoSize

# write to csv
$Array | Export-Csv -Path "./pingstatus.csv" -NoTypeInformation 
  • Related