2012-03-28 31 views
8

Làm cách nào để lấy tập lệnh PowerShell để in thông tin ở định dạng bảng khi tiến trình tập lệnh.Cách tạo hiệu ứng printf trong PowerShell

Trong bash tôi sẽ làm điều này bằng cách

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 

để có được

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 

Cảm ơn

Trả lời

16

Hãy thử điều này trong giao diện điều khiển PowerShell của bạn:

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

Bạn có thể sử dụng string formatting khá dễ dàng từ PowerShell.

-f operator là một cắt ngắn PowerShell với chức năng String.Format, bao gồm tất cả các định dạng hỗ trợ loại .NET standard and custom.

+0

Với phương pháp này, hai cột có điều kiện được in trên một dòng mới thay vì trên cùng một dòng – rojanu

+0

Sử dụng một chuỗi định dạng duy nhất, kết hợp tất cả các cột và sử dụng $ null hoặc một chuỗi rỗng cho các arg mà bạn không muốn in. –

2

Tôi đã chấp nhận câu trả lời của Davids vì đó là những gì tôi yêu cầu. Tuy nhiên, tôi đã chọn để tạo ra một đối tượng bằng cách

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

và sau đó sử dụng tập tin định dạng XML để định dạng nó như một bảng

<?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> 

và trong các mã làm

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

để in các mục nhập

Các vấn đề liên quan