I would like to format the output table with a left allingment on the first column and right allingment on all the remaining columns. When I replace $myobj = "" | Select "Drive","PercentFreeSpace", "DriveSpace", "FreeSpace" with $myobj = Format-Table [["" | Select "Drive","PercentFreeSpace", "DriveSpace", "FreeSpace"] [-AutoSize]] I get an error.
The powershell script below.
#********************************************************************
# For Loop - get % free space of all drives and email output
start-transcript -path c:\LOGS\log_drivespace.txt
#Define Variables
$Notify = 0
$MinFreePercent = "25"
$FileDriveSpace = "c:\Logs\DriveSpace.txt"
$FromAddress = "#[email protected]"
$ToAddress = "[email protected]"
#$ToAddress = "#[email protected]"
$SendingServer = "mailhost1"
$ComputerName = $(Get-WmiObject Win32_Computersystem).name
$Drives_Threshold = ""
$OutArray = @()
$outarray += "Disk space Alerts and Utilizations on server $ComputerName"
$AllDisks = get-wmiobject Win32_LogicalDisk -Filter “DriveType = 3"
#Omit the a, b drives if exist
$AllDisks = $AllDisks | ? { $_.DeviceID -notmatch "[ab]:"}
foreach ($objdisk in $AllDisks)
{
$DeviceId = $objDisk.DeviceID
$DeviceId = $DeviceId -replace ":", ""
#$FreePercent = "{0:P0} " -f ($objDisk.FreeSpace/$objDisk.Size)
$FreePercent = (100 * $objDisk.FreeSpace/$objDisk.Size)
If ($FreePercent -lt $MinFreePercent)
{
$Threshold = "Threshold Reached"
$Notify = 1
If ($Drives_Threshold -eq "")
{ $Drives_Threshold = $DeviceId }
Else
{ $Drives_Threshold = $Drives_Threshold + ", " + $DeviceId }
}
Else
{ $Threshold = "N/A"}
$FreePercent = "{0:P0} " -f ($objDisk.FreeSpace/$objDisk.Size)
$TotalDriveSpace = ($objDisk.Size/(1024 * 1024 * 1024))
#$TotalDriveSpace = ([Math]::Round($TotalDriveSpace, 0))
$TotalDriveSpace = ("{0:N0}" -f $TotalDriveSpace) + " GB"
$FreeSpace = ($objDisk.FreeSpace/(1024 * 1024 * 1024))
#$FreeSpace = [Math]::Round($FreeSpace, 0)
$FreeSpace = ("{0:N0}" -f $FreeSpace) + " GB"
$myobj = "" | Select "Drive","PercentFreeSpace", "DriveSpace", "FreeSpace"
#$myobj = Format-Table [["" | Select "Drive","PercentFreeSpace", "DriveSpace", "FreeSpace"] [-AutoSize]]
$myobj.Drive = $DeviceId
$myobj.PercentFreeSpace = $FreePercent
$myobj.DriveSpace = $TotalDriveSpace
$myobj.FreeSpace = $FreeSpace
#$myobj.Threshold = $Threshold
#Only for testing
#$outLine = $DeviceId + " " + $FreePercent + " " + $Threshold + " " + $MinFreePercent
#$outLine
#Add the object to the out-array
$outarray += $myobj
#Wipe the object just to be sure
$myobj = $null
}
$outarray += "The following Drives are below the $MinFreePercent% Threshold limit: $Drives_Threshold"
$outarray > $FileDriveSpace
$MessageSubject = "Disk Space Alert for Drives $Drives_Threshold on Server $ComputerName"
if ($Notify -eq 1)
{
$scr = $outarray | out-string
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $scr
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)
}
#********************************************************************