尝试编写关于 DHCP 使用情况的每日“检查”脚本。使用 netsh 从 DHCP 服务器中提取信息,查看正在使用和可用的 IP 地址,删除字母和分隔字符,所以我只剩下地址的原始数量。然后将它们相加以获得总地址池大小并以使用中的百分比结束。我已经为此烦恼了几天,终于得到了我喜欢的东西,但是我的数学不正确。一些远程站点有多个范围/子网,当尝试将这些数字添加到每个服务器的总数中时,它只是累积地添加地址。我认为我的问题是 $u 和 $f 中的最后一个“foreach”,但我不确定如何修复它并得到我想要的。如果我删除最终的 foreach,它对于单个子网站点可以正常工作,但不能在任何一台服务器上处理更多。我希望这是有道理的。
function dhcp {
$servers = "contoso","contoso2","contoso3"
Foreach ($server in $servers)
{
netsh dhcp server \\$server show server
"DHCP Server: $server"
#netsh dhcp server \\$server show mibinfo | select-string -pattern "scope", "subnet", "No. of Addresses in use = ", "No. of free Addresses = "
$u = (netsh dhcp server \\$server show mibinfo |
where-object {$_-match "No. of Addresses in use = "} |
foreach-object{$_.Split(“=")[-1].Trim( ).Trim(".").Trim(" ")} |
foreach-object{($u2+=[int]$_)})
$f = (netsh dhcp server \\$server show mibinfo |
where-object {$_-match "No. of free Addresses = "} |
foreach-object{$_.Split(“=")[-1].Trim().Trim(".").Trim(" ")} |
foreach-object{($f2+=[int]$_)})
$total2 = ([int]$u2+[int]$f2)
$percent = [Math]::Round([int]$u2/[int]$total2*100,2)
"No. of Addresses in use = $u2"
"No. of free Addresses = $f2"
"Total Addresses: $total2"
"Percent in use: $percent%"
" ======"
"=================================================="
}
}
dhcp | out-file "C:\Users\crabbypatty\Desktop\Powershell\dhcplog $(get-date -f MM-dd-HHmm).txt"