2012-03-28 139 views
8

如何讓我的PowerShell腳本在腳本進行時以表格格式打印信息。在PowerShell控制檯如何在PowerShell中創建printf效果

在bash,我會做這個通過

printf "%s\t%-15.15s" "Locale" "Jar" 
if($verbose);then 
    printf "%-15.15s %-15.15s" "HelpSet" "Exception" 
fi 
printf "\t%s\n" "Status" 
... 
printf "%s\t%-15.15s" $locale $helpFileName 
if($verbose); then 
    printf "%-15.15s %-15.15s" "$helpSetName" ${exclusion[$helpFileName]} 
fi 
status="OK" 
... 
if ($fixed); then 
    status="CORRECTED" 
fi 
printf "\t%s\n" $status 

得到

Locale Jar   HelpSet   Exception  Status 
de  help_D150  help_D150      CORRECTED 

es  help_D150  help_D150      OK 

fr  help_D150  help_D150      OK 

it  Locale folder not found 

nl  help_D150  help_D150      CORRECTED 

感謝

回答

16

嘗試了這一點:

"{0}`t{1,-15}{2,-15}{3,-15}" -f "Locale", "Jar", "HelpSet", "Exception" 

您可以使用string formatting很容易從PowerShell。

-f operator是一個PowerShell快捷方式的String.Format函數,包括所有支持格式的.NET類型的standard and custom

+0

通過這種方法,兩個條件列被打印在一個新行上而不是在同一行上 – rojanu 2012-03-28 10:43:44

+0

使用單個格式字符串,合併所有列,並且對不希望打印的參數使用$ null或空字符串。 – 2012-03-28 10:48:52

2

我接受了Davids的答案,因爲這就是我所要求的。不過,我選擇通過

try{ 
    add-type @' 
namespace FFPS { 
    public class Data { 
     public string Locale; 
     public string JarFile; 
     public string HelpSet; 
     public string CorrectName; 
     public string Status; 
    } 
} 
'@ 
} 
catch{} 

創建一個對象,然後使用XML格式文件格式化爲一個表

<?xml version="1.0" encoding="utf-16"?> 
<Configuration> 
    <ViewDefinitions> 
     <View> 
      <Name>ffps.data</Name> 
      <ViewSelectedBy> 
       <TypeName>ffps.data</TypeName> 
      </ViewSelectedBy> 
      <TableControl> 
       <TableHeaders> 
        <TableColumnHeader> 
         <Label>Locale</Label> 
         <Width>6</Width> 
        </TableColumnHeader> 
        <TableColumnHeader> 
         <Label>Jar File</Label> 
         <Width>16</Width> 
        </TableColumnHeader> 
        <TableColumnHeader> 
         <Label>Help Set</Label> 
         <Width>16</Width> 
        </TableColumnHeader> 
        <TableColumnHeader> 
         <Label>Correct Name</Label> 
         <Width>16</Width> 
        </TableColumnHeader> 
        <TableColumnHeader> 
         <Label>Status</Label> 
         <Width>100</Width> 
        </TableColumnHeader> 
       </TableHeaders> 
       <TableRowEntries> 
        <TableRowEntry> 
         <TableColumnItems> 
          <TableColumnItem> 
           <ScriptBlock>$_.Locale</ScriptBlock> 
          </TableColumnItem> 
          <TableColumnItem> 
           <ScriptBlock>$_.JarFile</ScriptBlock> 
          </TableColumnItem> 
          <TableColumnItem> 
           <ScriptBlock>$_.HelpSet</ScriptBlock> 
          </TableColumnItem> 
          <TableColumnItem> 
           <ScriptBlock>$_.CorrectName</ScriptBlock> 
          </TableColumnItem> 
          <TableColumnItem> 
           <ScriptBlock>$_.Status</ScriptBlock> 
          </TableColumnItem> 
         </TableColumnItems> 
        </TableRowEntry> 
       </TableRowEntries> 
      </TableControl> 
     </View> 
    </ViewDefinitions> 
</Configuration> 

,並在代碼做

$currentFile = New-Object ffps.data 
$currentFile.Locale = "DE" 
$currentFile.JarFile = "JarFile.Name" 
... 
$currentFile 

打印條目