1

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
3
  • possible duplicate of How to add timestamps to individual lines of Powershell & output?. You have made a filter but I don't see you using it anywhere Commented Jul 7, 2015 at 14:54
  • 1
    @Matt Not an exact duplicate, OP is at least aware of filters, he just can't apply a filter to exporting in CSV - this is done in a different way ;) Commented Jul 7, 2015 at 14:59
  • @Vesper the application of the filter is covered in the dup as well. The op only did half the work. But yeah, since it is csv export properties are the way to go. Commented Jul 7, 2015 at 15:05

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Don't think you have enough rep for this but this should just be flagged as a dup and not repeated as answer.
@Matt Flagging posts is available only at 15 rep.
@Matt The post you mentioned was one of the first posts I looked at for help. That post did not resolve my problem however it did give me the idea of using a filter, which I could not get to work. After extensive searching, I could not find anything else here or on any other forum that answered my question. So I realize that this might look like a duplicate post but in all actuality I am using a different method of exporting my data.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.