How can I get my PowerShell script to print the info in tabular format as the script progresses.
In bash I would do this by
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
to get
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
Thanks
Array subexpression operator @( )Returns the result of one or more statements as an array. The result is always an array of 0 or more objects. PowerShell Copy.
The echo command is used to print the variables or strings on the console. The echo command has an alias named “Write-Output” in Windows PowerShell Scripting language. In PowerShell, you can use “echo” and “Write-Output,” which will provide the same output.
The While statement in PowerShell is used to create a loop that runs a command or a set of commands if the condition evaluates to true. It checks the condition before executing the script block. As long as the condition is true, PowerShell will execute the script block until the condition results in false.
Try this out in your PowerShell console:
"{0}`t{1,-15}{2,-15}{3,-15}" -f "Locale", "Jar", "HelpSet", "Exception"
You can use string formatting quite easily from PowerShell.
The -f
operator is a PowerShell short cut to the String.Format
function, including all the standard and custom formatting .NET types support.
I have accepted Davids answer as that's what I asked for. However, I have chosen to create an object by
try{
add-type @'
namespace FFPS {
public class Data {
public string Locale;
public string JarFile;
public string HelpSet;
public string CorrectName;
public string Status;
}
}
'@
}
catch{}
and then use XML format file to format it as a table
<?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>
and in the code do
$currentFile = New-Object ffps.data
$currentFile.Locale = "DE"
$currentFile.JarFile = "JarFile.Name"
...
$currentFile
to print the entries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With