Saturday, June 12, 2021

Powershell script to collect hostname from ipaddress V2

Powershell script to collect hostname from ipaddress

##################################################

$ipaddress=Get-Content -Path C:\ry\ansible\ip2.txt
$ErrorActionPreference='silentlycontinue'
$results = @()

ForEach ($i in $ipaddress)
 {
  
$o=new-object psobject
$o | Add-Member -MemberType NoteProperty -Name hostname -Value ([System.Net.Dns]::GetHostByAddress($i).HostName) | select 

-ExpandProperty hostname
$f=$o | select -ExpandProperty hostname
$hItemDetails=[pscustomobject]@{
IP_address=$i
Hostname=$f
}
$results +=$hItemDetails
}

$results | Export-Csv C:\ry\ansible\machinenames.csv

#############################################################

No comments: