首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否在结果中只显示一列?

是否在结果中只显示一列?
EN

Stack Overflow用户
提问于 2013-06-03 21:16:32
回答 2查看 21.7K关注 0票数 8

这是一个很简单的问题,但是如何在下面的代码中选择特定的列呢?

我只想显示时间栏,不想显示其他内容。我尝试放入FORMAT_TABLE时间,但它只是多次填充时间,而没有实际显示时间。

代码语言:javascript
复制
$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

} 

结果:我只想显示时间列

代码语言:javascript
复制
   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.     
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-03 21:22:03

尝试:

代码语言:javascript
复制
$event = Get-EventLog -computerName $servers -logname system | Where-Object {$_.EventID -eq 6009} | Select TimeGenerated -First 1
票数 12
EN

Stack Overflow用户

发布于 2013-06-03 21:31:37

通过查看位于$pshome目录中名为DotNetTypes.format.ps1xml的文件,您可以看到名称是如何重新格式化的。

如果搜索System.Diagnostics.EventLogEntry,您将看到以下内容,其中TimeGenerated的格式为时间,显示为{0:mm} {0:dd} {0:HH}:{0:mm}:

代码语言:javascript
复制
<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>

因此,您可以使用TimeGenerated获取时间列,如下所示:

代码语言:javascript
复制
Get-EventLog -LogName System | select TimeGenerated
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16897659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档