首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代: Powershell输出

迭代: Powershell输出
EN

Stack Overflow用户
提问于 2016-07-15 12:38:23
回答 1查看 98关注 0票数 0

我很难获得正确的PowerShell输出。

输出的一个例子如下: ComputerName,驱动器,尺寸,尺寸,免费,PercentFree 公司名称H: 249 11 4.5 ComputerName,驱动器,尺寸,尺寸,免费,PercentFree 名称C: 67 3.2 4.7 ComputerName,驱动器,尺寸,尺寸,免费,PercentFree 公司名称H: 249 11 4.5 ComputerName,驱动器,尺寸,尺寸,免费,PercentFree 名称C: 67 3.2 4.7 ComputerName,驱动器,尺寸,尺寸,免费,PercentFree 公司名称H: 249 11 4.5 ComputerName,驱动器,尺寸,尺寸,免费,PercentFree 名称C: 67 3.2 4.7 ComputerName,驱动器,尺寸,尺寸,免费,PercentFree 公司名称H: 249 11 4.5 ComputerName,驱动器,尺寸,尺寸,免费,PercentFree 名称C: 67 3.2 4.7

下面的脚本查询主机是否低于某一阈值的磁盘,并将其发送到适当的电子邮件。这个脚本运行得很好,但我一直有重复的行。

有人能帮忙吗?谢谢。

代码语言:javascript
复制
#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
                }
   } 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-15 14:34:26

代码语言:javascript
复制
[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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38396334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档