3

尝试编写关于 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"
4

2 回答 2

1

我会通过在您的正则表达式中使用捕获组以及使用切换条件处理正则表达式的能力来显着简化这一点。

哦,我会使用字典来确保单独跟踪每个服务器,同时允许您在捕获完成后访问数据。如果您保存了数据,这也将使您能够进行一些历史跟踪和比较......但这超出了这个范围;)

例如

    $addressesInUse = @{};
    $addressesFree = @{};

    foreach ($server in $serverList) {
        $mibinfo = netsh dhcp server \\$server show mibinfo; #| where-object {$_-match "No. of Addresses in use = "}  |
        foreach ($info in $mibinfo) {
            switch -regex ($info) {
                "No. of Addresses in use = (?<count>\d+)" {
                    $val = $Matches["count"]; 
                    if ($addressesInUse.ContainsKey($server)) {
                        $addressesInUse.Add($server,$val);
                    } else {
                        $scopeCount.$server += $val;
                    }
                    break;
                }
                "No. of free Addresses = (?<count>\d+)" {
                    $val = [int]::Parse($Matches["count"]);
                    if ($addressesFree.ContainsKey($server)) {
                        $addressesFree.Add($server,$val);
                    } else {
                        $addressesFree.$server += $val;
                    }
                    break
                }
            }
        }
    }

    foreach ($key in $addressesFree.keys) {
        $total = $addressesInUse.$key + $addressesFree.$key;
        $percent = [Math]::Round($addressesInUse.$server / $total*100,2)
        Write-Host ("DHCP availablility for server {0}" -f $key);
        Write-Host ("No. of Addresses in use = {0}" -f $addressesInUse.$key);
        Write-Host ("No. of free Addresses = {0}" -f $addressesFree.$key);
        Write-Host ("Total Addresses: {0}" -f $total);
        Write-Host ("Percent in use: {0}%" - $percent);
    }
于 2013-03-20T12:13:54.947 回答
0

这可能就像在第一个循环的顶部将变量设置为零一样简单吗?意思是在您的“netsh dhcp server \$server show server”之后,将 u2 和 f2 都设置回零?

Foreach ($server in $servers) {
    netsh dhcp server \\$server show server
    "DHCP Server: $server"

此时,重置变量:

    $f2 = 0
    $u2 = 0
于 2012-12-12T22:35:45.250 回答