当从本身包含字符串数组的 Get-EventLog 返回对象列表时,如何访问数组元素?
When a list of objects is retruned from Get-EventLog that itself contains an array of strings, how do I access the array elements?
我正在寻找已登录计算机的用户列表,并且正在使用以下 PowerShell 示例:
Get-EventLog security -source microsoft-windows-security-auditing |
where {($_.instanceID -eq 4624) -and ($_.replacementstrings[5] -eq 'username')} |
select -property timegenerated, replacementstrings
正在返回:
TimeGenerated ReplacementStrings
------------- ------------------
14/08/2019 08:50:34 {S-1-5-18, TT01022$, DOMAIN, 0x3e7...}
14/08/2019 08:50:34 {S-1-5-18, TT01022$, DOMAIN, 0x3e7...}
14/08/2019 07:45:08 {S-1-5-18, TT01022$, DOMAIN, 0x3e7...}
replacementstrings[5] 是用户名,我想包含它而不是整个数组。
但以下不起作用:
PS C:\> Get-EventLog security -source microsoft-windows-security-auditing |
where {($_.instanceID -eq 4624) -and ($_.replacementstrings[5] -eq 'user')} |
select -property timegenerated, replacementstrings[5]
TimeGenerated replacementstrings[5]
------------- ---------------------
14/08/2019 08:50:34
14/08/2019 08:50:34
14/08/2019 07:45:08
结果是空白。
我玩过 -ExpandProperty,但这里的输出不是我想要的,而且我似乎无法访问 TimeGenerated 属性:
PS C:\> Get-EventLog security -source microsoft-windows-security-auditing |
where {($_.instanceID -eq 4624) -and ($_.replacementstrings[5] -eq 'gary-smith')} |
select -property timegenerated -ExpandProperty replacementstrings |
format-table
S-1-5-18
TT01022$
DOMAIN
0x3e7
S-1-5-21-2072319296-1284187727-59193277-2383
user
DOMAIN
0x1eef92935
2
User32
Negotiate
TT01022
{00000000-0000-0000-0000-000000000000}
-
-
0
0xbd4
C:\Windows\System32\svchost.exe
127.0.0.1
0
%%1833
-
-
-
%%1843
0x1eef927e1
%%1843
S-1-5-18
TT01022$
DOMAIN
0x3e7
...
我只是在寻找输出:
TimeGenerated User
------------- ---------------------
14/08/2019 08:50:34 user
14/08/2019 08:50:34 user
14/08/2019 07:45:08 user
我将更改过滤器以查找多个用户,因此希望显示用户而不是从 input/filter 本身获取。
因此,最终产生我正在寻找的输出的代码是:
Get-EventLog Security -Source microsoft-windows-security-auditing |
Where {($_.instanceID -eq 4624) -AND ($_.replacementstrings[5] -LIKE "*user*")} |
Select-Object -Property TimeGenerated, @{ Name = 'User'; Expression = { $_.replacementstrings[5] }} |
Format-Table @{Name='Time Generated';Width=20;Expression={$_.TimeGenerated}},@{Name='User';Width=40;Expression={$_.User}}
这是使用计算得到的 属性(感谢 @Lee_Dailey 的帮助)并且正在格式化输出。
:)
我正在寻找已登录计算机的用户列表,并且正在使用以下 PowerShell 示例:
Get-EventLog security -source microsoft-windows-security-auditing |
where {($_.instanceID -eq 4624) -and ($_.replacementstrings[5] -eq 'username')} |
select -property timegenerated, replacementstrings
正在返回:
TimeGenerated ReplacementStrings
------------- ------------------
14/08/2019 08:50:34 {S-1-5-18, TT01022$, DOMAIN, 0x3e7...}
14/08/2019 08:50:34 {S-1-5-18, TT01022$, DOMAIN, 0x3e7...}
14/08/2019 07:45:08 {S-1-5-18, TT01022$, DOMAIN, 0x3e7...}
replacementstrings[5] 是用户名,我想包含它而不是整个数组。
但以下不起作用:
PS C:\> Get-EventLog security -source microsoft-windows-security-auditing |
where {($_.instanceID -eq 4624) -and ($_.replacementstrings[5] -eq 'user')} |
select -property timegenerated, replacementstrings[5]
TimeGenerated replacementstrings[5]
------------- ---------------------
14/08/2019 08:50:34
14/08/2019 08:50:34
14/08/2019 07:45:08
结果是空白。
我玩过 -ExpandProperty,但这里的输出不是我想要的,而且我似乎无法访问 TimeGenerated 属性:
PS C:\> Get-EventLog security -source microsoft-windows-security-auditing |
where {($_.instanceID -eq 4624) -and ($_.replacementstrings[5] -eq 'gary-smith')} |
select -property timegenerated -ExpandProperty replacementstrings |
format-table
S-1-5-18
TT01022$
DOMAIN
0x3e7
S-1-5-21-2072319296-1284187727-59193277-2383
user
DOMAIN
0x1eef92935
2
User32
Negotiate
TT01022
{00000000-0000-0000-0000-000000000000}
-
-
0
0xbd4
C:\Windows\System32\svchost.exe
127.0.0.1
0
%%1833
-
-
-
%%1843
0x1eef927e1
%%1843
S-1-5-18
TT01022$
DOMAIN
0x3e7
...
我只是在寻找输出:
TimeGenerated User
------------- ---------------------
14/08/2019 08:50:34 user
14/08/2019 08:50:34 user
14/08/2019 07:45:08 user
我将更改过滤器以查找多个用户,因此希望显示用户而不是从 input/filter 本身获取。
因此,最终产生我正在寻找的输出的代码是:
Get-EventLog Security -Source microsoft-windows-security-auditing |
Where {($_.instanceID -eq 4624) -AND ($_.replacementstrings[5] -LIKE "*user*")} |
Select-Object -Property TimeGenerated, @{ Name = 'User'; Expression = { $_.replacementstrings[5] }} |
Format-Table @{Name='Time Generated';Width=20;Expression={$_.TimeGenerated}},@{Name='User';Width=40;Expression={$_.User}}
这是使用计算得到的 属性(感谢 @Lee_Dailey 的帮助)并且正在格式化输出。
:)