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