在Powershell中格式化表格

时间:2014-05-08 18:50:02

标签: powershell

我想格式化输出表,在第一列上使用左对齐,在所有剩余列上使用右对齐。当我替换$ myobj =“”|选择“Drive”,“PercentFreeSpace”,“DriveSpace”,“FreeSpace”  with $ myobj = Format-Table [[“”|选择“Drive”,“PercentFreeSpace”,“DriveSpace”,“FreeSpace”] [-AutoSize]] 我收到了错误。

下面的powershell脚本。

#********************************************************************
# 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 = "#lnteam@pepcoholdings.com"
$ToAddress = "hdsouza@pepco.com"
#$ToAddress = "#lnteam@pepcoholdings.com"
$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)
}
#********************************************************************

1 个答案:

答案 0 :(得分:2)

你不希望你的对象成为一个表,你希望它成为一个对象。然后当你去输出时,它将数组格式化为表格。所以请保留$ myObj原样,然后当你去输出时做类似的事情:

"The following Drives are below the $MinFreePercent% Threshold limit: $Drives_Threshold" | out-file $FileDriveSpace
$outarray |FT -Auto | Out-File $FileDriveSpace -append

if($notify -eq 1){
    $scr = gc $FileDriveSpace