我正在尝试做什么:使用get-winevent(ID:4624)使用Powershell获取用户登录统计信息。如果你知道更好的方法,我不反对其他建议。我已经让它工作了,但它没有格式化我需要它。这是我的Powershell代码(我是一个Powershell新手,但有些东西类似于PHP,我很满意):
$LogonIDs = "4624"
$computers = get-content "c:\computers.txt"
foreach($pc in $computers) {
if(test-connection $pc -count 1 -quiet) {
write-host "Acquiring information for $pc"
foreach ($item in $LogonIDs) {
(Get-WinEvent -computername $pc -max 2000 -FilterHashtable @{Logname='security';Id=$item} `
| Select TimeCreated,Message `
| fl * `
| findstr /G:c:\search.lst) `
-replace "^[\s]+","" `
-replace "[\s]+"," " `
| out-file -append "c:\scripts\winevent\$LogonIDs.txt"
}
}
else {
write-host "Couldn't ping $pc"
}
}
search.lst具有以下信息:
TimeCreated
Account Name:
Logon Type:
Logon GUID:
Logon Type:
Process Name:
脚本将导出如下内容:
TimeCreated : 5/2/2013 7:19:39 AM
Account Name: COMPUTERNAME$
Logon Type: 2
Account Name: [USERNAME]
Logon GUID: {00000000-0000-0000-0000-000000000000}
Process Name: C:\Windows\System32\winlogon.exe
TimeCreated : 5/2/2013 7:19:39 AM
Account Name: COMPUTERNAME$
Logon Type: 2
Account Name: [USERNAME]
Logon GUID: {AKEJ38DJ-3K45-3LKD-3LKD-DKEJ3787DJJ3}
Process Name: C:\Windows\System32\winlogon.exe
TimeCreated : 5/2/2013 6:50:42 AM
Account Name: -
Logon Type: 3
Account Name: COMPUTERNAME$
Logon GUID: {K458D890-3KJ8-DK3J-DK39-3LDJK23LD909}
Process Name: -
TimeCreated : 5/2/2013 6:27:22 AM
Account Name: COMPUTERNAME$
Logon Type: 5
Account Name: SYSTEM
Logon GUID: {00000000-0000-0000-0000-000000000000}
Process Name: C:\Windows\System32\services.exe
这个脚本可以很好地导出所有内容,但是我很难使用Powershell格式化和解析不需要的信息。我可以使用PHP格式化我需要的所有内容,但如果有人可以帮助我使用Powershell清除特定信息,那就非常出色。
1) Explode at TimeCreated to create array
2) If Logon Type = 2, proceed, else skip to next value;
3) If Process Name contains winlogon.exe, proceed, else skip to next value;
4) If GUID = {00000000-0000-0000-0000-000000000000}, skip to next value;
5) If GUID != {00000000-0000-0000-0000-000000000000}, Account Name[0] = computername, Account Name[1] = Username, build new array with this information
以下是我用过的PHP:
$file = "4624.txt";
$file_explode = explode("TimeCreated",$file);
while(list($k,$v) = each($file_explode)) {
$account_name = "";
$time = "";
if(preg_match("/Logon Type\:[\s]+2/msU",$v)) {
if(preg_match("/winlogon\.exe/msU",$v)) {
if(strstr($v,"{00000000-0000-0000-0000-000000000000}")) {
continue;
}
else {
preg_match("/[\s]+\:[\s]+(.*)$/msU",$v,$time);
preg_match_all("/Account Name\:[\s]+(.*)$/msU",$v,$account_name);
$new_arr[] = array(
"time" => $time[1],
"computer_name" => $account_name[1][0],
"user" => $account_name[1][1]
);
}
}
}
}
我无法使用PHP(我刚刚发现)的原因是我无法将4624.txt发布到我的Web服务器,因为它包含PII(个人身份信息)。如果有人能帮助我,我会永远欠你的债务;]
- 亚当
答案 0 :(得分:0)
一般来说,我通过在PowerShell中创建一个自定义对象来处理这类问题,其中存在我想要的每个数据的属性。一旦有了对象,就可以更容易地使用Sort-Object,Group-Object,Format-Object来操作数据。
$LogonIDs = 4624
$IgnoreLogonGuid = '{00000000-0000-0000-0000-000000000000}'
function Parse-LogonID4624Event
{
param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[psobject[]]
$EventLogEntry
)
Process {
foreach ($ev in $EventLogEntry)
{
$TimeCreated = [DateTime]::Parse($ev.TimeCreated)
$AccountName = 'not found'
if ($ev.Message -match '(?ims)New Logon:.*?Account Name:\s*(\S+)')
{
$AccountName = $matches[1]
}
$LogonGuid = 'not found'
if ($ev.Message -match '(?ims)^\s+Logon GUID:\s*(\S+)')
{
$LogonGuid = $matches[1]
}
$obj = [pscustomobject] @{
TimeCreated = $TimeCreated
AccountName = $AccountName
LogonGuid = $LogonGuid
Message = $ev.Message
}
$obj
}
}
}
Get-WinEvent -max 20 -FilterHashtable @{LogName='Security';ID=$LogonIDs} |
Parse-LogonID4624Event |
Where LogonGuid -ne $IgnoreLogonGuid
这种实施方法并没有抓住你需要的所有领域,但我认为你可以很容易地看到如何冲洗和重复您需要的其他字段。请注意,此实现使用PowerShell v3特定功能。如果您在v2上需要这个,请告诉我。它应该只需要一些调整。