Friday, June 11, 2021

Powershell script to collect gateway ip of remote window system

Powershell script to collect gateway ip of remote window system

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

$ipaddress=Get-Content -Path C:\ry\ansible\ip.txt

$results = @()

ForEach ($i in $ipaddress)
 {
$sess = New-PSSession -ComputerName $i
$o=Invoke-Command -Session $sess -ScriptBlock {hostname}
$g=Invoke-Command -Session $sess -ScriptBlock {Get-NetIPConfiguration |Select -ExpandProperty IPv4DefaultGateway | select -ExpandProperty NextHop}
$hItemDetails=[pscustomobject]@{
Hostname=$o
Gateway=$g
}
$results +=$hItemDetails
}

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

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

No comments: