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