gpt4 book ai didi

loops - Ping 主机名列表并将结果输出到 powershell 中的 csv

转载 作者:行者123 更新时间:2023-12-05 01:28:33 24 4
gpt4 key购买 nike

我有一个很大的主机名列表,我需要 ping 以查看它们是启动还是关闭。我在编写脚本方面并不是很擅长,但我设法弄清楚了这一点:

$names = Get-content "hnames.txt"

foreach ($name in $names){
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$name is up" -ForegroundColor Green
}
else{
Write-Host "$name is down" -ForegroundColor Red
}
}

这让我得到了我需要的东西,但我现在需要将这些结果写到一个 csv 文件中,我不知道该怎么做。

请帮忙!

最佳答案

您可以改用以下代码(我只是将写入主机调用更改为 CSV 格式)并使用“PowerShell.exe script.ps > output.csv”执行它
请注意,您必须从包含 hnames.txt 的文件夹中执行它,或者只需将“hnames.txt”更改为完整路径。

$names = Get-content "hnames.txt"

foreach ($name in $names){
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$name,up"
}
else{
Write-Host "$name,down"
}
}

附言您也可以使用 Out-File Cmdlet创建 CSV 文件

关于loops - Ping 主机名列表并将结果输出到 powershell 中的 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29493414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com