我有一个powershell脚本,它正在按预期工作。我需要一些格式化输出的帮助。
$Date = (Get-Date).AddDays(-1)
Get-ChildItem –Path "D:Log" -Recurse | Where-Object {($_.LastWriteTime -lt $Date)} | Remove-Item
$filter = @{
LogName='Application'
StartTime=$Date
}
Get-WinEvent -FilterHashtable $filter | Select-Object TimeCreated,Message |
Where-Object { $_.Message -like '*renamed*' -and $_.Message -notlike "*csv*" } |
Out-File -FilePath D:LogDailyReport_$(get-date -Format yyyyddmm_hhmmtt).txt
输出为
TimeCreated Message
----------- -------
4/16/2020 4:03:30 AM 04712: renamed
我需要输出
Date Time,File Name
4/16/2020 4:03:30 AM, 04712: renamed
列标题需要用逗号重命名。任何帮助都将不胜感激。
谢谢,
Arnab
您可以使用Select-Object
重命名属性,并使用ConvertTo-Csv
或Export-Csv
:转换为逗号分隔的值
# ...
Get-WinEvent -FilterHashtable $filter |
Select-Object TimeCreated,Message |
Where-Object { $_.Message -like '*renamed*' -and $_.Message -notlike "*csv*" } |
Select-Object @{Name='Date Time';Expression='TimeCreated'},@{Name='File Name';Expression='Message'} |
Export-Csv D:LogDailyReport_$(get-date -Format yyyyddmm_hhmmtt).txt -NoTypeInformation