show just one column in result?

不羁岁月 提交于 2019-12-08 19:28:09

问题


Bit of a simple question but how do I select a specific column in the below code?

I only want to show TIME column and nothing else. I try and put in FORMAT_TABLE TIME but it just populates with TIME multiple times without actually showing the time..

$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

} 

RESULT: I ONLY WANT TO SHOW THE TIME 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.     

回答1:


try:

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



回答2:


You can see how the names get re-formatted by looking in the file named DotNetTypes.format.ps1xml this is located in the $pshome directory.

If you search for System.Diagnostics.EventLogEntry you'll see the following, which formats TimeGenerated as Time and the display as {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>

So you can get just the Time Column using TimeGenerated as follows:

Get-EventLog -LogName System | select TimeGenerated


来源:https://stackoverflow.com/questions/16897659/show-just-one-column-in-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!