将时间和日期戳添加到导出的文件

时间:2018-10-10 11:19:39

标签: powershell date time

我正在使用以下脚本。但是我似乎无法获得输出的excel文件的日期/时间戳。理想情况下,我想多次运行此脚本,每次都按日期/时间顺序保存一个不同的文件名。

目前,它仅保存为一个文件,每次运行后都会被重写。

多台计算机,服务器搜索代码:

$Output = @()
$computername = "COMPUTERNAME" #Put name of computer(s) and or Server(S)
ForEach($Computer in $computername)
{
    $events = Get-WinEvent -Computername $Computer -FilterHashtable @{
        LogName = "application"
        Level = 2   #1 Critical, 2 Error , 3 Warning, 4 Information
    }
    ForEach($event in $events)
    {
        $output += $event | Add-Member -NotePropertyName 'computername' -NotePropertyValue $Computer -PassThru
    }
}
$CurrentDate = Get-Date
$CurrentDatev= $CurrentDate.ToString("dd-MM-yyyy---hh-mm-ss") + ".csv"
$Output | Export-Csv C:\adamtest100.csv -NoTypeInformation

1 个答案:

答案 0 :(得分:3)

更改代码的最后两行-

$CurrentDatev = $CurrentDate.ToString("dd-MM-yyyy---hh-mm-ss")
$Output | Export-Csv C:\adamtest100_$($CurrentDatev).csv -NoTypeInformation
相关问题