Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 导出具有精确时间的Windows日志_Powershell_Timestamp_Get Eventlog_Get Winevent - Fatal编程技术网

Powershell 导出具有精确时间的Windows日志

Powershell 导出具有精确时间的Windows日志,powershell,timestamp,get-eventlog,get-winevent,Powershell,Timestamp,Get Eventlog,Get Winevent,我正在尝试使用Get-WinEvent Powershell cmdlet导出Windows日志。下面的内容将为我提供所需的时间精度,但这只能为我提供时间戳。我需要将时间戳连接到包括机器名、事件id等的其他列 这个密码给我准确的时间戳 Get-WinEvent -LogName Application -MaxEvents 10 | Select-Object -Expand TimeCreated | ForEach-Object { $date = [DateTime]$_ $date

我正在尝试使用Get-WinEvent Powershell cmdlet导出Windows日志。下面的内容将为我提供所需的时间精度,但这只能为我提供时间戳。我需要将时间戳连接到包括机器名、事件id等的其他列

这个密码给我准确的时间戳

  Get-WinEvent -LogName Application -MaxEvents 10 | Select-Object -Expand TimeCreated | ForEach-Object { 
$date = [DateTime]$_
$date.ToString("yyyy-MM-dd HH:mm:ss")}
输出如下所示,这就是我想要的:

2018-12-06 08:52:28 
2018-12-06 08:52:28 
2018-12-06 08:51:32 
2018-12-06 08:51:31 
2018-12-06 08:51:31 
2018-12-06 08:51:31 
2018-12-06 08:51:31
2018-12-06 08:51:31 
2018-12-06 08:51:31 
2018-12-06 08:44:16
但是我需要输出包括精确的时间以及MachineName、EventID、LevelDisplayName、Message等内容。因此,在下面的命令中,我希望插入精确的时间,而不是“TimeCreated”

Get-WinEvent -LogName Application -MaxEvents 10 | Select-Object TimeCreated,Machinename,Id,LevelDisplayName,Message,Logname | ft

谢谢

要创建时间的精确格式,请使用计算属性

Get-WinEvent -LogName Application -MaxEvents 10 |
   Select-Object @{n='TimeCreated';e={$_.TimeCreated.ToString("yyyy-MM-dd HH:mm:ss")}},
                 Machinename,Id,LevelDisplayName,Logname,Message|Format-Table -auto
为了获得更高的精度,还可以包括秒的分数
(将
,f
,fffffff
附加到格式字符串)

编辑:我没有您的环境,但不需要编写主机。

这会将格式化的CreatedTime输出到csv

Get-WinEvent -LogName "Microsoft-Windows-TerminalServices-SessionBroker/Operational" `
             -ComputerName $SessionBroker -MaxEvents 150 | 
  Select-Object @{n='TimeCreated';e={$_.TimeCreated.ToString("yyyy-MM-dd HH:mm:ss")}}, 
                Machinename,Id,LevelDisplayName,Message,LogName,TaskDisplayName | 
    Export-Csv $RDSLogs\SessionBrokerOperational.csv -Append -NoTypeInformation

不必要的命令
| Select Object-Expand TimeCreated
排除所有其他属性,只需将其删除即可。要创建时间的精确格式,请使用计算属性(所有属性都在那里,但在输出时可能由于未知的区域设置而被剥离)。谢谢,这太完美了!这正是我需要它做的。我很感激!抱歉,还有一个后续问题。我有下面一行,当我“编写主机”时,它工作得很好。但是当我导出Csv时,“TimeCreated”列不会显示精确的时间。它显示原始时间。获取WinEvent-LogName“Microsoft Windows TerminalsServices SessionBroker/Operational”-ComputerName$SessionBroker-MaxEvents 150 |选择对象@{n='TimeCreated';e={$\ TimeCreated.ToString(“yyyy-MM-dd HH:MM:ss”)},机器名,Id,LevelDisplayName,消息,日志名,TaskDisplayName |导出Csv$RDSLogs\SessionBrokerOperational.Csv-Append-notypeinformation好吧,它更像是一条断断续续的后续评论。正如您所看到的,注释只是针对注释生成的,没有换行符的代码几乎不可读。编辑此问题以包含更多信息会更好,但会让我(和其他人)有机会以另一个好答案赢得声誉。