powershell - Format Log Output -
my script backing multiple sql server databases , have outputting each database , file name log file. unsure how align them in column format.
function logwrite { param ([string]$logstring) add-content $logfile -value $logstring } $server = new-object ("microsoft.sqlserver.management.smo.server") "(local)\sqlexpress" $databases = $server.databases $array = @($db1,$db2,$db3,$db4,$db5,$db6,$db7,$db8,$db9,$db10,$db11) foreach ($element in $array) { foreach ($db in $databases) { if($db.name -like $element) { $dbname = $db.name $datetime = get-date -format yyyy_mm_dd $fullbackupfilepath = ($backupdirectory + "\" + $dbname + "_full_" + $datetime + ".bak") backup-sqldatabase -serverinstance $sqlserver -database $dbname -backupfile $fullbackupfilepath logwrite "$dbname - $backupdirectory" } } }
this looks like:
databases succesfully backed up: aqm c:\mappa_backup\full atlas c:\mappa_backup\full halive c:\mappa_backup\full hatrafficinfo c:\mappa_backup\full jtr c:\mappa_backup\full jtr2 c:\mappa_backup\full mappa c:\mappa_backup\full mtf c:\mappa_backup\full partoneclaims c:\mappa_backup\full ptfeedback c:\mappa_backup\full following files deleted older 14 days: aqm_full_2015_08_19.bak atlas_full_2015_08_19.bak halive_full_2015_08_19.bak hatrafficinfo_full_2015_08_19.bak jtr2_full_2015_08_19.bak jtr_full_2015_08_19.bak mappa_full_2015_08_19.bak mtf_full_2015_08_19.bak partoneclaims_full_2015_08_19.bak ptfeedback_full_2015_08_19.bak
another simple option use .net formatting directives e.g.:
logwrite ("{0,20} - {1,-60}" -f $dbname,$backupdirectory)
where format specifier {<placeholder-number>,[- left-align]<field-width>}
.
Comments
Post a Comment