在PowerShell中使用变量创建表

时间:2014-02-17 20:12:23

标签: powershell formatting wmi

我还在学习PowerShell脚本的方法,我正在编写一个脚本来计算文件服务器的可用空间百分比,并在驱动器剩余可用空间的10%或更少时发送电子邮件通知(这种情况大约每月发生一次,在我从客户端发送电子邮件之前我需要知道没有更多的空间)。截至目前,该脚本运行良好,并设置为每天早上通过Windows任务运行。但是我现有的输出格式是手动完成的。我想知道是否有办法从使用Get-WmiObject函数收集和计算的信息中传递变量。我已经尝试了格式表并尝试搞乱哈希表,但无济于事。任何想法都会有所帮助。感谢。

# Set Parameters
$file = "c:\location\Lowdisk.txt"
Clear-Content $file

$emailTO = "Someone@SomeDomain.com"
$emailFrom = "Someone@SomeDomain.com"
$smtpServer = "smtpServer"

$diskspace = "3"
$computers = ("FSCN01","FSCN02","FSCN03","FSCN04")
echo "Server Name       Drive       Drive Size       Free Space       % Free" >> $file
$i = 0

# Get Drive Data
foreach($computer in $computers)
{
$drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
foreach($drive in $drives)
{
    $ID = $drive.DeviceID
    $size1 = $drive.size / 1GB
    $size = "{0:N1}" -f $size1
    $free1 = $drive.freespace / 1GB
    $free = "{0:N1}" -f $free1
    $a = $free1 / $size1 * 100
    $b = "{0:N1}" -f $a
        # Monitor for drive free space % under 10%
        if ($b -lt 10)
        {
            echo "$computer         $ID          $size         $free         $b" >> $file
            $i++
        }
}
}
# Send notification if script finds more than 0 drives with less than 35% 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 Get-Content $file) 
                { 
                $body += "$line `n" 
                } 
        Send-MailMessage -to $user -From $emailFrom -Attachments $file -SmtpServer $smtpServer -Subject $Subject -Body $body 
        $body = "" 
                } 
   } 

2 个答案:

答案 0 :(得分:11)

当您拥有单一类型对象中感兴趣的所有数据时,

Format-Table效果最佳。我建议为此创建自定义对象:

foreach($computer in $computers)
{
    $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
    foreach($drive in $drives)
    {
        $obj = new-object psobject -Property @{
                   ComputerName = $computer
                   Drive = $drive.DeviceID
                   Size  = $drive.size / 1GB
                   Free  = $drive.freespace / 1GB
                   PercentFree = $drive.freespace / $drive.size * 100
               }
        if ($obj.PercentFree -lt 10) {
            $obj | Format-Table ComputerName,Drive,Size,Free,PercentFree
            $i++
        }
    }
}

如果要更改显示格式,请在format-table命令中执行此操作:

$obj | Format-Table ComputerName,Drive,@{n='Size';e={'{0:N1}' -f $_.Size}},Free,PercentFree

答案 1 :(得分:1)

基思已经覆盖了它,但是我的回复是半个写的,不管怎么说。我的版本不需要创建新对象,并将过滤作为管道的一部分。

正如凯斯所说,把所有东西都当作对象,直到最后一分钟。如果您需要强制PowerShell格式化,请使用'out-string'。

另外,请留意对列表的支持。许多命令将列表作为参数(例如计算机名称列表,电子邮件收件人),而不需要for-each循环。这可以使代码更简洁,但可读性更低,特别是对于初学者。无论如何,这就是我做的事情:

$comps=@("server1","server2","server3","server4")
$smtpServer = "mail.mydomain.com"
$toList = @("someone@mydomain.com","anotherperson@mydomain.com")
$minPercent = 10

$tableSpec = @(
        @{label="Computer";expression={$_.SystemName}},
        @{label="Disk";expression={$_.DeviceID}},
        @{label="Size Gb";expression={[int]($_.size / 1Gb)}},
        @{label="PercentFree";expression={[int]($_.FreeSpace / $_.Size * 100)}}
    )

$report = (

    Get-WmiObject -ComputerName $comps -Class Win32_LogicalDisk |
    Where-Object { ($_.DriveType -eq 3) -and ($_.FreeSpace -lt ($_.Size * $minPercent / 100)) } |
    Sort-Object SystemName, DeviceID |
    ft -property $tableSpec -AutoSize |
    Out-String

    )

Send-MailMessage -Body $report -To $toList -Subject "Disk space report" -SmtpServer $smtpServer