测试连接输出到具有主机名和IP地址的csv文件 [英] test-connection output to csv file with hostname and IPaddress

查看:123
本文介绍了测试连接输出到具有主机名和IP地址的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有以下脚本要从带有几列的csv文件中导入.我感兴趣的是主机名和IP地址. ipaddress =服务器的ip地址 主机名-服务器的主机名

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

基本上,我的目标是对CMDB中当前处于活动状态的服务器进行审核(很多旧垃圾未正确清除)

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

我的目标是在每个IP地址上运行测试连接,并输出到具有时间戳记/结果/服务器名(即主机名)的文件.

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

当前,我总是得到结果无效",我认为这是由于我没有从csv调用正确的字段. 我对于从csv导入还很陌生,因此,如何正确导入和导出特定列的任何帮助都将非常有帮助. 我已经在网上做了一些搜索,但是没有什么能真正清楚地说明或在这种情况下进行解释.

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.

谢谢.

推荐答案

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)
...
..

.

这篇关于测试连接输出到具有主机名和IP地址的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆