磁盘 space 报告创建 HTML 并将服务器列表添加到电子邮件

Disk space report create HTML and add server list to email

我正在使用一个很长的脚本,无法正常工作的部分让我有点困惑。

简而言之,该脚本查找所有服务器 OS,检查所有磁盘 space,如果符合条件则对某些磁盘进行颜色编码,将其输出到 HTML(我可以't email HTML files 因为它们被阻止了,所以不得不走这条路)然后它将满足条件的服务器列表添加到电子邮件正文并发送电子邮件。

一切正常,除了 "then it adds a list of server that meet a criteria to the body of an email and sends the email" 它只将列表中的最后一个服务器添加到电子邮件中。

这是我遇到问题的脚本片段。

第一部分是计算和计算百分比的内容,效果很好,添加到 HTML 报告中也很好。

第二部分是没有正确添加到电子邮件中的部分。我只从列表中得到最后一个满足免费 space 标准的服务器不到 5%。我确定我只是在看东西。有任何想法吗?

Get-ADComputer -Filter 'OperatingSystem -Like "*Server*"' | Sort DNSHostName | ForEach-Object { 
    ### Get the hostname of the machine
    #$hostname = Get-WmiObject -Impersonation Impersonate -ComputerName $_ -Query "SELECT Name From Win32_ComputerSystem"
    #$name = $hostname.Name.ToUpper()
    $name = $_.DNSHostName.ToUpper()
    Add-Content $html_file ('<Table id="dataTable"><tr><td colspan="3" class="serverName">' + $name + '</td></tr>
    <tr><td class="headings">Drive Letter</td><td class="headings">Total Size</td><td class="headings">Free Space</td><td class="headings">Percent Free</td></tr>')

    ### Get the drives of the machine
    $drives = Get-WmiObject Win32_LogicalDisk -Filter "drivetype=3" -ComputerName $name -Impersonation Impersonate

    ####loop through and add to html report
#####Edited portion of script below#########
 ForEach ($drive in $drives) {
        $space_color = ""
        $free_space = $drive.FreeSpace
        $percent = ($drive.FreeSpace/$drive.size*100)
        $percent = [Math]::Round($percent, 2)
        If ($percent -le 1) {
            $space_color = $Critical_space
            }
            elseif ($percent -le 5) {
            $space_color = $very_low_space
            }
            elseif ($percent -le 10) {
            $space_color = $low_space
                            }
            elseif ($percent -le 15) {
            $space_color = $medium_space
            }

        If ($percent -lt 5) {$addtobody += $name,$drive.deviceid,$Percent,"%"}

我在最初的 post 中未能指出 $addtobody 应该定义在您的外循环之上:

$addtobody = @()

在最后一行,尝试将 $addtobody 更改为 +=.

if ($percent -le 5) {
       $addtobody += $name,$drive.deviceid,$Percent,"%"
}

这会将值添加到现有值,而不是设置值。循环的每次迭代(如当前所写),您都将值重新分配给 $addtobody。

考虑到您的 HTML 电子邮件限制后,您还可以尝试发送基于文本的 table。以下代码段将超出您的循环范围。

$emailBody = $addtobody | Format-Table -AutoSize | Out-String

我认为 $addtobody 在代码未显示的某个时刻被添加到电子邮件中,对吗?如果是这样,那么您只需要将其转换为数组并向其中添加元素,而不是将其设置为等于元素。 (还假设缺少的两个 } 只是从您复制的内容中删除,我已经添加了它们,但请确保不要在您的脚本中将它们加倍)

$addtobody = @()
ForEach ($drive in $drives) {
    $space_color = ""
    $free_space = $drive.FreeSpace
    $percent = ($drive.FreeSpace/$drive.size*100)
    $percent = [Math]::Round($percent, 2)
    If ($percent -le 1) {
        $space_color = $Critical_space
        }
        elseif ($percent -le 5) {
        $space_color = $very_low_space
        }
        elseif ($percent -le 10) {
        $space_color = $low_space
                        }
        elseif ($percent -le 15) {
        $space_color = $medium_space
        }


   ForEach ($drive in $drives) {
    $free_space = $drive.FreeSpace
    $percent = ($drive.FreeSpace/$drive.size*100)
    $percent = [Math]::Round($percent, 2)
    If ($percent -le 5) {$addtobody += $name,$drive.deviceid,$Percent,"%"}
   }
}