Reputation: 13
I would like to add timestamps to my test-connection results in the output file. I am importing data from a csv file which does contain a spot for TIMESTAMP. To create the output file I am using Export-Csv as shown in the code snippet below. I have tried various methods to add timestamps to every line in the output file with no success. My latest attempt was to use a filter and then pipeline that in but that also failed. I am relatively new to Powershell so please forgive any sloppiness. Thank you in advance for any help.
$printerList = Import-Csv "printerList.csv"
$releaseList = Import-Csv "releasestations.csv" #Object List for rows of csv file
$fileName = "printlog.csv"
$fileName2 = "releaselog.csv" #file name for log file
$printersDown = @() #string to list printers down in email
$printersDown += "****************"
$printersDown += "* Printer Down *"
$printersDown += "****************"
$printersDown += ""
$stationDown = @()
$stationDown += "****************"
$stationDown += "*Release Station Down*"
$stationDown += "****************"
$stationDown += ""
$downFlag = 0
$downFlag2 = 0 #flag to check when to 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
Upvotes: 1
Views: 4431
Reputation: 18757
You should add the timestamp directly to the objects in your lists. Say if your CSV has a "timestamp" field available for that, you then change your objects like 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. This should work because Export-CSV
enumerates fields in lists supplied, and since all objects now have another field, it will get exported properly.
Upvotes: 0
Reputation: 1349
You could use a filter:
filter timestamp {"$(Get-Date -Format o): $_"}
$result = & ping 192.168.1.1 | timestamp
From How to add timestamps to individual lines of Powershell & output?
Upvotes: 1