我仍在學習PowerShell腳本編寫方法,並且正在研究一個腳本來計算文件服務器的可用空間百分比,並在驅動器達到10%時發送電子郵件通知剩餘空間或更少(這大概每月發生一次,而且在我從客戶端收到電子郵件之前我需要知道沒有更多空間)。截至目前,該腳本運行良好,並且設置爲每天早上通過Windows任務運行。但是我現有的輸出格式是手動完成的。我想知道是否有一種方法可以通過Get-WmiObject函數收集和計算的信息傳遞變量。我已經嘗試過格式表並試圖搞亂哈希表,但無濟於事。任何想法都會有所幫助。謝謝。在PowerShell中創建包含變量的表格
# Set Parameters
$file = "c:\location\Lowdisk.txt"
Clear-Content $file
$emailTO = "[email protected]"
$emailFrom = "[email protected]"
$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 = ""
}
}
這正是我期望完成的。謝謝你的幫助!! – user3140412