csv - Adding timestamps to every line in output file in Powershell -
i add timestamps test-connection results in output file. importing data csv file contain spot timestamp. create output file using export-csv shown in code snippet below. have tried various methods add timestamps every line in output file no success. latest attempt use filter , pipeline in failed. relatively new powershell please forgive sloppiness. thank in advance help.
$printerlist = import-csv "printerlist.csv" $releaselist = import-csv "releasestations.csv" #object list rows of csv file $filename = "printlog.csv" $filename2 = "releaselog.csv" #file name log file $printersdown = @() #string list printers down in email $printersdown += "****************" $printersdown += "* printer down *" $printersdown += "****************" $printersdown += "" $stationdown = @() $stationdown += "****************" $stationdown += "*release station down*" $stationdown += "****************" $stationdown += "" $downflag = 0 $downflag2 = 0 #flag check when send alert email filter timestamp {"$(get-date -format mm_dd_yy_hhmm):$_"} foreach ($printer in $printerlist){ if(test-connection -count 1 -quiet -computername $printer.ip){ $printer.status = "up" write-host ("{0}: up" -f $printer.printername) }else{ write-host ("{0}: down" -f $printer.printername) $printer.status = "down" $printersdown += ("{0} : {1}" -f $printer.printername, $printer.ip) $downflag = 1 } } foreach ($station in $releaselist){ if(test-connection -count 1 -quiet -computername $station.releasestation){ $station.status = "up" write-host ("{0}: up" -f $station.releasestation) }else{ write-host ("{0}: down" -f $station.releasestation) $station.status = "down" $stationdown += ("{0}" -f $station.releasestation) $downflag2 = 1 } } # write csv file $printerlist | export-csv -append -notypeinformation -path logs\$filename $releaselist | export-csv -append -notypeinformation -path logs\$filename2
you should add timestamp directly objects in lists. if csv has "timestamp" field available that, change objects this:
foreach ($printer in $printerlist){ if(test-connection -count 1 -quiet -computername $printer.ip){ $printer.status = "up" write-host ("{0}: up" -f $printer.printername) }else{ write-host ("{0}: down" -f $printer.printername) $printer.status = "down" $printersdown += ("{0} : {1}" -f $printer.printername, $printer.ip) $downflag = 1 } $printer.timestamp=(get-date -format mm_dd_yy_hhmm) } foreach ($station in $releaselist){ if(test-connection -count 1 -quiet -computername $station.releasestation){ $station.status = "up" write-host ("{0}: up" -f $station.releasestation) }else{ write-host ("{0}: down" -f $station.releasestation) $station.status = "down" $stationdown += ("{0}" -f $station.releasestation) $downflag2 = 1 } $station.timestamp=(get-date -format mm_dd_yy_hhmm) } should do. should work because export-csv enumerates fields in lists supplied, , since objects have field, exported properly.
Comments
Post a Comment