【发布时间】: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