Reputation: 187
I want to have my script output status updates as it runs. It's a simple script that installs basic stuff as unattended as possible (sadly not fully unattended).
How you see below is how I've been doing this. I want this to be very human-readable, as I'm likely not the only one who's going to use this script.
I want to display two columns. One with the message saying what's happened, and one with what it's applied to.
I don't believe tables will work, as I want it to output each line individually in case if one actually fails hard enough to crash the script, or anything like that. To the best of my knowledge, tables are output all at once.
Currently I'm using strings with an excess of spaces. This works, but I feel like there has to be a more elegant way to do this. A way that I can just feed something a bunch of strings, finds the length of the longest one, and adds all the spaces necessary to make them the same length.
Current Variables
$Already = "Already Installed "
$Installing = "Installing "
$Installed = "Installed "
$Run = "Run "
$Warning = "WARNING "
$Failed = "FAILED "
$NotFound = "NOT FOUND "
$SWhatIf = "WhatIf "
Desired Output
Write-Host "${Installing}Google Chrome"
Installing Google Chrome
Write-Host "${Already}Adobe Reader"
Already Installed Adobe Reader
Write-Host "${Run}7-Zip"
Run 7-Zip
Upvotes: 0
Views: 47
Reputation: 61068
You could opt for creating a Hashtable instead of single variables and set all values to be of the same length. Something like this:
$actions = @{
Already = "Already Installed"
Installing = "Installing"
Installed = "Installed"
Run = "Run"
Warning = "WARNING"
Failed = "FAILED"
NotFound = "NOT FOUND"
WhatIf = "WhatIf"
}
# get the length of the longest value
$longest = ($actions.Values | Measure-Object -Property Length -Maximum).Maximum
# next update the values of the hashtable so all get the same length
foreach($key in $($actions.keys)){
$actions[$key] = ($actions[$key]).PadRight($longest, ' ')
}
Then use that $actions
hash like:
Write-Host "$($actions['Installing']): Google Chrome"
Write-Host "$($actions['Already']): Adobe Reader"
Write-Host "$($actions['Run']): 7-Zip"
Output:
Installing : Google Chrome Already Installed: Adobe Reader Run : 7-Zip
Upvotes: 1