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 = "[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 = ""
}
}
First, you start with creating the DataTable object and give it a name. This creates a new object of type „System. Data. DataTable“ and assign it to the variable $table.
By default, the variables that you create at the PowerShell command line exist only while the PowerShell window is open. When the PowerShell windows is closed, the variables are deleted. To save a variable, add it to your PowerShell profile. You can also create variables in scripts with global, script, or local scope.
$_ is a variable created by the system usually inside block expressions that are referenced by cmdlets that are used with pipe such as Where-Object and ForEach-Object . But it can be used also in other types of expressions, for example with Select-Object combined with expression properties.
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++
}
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With