Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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/6/opengl/4.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是否仅显示消息属性的第一行?_Powershell_Parsing - Fatal编程技术网

PowerShell是否仅显示消息属性的第一行?

PowerShell是否仅显示消息属性的第一行?,powershell,parsing,Powershell,Parsing,获得PowerShell许可,遇到无数问题(缺乏知识) 我试图按计数列出我自己的Windows安全日志记录事件,但我也可以使用一个“友好”描述字段,它恰好是“Message”属性的第一行。我想不出一个方法来提取它 因此,我将运行以下程序以获得事件的概述: PS C:\TEST> Get-WinEvent -FilterHashtable @{logname="security"}| Group-Object id -NoElement | sort count Count Name --

获得PowerShell许可,遇到无数问题(缺乏知识)

我试图按计数列出我自己的Windows安全日志记录事件,但我也可以使用一个“友好”描述字段,它恰好是“Message”属性的第一行。我想不出一个方法来提取它

因此,我将运行以下程序以获得事件的概述:

PS C:\TEST> Get-WinEvent -FilterHashtable @{logname="security"}| Group-Object id -NoElement | sort count

Count Name
----- ----
    1 4724
    1 4722
    1 1102
    1 4725
    2 4718
    2 6408
    2 4739
    2 1101
    2 5038
    2 4737
    3 4717
    4 6407
    4 4731
   10 4738
   16 1100
   19 4781
   22 4904
   22 4905
   35 6406
   38 5033
   38 5024
   39 4826
   39 4608
   39 4902
   40 4735
  113 4647
  156 4616
  239 5059
  355 4688
  551 4733
  557 4732
  605 4797
  965 5061
  977 5058
 1647 4798
 6364 4907
 6759 4634
 7000 4648
10950 4799
19407 4672
22049 4624
但我想包括“Description/Message”列,以显示每个事件ID对应的内容。例如,对于事件ID,
消息
属性包含以下值(?):

在这整条信息中,我只想摘录以下几行:

An attempt was made to reset an account's password.
因此,回到我最初的观点,理想情况下,它将显示以下内容:

Count Name Message
----- ---- ----
    1 4724 An attempt was made to reset an account's password.
    1 4722 A user account was enabled.
    1 1102 The audit log was cleared.
(...)
试试这个:

$Events = Get-WinEvent -FilterHashtable @{logname="security"} | Group-Object id

$Events | Select-Object Count,Name,@{Name='Message';Expression={ (($_.Group.Message | Select -First 1) -Split "`n")[0] }} | Sort-Object Count -Descending | Format-Table -Wrap
通过删除
组对象
-NoElement
参数,我们可以得到返回的
结果,然后从中检索消息属性的第一行

我们使用
selectobject
将计算属性添加到包含消息的结果中

还可以使用
Format Table-Wrap
,这样最终输出的视图不会截断第一行(如果第一行很长)

示例输出:

Count Name Message                                         
----- ---- -------                                         
   81 4798 A user's local group membership was enumerated. 
   13 5379 Credential Manager credentials were read.       
    5 5061 Cryptographic operation.                        
    1 5058 Key file operation.      

因此,您必须使用foreach迭代组,从组的第一个元素获取消息,在
r?
n”
处拆分它,然后只输出第一行。这是gold!我需要更好地理解表达式部分
($.Group.Message | Select-First 1)-Split“
n”)[0]`您为每个对象选择
$
组.Message属性,然后在战后根据换行符选择
第一个对象(?)和
Split
它,对吗?
[0]
在做什么?我假设它正在选择第1行(从0开始)?我将有后续问题,理想情况下,我希望根据其内容将整个
message
属性拆分为每个事件id的键/值对象……是的,因为
组。message
将是该组中所有事件的消息数组,我们使用
选择-First 1
仅获取第一个。然后
-split
操作符返回一个字符串数组,因此我们使用
[0]
返回该数组的第一个元素(数组从0开始计数)。您可以使用
Get Member
浏览对象的属性:有时,将对象通过管道传送到
Fomat List*
也很有用,可以查看所有属性及其内容(至少在第一级)。试试看
$\u124;格式列表*
KevMar的深潜技术非常好。试试这个:再来一次,谢谢!这正是我想要找到和理解的(来自bash,那里的东西不是对象)!我不知道如何列出可用的属性,现在我知道了:我可以通过以下方式分解事件:
$event4672=Get WinEvent-FilterHashtable@{logname=“security”;id=4672}-MaxEvents 1
$event4672 | gm-MemberType属性
以及最后的
$event4672.Properties
因此我现在了解了索引的值从何而来
$event4672.Properties[0]
Count Name Message                                         
----- ---- -------                                         
   81 4798 A user's local group membership was enumerated. 
   13 5379 Credential Manager credentials were read.       
    5 5061 Cryptographic operation.                        
    1 5058 Key file operation.