在PowerShell中使用变量创建表 [英] Create Table with Variables in PowerShell

查看:113
本文介绍了在PowerShell中使用变量创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm still learning my way around PowerShell scripting and I'm working on a script to calculate the free space percentage of my file servers and send an e-mail notification when a drive reaches 10% of free space remaining or less (this happens roughly once a month, and I need to know before I get e-mailed from client's that there's no more space). As of now the script works great and is set up to run every morning via Windows Task. But the current formatting that I have in place for the output is done manually. I was wondering if there was a way to pass the variables from the information that was gathered and calculated with the Get-WmiObject function. I've tried the Format-Table and attempted messing with hash tables, but to no avail. Any ideas would be helpful. Thanks.

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

推荐答案

Format-Table在单一对象类型中拥有所有感兴趣的数据时效果最佳.我建议为此创建自定义对象:

Format-Table works best when you have all the data of interest in a single type of object. I would recommend creating custom objects for this e.g.:

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命令中执行以下操作:

If you want to change the display format then in the format-table command you can do this:

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

这篇关于在PowerShell中使用变量创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆