powershell - ping多台服务器并保存为CSV

标签 powershell

我是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/

相关文章:

json - Powershell - 从嵌套对象中获取多个值

parsing - 使用 Powershell 读取/解析二进制文件

string - Powershell Select-String 然后获取返回文件的数据

powershell - 如何在 PowerShell 中运行命令行应用程序

powershell - 如果代码的其他部分或其他点源脚本中未定义,如何检查然后定义函数

Powershell密码解密

powershell - 忽略 Powershell 中文本字符串中的引号

powershell - 如何替换字符串/文件末尾的字符?

powershell - 我可以获得注册到 TfsTeamProjectCollection 的服务列表吗

image - 从网址/网络摄像头保存图像