2013-06-03 38 views
5

Bit của một câu hỏi đơn giản nhưng làm cách nào để chọn một cột cụ thể trong mã bên dưới?chỉ hiển thị một cột trong kết quả?

Tôi chỉ muốn hiển thị cột TIME và không có gì khác. Tôi thử và đưa vào LÚC NÀO FORMAT_TABLE nhưng nó chỉ populates với LÚC NÀO nhiều lần mà không thực sự hiển thị thời gian ..

$server_event = Get-Content -Path "c:\Temp\Servers.txt" 

foreach($servers in $server_event) 
{ 

$event = Get-EventLog -computerName $servers -logname system | Where-Object {$_.EventID -eq 6009} | Select -First 1 

$event 

} 

KẾT QUẢ: Tôi chỉ muốn hiển thị THỜI GIAN COLUMN

Index Time   EntryType Source     InstanceID Message                      
    ----- ----   --------- ------     ---------- -------                      
    78858 Jun 01 07:19 Information EventLog    2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Multiprocessor Free.      
    46221 Jun 01 07:20 Information EventLog    2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Multiprocessor Free.      
    214480 Jun 01 07:46 Information EventLog    2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Multiprocessor Free.      
    461238 Jun 01 07:18 Information EventLog    2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Multiprocessor Free.      
    70889 Jun 01 07:17 Information EventLog    2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Multiprocessor Free.      
    52397 Jun 01 07:19 Information EventLog    2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Multiprocessor Free.      
    42219 Jun 01 07:40 Information EventLog    2147489657 Microsoft (R) Windows (R) 5.02. 3790 Service Pack 2 Uniprocessor Free.  

Trả lời

6

thử:

$event = Get-EventLog -computerName $servers -logname system | Where-Object {$_.EventID -eq 6009} | Select TimeGenerated -First 1 
+1

ahhh ........ TimeGenerated! Đó là lý do tại sao nó đã gây nhầm lẫn cho tôi! làm thế nào để bạn tìm thấy tên chính xác của cột C.B.? – lara400

+0

Ngoài ra, hãy sử dụng 'Get-WinEvent' để thay thế. 'Get-EventLog' là dành cho [tương thích ngược] (http://technet.microsoft.com/en-us/library/hh849682.aspx) (xem phần Ghi chú). Oh, và sau đó là TimeCreated, không phải TimeGenerated. – vonPryz

+3

theo cách này: '(Get-EventLog -logname system) [0] | fl * 'nó trả về một danh sách tất cả các thuộc tính cho một eventlog duy nhất. Bạn cũng tìm thấy 'timewritten' nhưng IIRC mà bạn thấy theo định dạng powershell mặc định của' Timegenerated' –

2

Bạn có thể xem cách đặt lại tên bằng cách tìm trong tệp có tên DotNetTypes.format.ps1xml mục này nằm trong thư mục $pshome.

Nếu bạn tìm kiếm System.Diagnostics.EventLogEntry bạn sẽ thấy những điều sau đây, trong đó định dạng TimeGenerated như thời gian và hiển thị dưới dạng {0: MMM} {0: dd} {0: HH}: {0: mm}:

<View> 
    <Name>System.Diagnostics.EventLogEntry</Name> 
    <ViewSelectedBy> 
     <TypeName>System.Diagnostics.EventLogEntry</TypeName> 
    </ViewSelectedBy> 

    <TableControl> 
     <TableHeaders> 
      <TableColumnHeader> 
       <Label>Index</Label> 
       <Alignment>Right</Alignment> 
       <Width>8</Width> 
      </TableColumnHeader> 
      <TableColumnHeader> 
       <Label>Time</Label> 
       <Width>13</Width> 
      </TableColumnHeader> 
      <TableColumnHeader> 
       <Label>EntryType</Label> 
       <Width>11</Width> 
      </TableColumnHeader> 
      <TableColumnHeader> 
       <Label>Source</Label> 
       <Width>20</Width> 
      </TableColumnHeader> 
      <TableColumnHeader> 
       <Label>InstanceID</Label> 
       <Alignment>Right</Alignment> 
       <Width>12</Width> 
      </TableColumnHeader> 
      <TableColumnHeader> 
       <Label>Message</Label> 
      </TableColumnHeader> 
     </TableHeaders> 
     <TableRowEntries> 
      <TableRowEntry> 
       <TableColumnItems> 
        <TableColumnItem> 
         <PropertyName>Index</PropertyName> 
        </TableColumnItem> 
        <TableColumnItem> 
         <PropertyName>TimeGenerated</PropertyName> 
         <FormatString>{0:MMM} {0:dd} {0:HH}:{0:mm}</FormatString> 
        </TableColumnItem> 
        <TableColumnItem> 
         <ScriptBlock>$_.EntryType</ScriptBlock> 
        </TableColumnItem> 
        <TableColumnItem> 
         <PropertyName>Source</PropertyName> 
        </TableColumnItem> 
        <TableColumnItem> 
         <PropertyName>InstanceID</PropertyName> 
        </TableColumnItem> 
        <TableColumnItem> 
         <PropertyName>Message</PropertyName> 
        </TableColumnItem> 
       </TableColumnItems> 
      </TableRowEntry> 
     </TableRowEntries> 
    </TableControl> 
</View> 

vì vậy, bạn có thể nhận được chỉ là Cột Thời gian sử dụng TimeGenerated như sau:

Get-EventLog -LogName System | select TimeGenerated 
Các vấn đề liên quan