LewisFletch
LewisFletch

Reputation: 125

test-connection output to csv file with hostname and IPaddress

Currently i have the below script to import from a csv file with a few columns. the ones i am interested in are Hostname and Ipaddress. Ipaddress = ip address of servers Hostname - hostnames of servers

basically what i am aiming to do is an audit of currently active servers in our CMDB (lot of old junk that hasn't been properly removed)

$servers = import-csv 'config-network devicetest.csv'
$collection = $()
foreach ( $IPAddress in $servers)
{
$status = @{ "ServerName" = $Hostname; "TimeStamp" = (Get-Date -f s) }
if (Test-Connection -IPAddress $IPAddress -Count 2 -ea 0 -quiet)
{ 
    $status["Results"] = "Active"
} 
else 
{ 
    $status["Results"] = "Inactive" 
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus

}
$collection | Export-Csv .\ServerStatus.csv -NoTypeInformation

My goal is to run test-connection on each Ip address and output to a file that has timestamp/results/servername (being Hostname).

currently i am always getting the result "inactive" and i think this is due to me not calling the proper fields from the csv. i'm fairly new to importing from csv so any help with how to properly import and export specific columns would be very helpful. I've done a bit of a search on the web but nothing that really explains it clearly or in this context.

Thank you in advance.

Upvotes: 1

Views: 5418

Answers (1)

walid toumi
walid toumi

Reputation: 2282

Try to change these lines:

$status = @{ "ServerName" = $IPAddress.Hostname; "TimeStamp" = (Get-Date -f s); "Results"="" }
...
....
(Test-Connection -IPAddress $IPAddress.ipaddress -Count 2 -ea 0 -quiet)
...
..

.

Upvotes: 2

Related Questions