简体   繁体   English

在Powershell的输出文件的每一行中添加时间戳

[英]Adding timestamps to every line in output file in Powershell

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. 我正在从确实包含TIMESTAMP位置的csv文件中导入数据。 To create the output file I am using Export-Csv as shown in the code snippet below. 要创建输出文件,我正在使用Export-Csv,如下面的代码片段所示。 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. 我对Powershell相对较新,因此请原谅任何草率的行为。 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

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? 如何为Powershell和输出的各个行添加时间戳?

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: 假设您的CSV文件具有“时间戳”字段,则可以像这样更改对象:

    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. 这应该起作用,因为Export-CSV枚举提供的列表中的字段,并且由于所有对象现在都有另一个字段,因此它将正确导出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM