cancel
Showing results for 
Search instead for 
Did you mean: 
Create Post
Level 12

powershell - Get IPAddress from Hostname

https://gallery.technet.microsoft.com/scriptcenter/Get-IPAddress-from-Hostname-e8c23e1f

$array= @() 
$computer=Get-Content c:\computers.txt 
foreach ($server in $computer) 

if (Test-Connection $server -Quiet) 

try { 
$IP=[System.net.dns]::GetHostEntry($server).AddressList | %{$_.IPAddressToString} 

catch {"Invalid HostName -  $server"} 
 
$obj = New-Object PSObject -Property @{ 
        Hostname=$server 
        IP=$IP 
        } 
$array += $obj 

else { 
$IP="Invalid Host" 
$obj = New-Object PSObject -Property @{ 
    Hostname=$server 
    IP=$IP 
    } 
 $array += $obj 


$array | Export-Csv C:\Result.csv

0 Kudos
5 Replies

Here's my script to import a list of IPs from a CSV file, perform a reverse lookup, and add the hostname to the CSV:

$csv = "IPAddresses.csv" #The key colums in this example are 'SourceIP' and 'HostName'
$addresses = Import-Csv -Path $csv

$addresses | foreach-object -parallel {
    $ip = $_.SourceIP
    $_.Hostname = ([System.Net.Dns]::GetHostEntry($ip)).HostName
}

$addresses | Export-Csv -Path $csv

Yes? Is there an Orion question, or just sharing a useful script?

Fun side note! I wrote a similar script myself recently, and found that I could make it run faster when using PowerShell 7 by making use of $array | foreach-object -parallel so it could perform multiple lookups simultaneously.

Thanks @sturdyerde I found it useful so saved it here for my future use.

Meanwhile I have started exploring OrionSDK, and trying to use verb in SWQL verb in script.

Can you help me to add multiple nodes in NCM using script, we can do one at a time using in SWQL Studio:

 

prashantsingh_1-1596533466727.png

 

0 Kudos

@prashantsingh Here's what I was able to get working real quick for this.

 

 

import-module SwisPowershell
#define your swis connection settings, if running locally on a SolarWinds Polling Engine you can leave the certificate flag, if running remotely modify the hostname and provide it credentials
$swis = Connect-Swis -Host localhost -Certificate

# define your nodes you want to add via a query and grab the nodeid for them
$nodes = Get-SwisData $swis -query "select n.NodeID, n.caption from orion.nodes n
where n.Nodeid ="

#go through each node found in query above and invoke verb to add to NCM
foreach($node in $nodes){
Write-Output "Adding Node $($node.caption)' to NCM"
Invoke-SwisVerb $swis Cirrus.Nodes AddNodeToNCM @($node.nodeid)
sleep -m 200
}
Write-Output "Script Complete. Exiting..."

 

Thank you so much @christopher.t.jones123 😊

 

0 Kudos