【发布时间】:2016-12-14 19:57:22
【问题描述】:
我正在处理一个很长的脚本,其中不工作的部分让我有点困惑。
简而言之,该脚本会查找所有服务器操作系统,检查所有磁盘空间,对某些符合条件的文件进行颜色编码,然后将其输出为 HTML(我无法通过电子邮件发送 HTML 文件,因为它们被阻止了,所以不得不去这种方式)然后它将满足标准的服务器列表添加到电子邮件正文并发送电子邮件。
一切正常,除了“然后它将满足标准的服务器列表添加到电子邮件正文并发送电子邮件”它只将列表中的最后一个服务器添加到电子邮件中。
这是我遇到问题的脚本的 sn-p。
第一部分是数学和数字百分比,它工作得很好,添加到 HTML 报告中也很好。
第二部分是未正确添加到电子邮件的部分。我只从列表中获取满足可用空间小于 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,"%"}
【问题讨论】:
-
不知道 Powershell 的语法,但我假设您的函数会在 foreach 的每次迭代中覆盖 $addtobody(保留最后一个),而不是通过 .= 或 + 添加到 $addtobody = 或类似的东西。
标签: html powershell foreach diskspace