如何在PowerShell中创建printf效果

时间:2012-03-28 09:09:03

标签: powershell printf

当脚本进展时,如何让我的PowerShell脚本以表格格式打印信息。

在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

由于

2 个答案:

答案 0 :(得分:18)

在PowerShell控制台中试试这个:

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

您可以在PowerShell中轻松使用string formatting

-f operatorString.Format函数的PowerShell快捷方式,包括所有standard and custom格式化.NET类型支持。

答案 1 :(得分:2)

我接受了戴维斯的回答,因为这就是我的要求。但是,我选择通过

创建一个对象
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

打印条目