迭代:Powershell输出

时间:2016-07-15 12:38:23

标签: powershell scripting

我无法获得正确的PowerShell输出。

  
    

输出的示例如下:

         

ComputerName |驱动器|尺寸|免费| PercentFree
    COMP-NAME H:249 11 4.5

         
      

ComputerName |驱动器|尺寸|免费| PercentFree
      COMP-NAME C:67 3.2 4.7

             

ComputerName |驱动器|尺寸|免费| PercentFree
      COMP-NAME H:249 11 4.5

             

ComputerName |驱动器|尺寸|免费| PercentFree
      COMP-NAME C:67 3.2 4.7

             

ComputerName |驱动器|尺寸|免费| PercentFree
      COMP-NAME H:249 11 4.5

             

ComputerName |驱动器|尺寸|免费| PercentFree
      COMP-NAME C:67 3.2 4.7

             

ComputerName |驱动器|尺寸|免费| PercentFree
      COMP-NAME H:249 11 4.5

             

ComputerName |驱动器|尺寸|免费| PercentFree
      COMP-NAME C:67 3.2 4.7

    
  

下面的脚本向主机查询低于特定阈值的低磁盘并将其发送到相应的电子邮件。脚本运行正常,但我不断重复。

有人可以帮忙吗?谢谢。

#THE SCRIPT

# Set Global Parameters
$emailTO = "email@email.com"
$emailFrom = "LowSpaceNotify@email.com"
$smtpServer = "X.X.X.X"

$computers = "COMP-NAME"
$i = 0

# Get Drive Data
$report = @(
foreach($computer in $computers)
{
$drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
     foreach($drive in $drives)
     {
          # Calculate Free Space
          $obj = new-object psobject -Property @{
               ComputerName = $computer
               Drive = $drive.DeviceID
               Size = $drive.size / 1GB
               Free = $drive.freespace / 1GB
               PercentFree = $drive.freespace / $drive.size * 100
               }
          # Monitor for 10% or less in free space and report accordingly
          if ($obj.PercentFree -lt 10) {
               $obj | Format-Table ComputerName,Drive,@{n='Size';e={'{0:N1}' -f $_.Size}},@{n='Free';e={'{0:N1}' -f $_.Free}},@{n='PercentFree';e={'{0:N1}' -f $_.PercentFree}} | Out-String
               $i++
               }
     }

}
)

# Send notification if script finds more than 0 drives with less than 10% free space
if ($i -gt 0)
   {
       foreach ($user in $emailTo)
                {
        echo "Sending Email Notification to $user"
        $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
        $subject = "Server with Low Disk Space"
        foreach ($line in $report)
            {
                $body += "$line "
                }
        Send-MailMessage -to $user -From $emailFrom -SmtpServer $smtpServer -Subject $Subject -Body $body
                }
   } 

1 个答案:

答案 0 :(得分:1)

[CmdletBinding()]
Param (
    $ComputerName,
    $EmailTo = "email@email.com",
    $EmailFrom = "LowSpaceNotify@email.com",
    $EmailSubject = 'Low Hard Disk Report',
    $SmtpServer = "X.X.X.X"
)

# Get Drive Data
$DATA = foreach ( $computer in $ComputerName ) {
    Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object { ( $_.DriveType ) -eq 3 -and ( ( $_.freespace / $_.size ) -lt .1  ) } | ForEach-Object -Process {
        [pscustomobject] @{
            ComputerName = $computer
            Drive        = $_.DeviceID
            Size         = '{0:N1}' -f ( $_.Size / 1GB )
            Free         = '{0:N1}' -f ( $_.freespace / 1GB )
            PercentFree  = '{0:N1}' -f ( $_.freespace / $_.size * 100 )
        }
     }
}

if ( $DATA ) {
    $HTMLBody = $Data | ConvertTo-Html -Fragment
    Send-MailMessage -To $EmailTo -From $EmailFrom -SmtpServer $SmtpServer -Subject $EmailSubject -Body ( $HTMLBody -Join '`n' ) -BodyAsHtml
}