Iteration: Powershell Output

≡放荡痞女 提交于 2019-12-13 07:00:07

问题


I am having trouble getting the right PowerShell output.

An example of the Output is as follows:

ComputerName | Drive | Size | Free | PercentFree
COMP-NAME H: 249 11 4.5

ComputerName | Drive | Size | Free | PercentFree
COMP-NAME C: 67 3.2 4.7

ComputerName | Drive | Size | Free | PercentFree
COMP-NAME H: 249 11 4.5

ComputerName | Drive | Size | Free | PercentFree
COMP-NAME C: 67 3.2 4.7

ComputerName | Drive | Size | Free | PercentFree
COMP-NAME H: 249 11 4.5

ComputerName | Drive | Size | Free | PercentFree
COMP-NAME C: 67 3.2 4.7

ComputerName | Drive | Size | Free | PercentFree
COMP-NAME H: 249 11 4.5

ComputerName | Drive | Size | Free | PercentFree
COMP-NAME C: 67 3.2 4.7

The Script below queries the host for low disk below certain threshold and send it to the appropriate email. The Script runs fine, but I keep getting repeated lines.

Can someone please help? Thank you.

#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:


[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
}


来源:https://stackoverflow.com/questions/38396334/iteration-powershell-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!