0

我有一个运行磁盘使用报告的 PowerShell 脚本(来自http://gallery.technet.microsoft.com/Disk-Space-HTML-Email-f8b6bbfe

我正在尝试将其更改为也显示文件夹大小,但已经卡住了。我写了这个,效果很好:

$volName = "D:\"
$folders = (Get-ChildItem $volName | ?{ $_.PSIsContainer })
foreach ($folders in $folders)
{
$size = (get-childitem $folders.fullname -recurse | measure-object -property length -sum)
write-host $folders.fullname ($size.sum / 1MB)
}

但是当我尝试插入它时它并没有正确显示。这是我到目前为止所拥有的:

# Script will generate Disk Space Report for Exchange Servers (Daily, Weekly, Monthly)

$ErrorActionPreference = "SilentlyContinue";
$scriptpath = $MyInvocation.MyCommand.Definition 
$dir = Split-Path $scriptpath 

#Variables to configure
$percentWarning = 25;
$percentCritcal = 15;
$smtpServer = "blah@blah.com" 
$ReportSender = "blah@blah.com"  
$users = "user1@mydomain.com", "user2@mydomain.com"; 
$MailSubject = "DiskSpace Report for $titledate"

#No change needed from here!!!
$reportPath = "$dir\Logs\"
$reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html";
$diskReport = $reportPath + $reportName
$redColor = "#FF0000"
$orangeColor = "#FBB917"
$whiteColor = "#FFFFFF"
$greenColor = "#7FFF00"
$i = 0;
$computers = $env:COMPUTERNAME;
$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss";
If (Test-Path $diskReport)
    {
        Remove-Item $diskReport
    }
$titleDate = get-date -uformat "%m-%d-%Y - %A"
$header = "
        <html>
        <head>
        <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
        <title>DiskSpace Report</title>
        <STYLE TYPE='text/css'>
        <!--
            table {
                    border: thin solid #666666;
            }
        td {
            font-family: Tahoma;
            font-size: 11px;
            border-top: 1px solid #999999;
            border-right: 1px solid #999999;
            border-bottom: 1px solid #999999;
            border-left: 1px solid #999999;
            padding-top: 0px;
            padding-right: 0px;
            padding-bottom: 0px;
            padding-left: 0px;
        }
        body {
            margin-left: 5px;
            margin-top: 5px;
            margin-right: 0px;
            margin-bottom: 10px;
            table {
            border: thin solid #000000;
        }
        -->
        </style>
        </head>
        <body>
        <table width='100%'>
        <tr bgcolor='#CCCCCC'>
        <td colspan='7' height='25' align='center'>
        <font face='tahoma' color='#003399' size='4'><strong>DiskSpace Report for $titledate</strong></font>
        </td>
        </tr>
        </table>
"
 Add-Content $diskReport $header
 $tableHeader = "
 <table width='100%'><tbody>
    <tr bgcolor=#CCCCCC>
    <td width='10%' align='center'>Server</td>
    <td width='5%' align='center'>Drive</td>
    <td width='25%' align='center'>Folder</td>
    <td width='5%' align='center'>Folder Size</td>
    <td width='10%' align='center'>Total Capacity(GB)</td>
    <td width='5%' align='center'>Used Capacity(GB)</td>
    <td width='5%' align='center'>Free Space(GB)</td>
    <td width='5%' align='center'>Freespace %</td>
    </tr>
"
Add-Content $diskReport $tableHeader
  foreach($computer in $computers)
    {   
    $disks = Get-WmiObject -ComputerName $computer -Class Win32_Volume -Filter "DriveType = 3" | Where-Object {$_.Label -ne "System Reserved" -and $_.DriveLetter -ne "C:"}
    $computer = $computer.toupper()
        foreach($disk in $disks)
    {        
        $deviceID = $disk.Label;
            $volName = $disk.Name;
            $folders = (Get-ChildItem $volName | ?{ $_.PSIsContainer });

      foreach ($folders in $folders)
      {
          $size = (get-childitem $folders.fullname -recurse | measure-object -property length -sum)

        [float]$size = $disk.Capacity;
        [float]$freespace = $disk.FreeSpace; 
        $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
        $sizeGB = [Math]::Round($size / 1073741824, 2);
        $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
        $usedSpaceGB = [Math]::Round($sizeGB - $freeSpaceGB, 2);
        $color = $greenColor;
    if($percentFree -lt $percentWarning)      
        {
       $color = $orangeColor    
      if($percentFree -lt $percentCritcal)
        {
        $color = $redColor
        }
        }        
    $dataRow = "
        <tr>
        <td width='10%'>$computer</td>
        <td width='5%' align='center'>$volName</td>
        <td width='25%' >$folders.fullname</td>
        <td width='5%' >$size</td>
        <td width='10%' align='center'>$sizeGB</td>
        <td width='5%' align='center'>$usedSpaceGB</td>
        <td width='5%' align='center'>$freeSpaceGB</td>
        <td width='5%' bgcolor=`'$color`' align='center'>$percentFree</td>
        </tr>
"
Add-Content $diskReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree";
    $i++        
    }
    }
}
 $tableDescription = "
 </table><br><table width='20%'>
    <tr bgcolor='White'>
    <td width='10%' align='center' bgcolor='#FBB917'>Warning less than $percentWarning% free space</td>
    <td width='10%' align='center' bgcolor='#FF0000'>Critical less than $percentCritcal% free space</td>
    </tr>
"
    Add-Content $diskReport $tableDescription
    Add-Content $diskReport "</body></html>"
if ($i -gt 0)
{
    foreach ($user in $users)
{
        Write-Host "Sending Email notification to $user"

        $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
        $msg = New-Object Net.Mail.MailMessage
        $msg.To.Add($user)
        $msg.From = $ReportSender
        $msg.Subject = $MailSubject
        $msg.IsBodyHTML = $true
        $msg.Body = get-content $diskReport
        $smtp.Send($msg)
        $body = ""
    }
  }

它看起来像这样:

http://oi40.tinypic.com/ru9sfb.jpg

任何人都知道为什么文件夹名称和文件夹大小变量不起作用?

4

2 回答 2

1

我认为问题(或问题之一)是这一行:foreach ($folders in $folders). 它应该是foreach ($folder in $folders),然后在 for 循环中的任何地方,您都应该引用您应该使用的当前文件夹$folder(不是$folders)。

试一试,看看它的外观。

于 2013-07-10T23:06:25.370 回答
0

感谢您的帮助,结果修改 $folder (并创建了一些其他)变量有效。包含文件夹和文件夹大小的报告:

# Script will generate Disk Space Report for Exchange Servers (Daily, Weekly, Monthly)

$ErrorActionPreference = "SilentlyContinue";
$scriptpath = $MyInvocation.MyCommand.Definition 
$dir = Split-Path $scriptpath 

#Variables to configure
$percentWarning = 25;
$percentCritcal = 15;
$smtpServer = "blah@blah.com" 
$ReportSender = "blah@blah.com"  
$users = "user1@mydomain.com", "user2@mydomain.com"; 
$MailSubject = "DiskSpace Report for $titledate"

#No change needed from here!!!
$reportPath = "$dir\Logs\"
$reportName = "DiskSpaceRpt_$(get-date -format ddMMyyyy).html";
$diskReport = $reportPath + $reportName
$redColor = "#FF0000"
$orangeColor = "#FBB917"
$whiteColor = "#FFFFFF"
$greenColor = "#7FFF00"
$i = 0;
$computers = $env:COMPUTERNAME;
$datetime = Get-Date -Format "MM-dd-yyyy_HHmmss";
If (Test-Path $diskReport)
    {
        Remove-Item $diskReport
    }
$titleDate = get-date -uformat "%m-%d-%Y - %A"
$header = "
        <html>
        <head>
        <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
        <title>DiskSpace Report</title>
        <STYLE TYPE='text/css'>
        <!--
            table {
                    border: thin solid #666666;
            }
        td {
            font-family: Tahoma;
            font-size: 11px;
            border-top: 1px solid #999999;
            border-right: 1px solid #999999;
            border-bottom: 1px solid #999999;
            border-left: 1px solid #999999;
            padding-top: 0px;
            padding-right: 0px;
            padding-bottom: 0px;
            padding-left: 0px;
        }
        body {
            margin-left: 5px;
            margin-top: 5px;
            margin-right: 0px;
            margin-bottom: 10px;
            table {
            border: thin solid #000000;
        }
        -->
        </style>
        </head>
        <body>
        <table width='100%'>
        <tr bgcolor='#CCCCCC'>
        <td colspan='7' height='25' align='center'>
        <font face='tahoma' color='#003399' size='4'><strong>DiskSpace Report for $titledate</strong></font>
        </td>
        </tr>
        </table>
"
 Add-Content $diskReport $header
 $tableHeader = "
 <table width='100%'><tbody>
    <tr bgcolor=#CCCCCC>
    <td width='10%' align='center'>Server</td>
    <td width='5%' align='center'>Drive</td>
    <td width='25%' align='center'>Folder</td>
    <td width='5%' align='center'>Folder Size (MB)</td>
    <td width='10%' align='center'>Total Capacity(GB)</td>
    <td width='5%' align='center'>Used Capacity(GB)</td>
    <td width='5%' align='center'>Free Space(GB)</td>
    <td width='5%' align='center'>Freespace %</td>
    </tr>
"
Add-Content $diskReport $tableHeader
  foreach($computer in $computers)
    {   
    $disks = Get-WmiObject -ComputerName $computer -Class Win32_Volume -Filter "DriveType = 3" | Where-Object {$_.Label -ne "System Reserved" -and $_.DriveLetter -ne "C:"}
    $computer = $computer.toupper()
        foreach($disk in $disks)
    {        
        $deviceID = $disk.Label;
            $volName = $disk.Name;
            $folders = (Get-ChildItem $volName | ?{ $_.PSIsContainer });

      foreach ($folder in $folders)
      {
          $sizefolder = (get-childitem $folder.fullname -recurse | measure-object -property length -sum)
          $FolderPaths = $folder.fullname
    [float]$FolderSize = $sizefolder.sum
        $foldersizeMB = [Math]::Round($foldersize / 1048576, 2);
        [float]$size = $disk.Capacity;
        [float]$freespace = $disk.FreeSpace; 
        $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
        $sizeGB = [Math]::Round($size / 1073741824, 2);
        $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
        $usedSpaceGB = [Math]::Round($sizeGB - $freeSpaceGB, 2);
        $color = $greenColor;
    if($percentFree -lt $percentWarning)      
        {
       $color = $orangeColor    
      if($percentFree -lt $percentCritcal)
        {
        $color = $redColor
        }
        }        
    $dataRow = "
        <tr>
        <td width='10%'>$computer</td>
        <td width='5%' align='center'>$volName</td>
        <td width='25%' >$folderpaths</td>
        <td width='5%' >$foldersizeMB</td>
        <td width='10%' align='center'>$sizeGB</td>
        <td width='5%' align='center'>$usedSpaceGB</td>
        <td width='5%' align='center'>$freeSpaceGB</td>
        <td width='5%' bgcolor=`'$color`' align='center'>$percentFree</td>
        </tr>
"
Add-Content $diskReport $dataRow;
Write-Host -ForegroundColor DarkYellow "$computer $deviceID percentage free space = $percentFree";
    $i++        
    }
    }
}
 $tableDescription = "
 </table><br><table width='20%'>
    <tr bgcolor='White'>
    <td width='10%' align='center' bgcolor='#FBB917'>Warning less than $percentWarning% free space</td>
    <td width='10%' align='center' bgcolor='#FF0000'>Critical less than $percentCritcal% free space</td>
    </tr>
"
    Add-Content $diskReport $tableDescription
    Add-Content $diskReport "</body></html>"
if ($i -gt 0)
{
    foreach ($user in $users)
{
        Write-Host "Sending Email notification to $user"

        $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
        $msg = New-Object Net.Mail.MailMessage
        $msg.To.Add($user)
        $msg.From = $ReportSender
        $msg.Subject = $MailSubject
        $msg.IsBodyHTML = $true
        $msg.Body = get-content $diskReport
        $smtp.Send($msg)
        $body = ""
    }
  }
于 2013-07-11T15:43:36.097 回答