我是PowerShell脚本的新手,需要帮助。
我有一个带有服务器IPadd的txt文件,以下脚本调用该txt文件,并以CSV格式显示服务器状态为UP / DOWN。我需要获得CSV文件结果角色库,如果我在txt中添加“域 Controller ”行并提及IP地址,则得到的结果“域 Controller 关闭”和“IPAdd”具有正确的状态。我也可以提及IPadd或主机名。需要您的帮助以获取带有主机名,IP地址和带有标题(角色)的状态的CSV报告
脚本:
$Servers = Get-Content D:\test\ServerStatus.txt
$collection = $()
foreach ($server in $servers)
{
$status = @{ "ServerName" = $server}
if (Test-Connection -Computername $Server -Count 2 -ea 0 -Quiet)
{
$status["Results"] = "Up"
}
else
{
$status["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $status -OutVariable serverStatus
$collection += $serverStatus
}
$collection | Export-Csv -LiteralPath d:\ServerStatus.csv -NoTypeInformation
最佳答案
您必须使用以下命令将$collection
初始化为数组:
$collection = @()
代替:$collection = $()
关于powershell - ping多台服务器并保存为CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39594564/