【问题标题】:Create Table with Variables in PowerShell在 PowerShell 中使用变量创建表
【发布时间】:2014-02-17 20:12:22
【问题描述】:

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

# 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 = "" 
                } 
   } 

【问题讨论】:

    标签: powershell formatting wmi


    【解决方案1】:

    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
    

    【讨论】:

    • $JsonAsString = @' { "key1": "value1", "key2": "value2", "key3": "value3" } '@ $FinalJsonArray = $($JsonAsString | ConvertFrom- Json).PSObject.Properties | foreach {@{Name = $_.Name;价值 = $_.价值}} | ConvertTo-Json $FinalJsonArray
    【解决方案2】:

    Keith 已经涵盖了,但由于我的回复无论如何都写了一半,就在这里。我的版本不需要创建新对象,并且将过滤作为管道的一部分。

    正如基思所说,在最后一分钟之前,将所有东西都作为一个对象保留。如果您需要强制 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      相关资源
      最近更新 更多