0
点赞
收藏
分享

微信扫一扫

简单实现批量ping并生成html报告 Powershell Code

千妈小语 2022-09-06 阅读 199
  1. 准备txt文本存储ip地址列表,ip一行一个
  2. 用到关键命令 Test-Connection -Quiet

Quiet 参数返回布尔值。 使用此参数可禁止显示所有错误。

测试的每个连接都返回 一个布尔 值。 如果 TargetName 参数指定多台计算机,则返回 布尔 值的数组。

如果给定目标 的任何 ping 成功, ​​$True​​ 则返回。

如果给定目标 的所有 ping 都失败, ​​$False​​ 则返回。


  1. 代码段

$ips = Get-Content -Path "C:\Users\xx\ip.txt"

$ip= $ips.Split([Environment]::NewLine) #以换行符分割

$output = New-Object System.Collections.ArrayList

foreach($i in $ip){

$result=Test-Connection -ipaddress $i -Count 1 -Quiet -ErrorAction SilentlyContinue #对各ip 返回值 是否能ping通

#创建可用对象
$output.add([pscustomobject]@{
IPtoPing = $i;
Result = $result
})
}

#html CSS格式
$Header = @"
<style>
tbody tr:nth-child(even) {
background: #f0f0f2;
}
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
.FalseStatus {
color: #ff0000;
}
</style>
"@

$body = $output|Select-Object -Property IPtoPing,Result |ConvertTo-Html -Property IPtoPing,Result -Fragment -PreContent "<h2>Ping Result List --- Creation Date: $(Get-Date)</h2>"
$body = $body -replace '<td>False</td>','<td class="FalseStatus">False</td>' #更换html内容 False显示红色

$report = ConvertTo-Html -Body "$body" -Head $Header | Out-File -FilePath "C:\Users\xx\ip.html"

  1. 输出结果 示例

 简单实现批量ping并生成html报告 Powershell Code_powershell

举报

相关推荐

0 条评论