2013-04-11 87 views
0

所以我需要一些幫助。我得到了一個項目,涉及編寫一個PowerShell腳本。我需要能夠從Windows 7計算機的安全日誌中獲取最新的SuccessAudit,ID = 4776。在PowerShell中解析PSCustomObject,事件日誌

我設法在幾個小時內完成這個任務,這是我第一次使用powershell。

$eventtime=Get-EventLog Security | {$_.EventId -eq 4776} | where {$_.entrytype -eq "SuccessAudit"} | Select-Object TimeGenerated | Select -first 1 

經由控制檯輸出(W/O設定一個變量):

TimeGenerated 
-------------------------- 
4/10/2013 10:35:01 PM 

如果我設置命令給一個變量,這是可變的結果:

@{TimeGenerated=4/10/2013 10:35:01 PM} 

我想削減變量,以便它只會等於日期和時間: 2013年4月10日下午10:35:01下午

幫助!我已經呆了好幾個小時了!

回答

2

嘗試:

$eventtime=Get-EventLog Security | ? {$_.EventId -eq 4776} | 
where {$_.entrytype -eq "SuccessAudit"} | 
Select-Object -expand TimeGenerated | Select -first 1 
+0

真棒!這工作。非常感謝你!! – ShadowM82 2013-04-11 06:10:30

+0

@ user2268898很高興幫助! – 2013-04-11 06:11:59

+0

@CB,這也適用於我,ty!通過只做Select-Object -Property,我得到了PSCustomObject的一個數組(Object []),通過改變使用-ExpandProperty,我得到了我想要的項目數組。 – AnneTheAgile 2015-07-01 18:54:58