循环中的Powrshell循环 [英] Powrshell Loop within Loops

查看:132
本文介绍了循环中的Powrshell循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a PowerShell script that runs a disk usage report (From http://gallery.technet.microsoft.com/Disk-Space-HTML-Email-f8b6bbfe)

我试图将其更改为显示文件夹大小也是,但已经被卡住了。我写这个工作正常:

I'm trying to change it to display folder size as well, but have gotten stuck. I wrote this which works fine:

$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)
}

但是当我尝试插入它时,它不会出来。这是我到目前为止:

But it doesn't come out right when I try to insert it. This is what I have so far:

# 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

任何人都知道为什么文件夹名称和文件夹大小变量isn'

Anyone know why the folder name and folder size variables aren't working?

推荐答案

我认为问题(或其中一个问题)是这一行: foreach ($文件夹中的$文件夹)。应该是 foreach($文件夹中的$文件夹),然后在你所指向的当前文件夹的for循环中的任何地方应该使用 $ folder (不是 $ folders )。

I think the problem (or one of the problems) is this line: foreach ($folders in $folders). It should be foreach ($folder in $folders) and then everywhere in the for loop that you refer to the current folder you should use $folder (not $folders).

尝试看看它的外观。

这篇关于循环中的Powrshell循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆