logging - PowerShell Log Function Readability -
currently log function spits out information in single column , hard read. there way make split different columns each (displayname, poolname, poolsnapshot, , desktopsvivmsnapshot) , respective information put correctly?
function log ([string]$entry) { write-output $entry | out-file -append "c:\logs\snapshot.csv" } add-pssnapin quest.activeroles.admanagement $date = get-date -format "mm-dd-yyyy" $time = get-date -format "hh:mm:sstt" # begin log log $(get-date) log "the below desktops not using correct snapshot." if (@($desktopexceptions).count -lt 1) { write-output "all desktops in $pool using correct snapshots." | out-file -append "c:\logs\snapshot.csv" } else { write-output $desktopexceptions | select-object displayname,poolname,poolsnapshot,desktopsvivmsnapshot | sort displayname | out-file -append "c:\logs\snapshot.csv" } log $(get-date) 09/11/2017 12:16:17 displayname poolname poolsnapshot desktopsvivmsnapshot ----------- -------- ------------ -------------------- xxxc-13v xxxc-xxx /8-11-2017/09-07-2017 /8-11-2017 xxxc-15v xxxc-xxx /8-11-2017/09-07-2017 /8-11-2017 xxxc-1v xxxc-xxx /8-11-2017/09-07-2017 /8-11-2017 xxxc-20v xxxc-xxx /8-11-2017/09-07-2017 /8-11-2017
note: removed parts of log in hopes not make post long.
csv files require uniform lines: header line column names, followed data lines containing column values.
by writing output get-date first - single date/time string - followed single-string output, followed multi-column output $desktopexceptions | select-object ... call, you're definition not creating valid csv file.
if still want create such file:
log (get-date) # single command, don't need $(...) - (...) do. log "the below desktops not using correct snapshot." if ($desktopexceptions) # non-empty array / non-$null object { log ($desktopexceptions | select-object displayname,poolname,poolsnapshot,desktopsvivmsnapshot | sort-object displayname | convertto-csv -notypeinformation) } else { log "all desktops in $pool using correct snapshots." } log (get-date) by defining
log()function's parameter type[string], you're forcing stringification of whatever object pass it. stringification same when embed variable reference or command inside"..."(string expansion / interpolation) - not same default, when print console.out-file, contrast, does result in same output when printing console, which, however, format human consumption, not machine parsing (as csv is, instance).to csv-formatted output, must either use
export-csv- write directly file - orconvertto-csv- string representation.also note there's typically no reason use
write-outputexplicitly - command / expression's output not explicitly assigned variable / redirected (to file or$null) implicitly sent powershell's [success] output stream; e.g.,write-output get-datesameget-date.
Comments
Post a Comment