0
是否有一种方法可以按用户名对csv表进行排序,然后为每个用户排序创建时间列?Powershell - 按用户名和创建时间对表进行排序
我想看看我是否可以从最近的回来排序创建时间列。
我正在研究脚本以查看安全事件日志中每个用户的最后一次登录。
我可以按名称排序,但写入的时间不是按任何顺序。
我的下一个目标是显示最近一次为一位用户创建的条目,而不是其他条目。
$time = (Get-Date) – (New-TimeSpan -Day 30)
$ComputerName = $env:COMPUTERNAME
#Delete any previously created files
Get-ChildItem -Path "C:\PowerShellScripts\LastLogon\Results" -Recurse |
Where-Object CreationTime -lt (Get-Date).AddDays(-0) | Remove-Item -
ErrorAction SilentlyContinue
$hastable = Get-WinEvent -FilterHashtable
@{Logname='Security';ID=4624;starttime=$time} -ComputerName $ComputerName |
? {($_.Properties[8].Value -eq '10') -and
($_.Properties[5].Value -ne 'SYSTEM') -and
($_.Properties[5].Value -ne 'Agvadmin') -and
($_.Properties[5].Value -ne 'Agvance') -and
($_.Properties[5].Value -ne 'ssi1') -and
($_.Properties[5].Value -ne 'ssi2') -and
($_.Properties[5].Value -ne 'ssi3') -and
($_.Properties[5].Value -notmatch 'DWM') -and
($_.Properties[5].Value -notmatch 'Default') -and
($_.Properties[5].Value -notmatch 'TS6') -and
($_.Properties[5].Value -notmatch 'DC1') -and
($_.Properties[5].Value -notmatch 'DC1') -and
($_.Properties[5].Value -notmatch 'SQLTELEMETRY') -and
($_.Properties[5].Value -notmatch 'MSSQL') -and
($_.Properties[5].Value -notmatch "$env:COMPUTERNAME") -and
($_.Properties[5].Value -ne 'PANKAHLERSERVER$') -and
($_.Properties[5].Value -ne 'GREKAHLERSRVR$') -and
($_.Properties[5].Value -ne 'DIEKAHLERSERVER$') -and
($_.Properties[5].Value -ne 'ANONYMOUS LOGON') -and
($_.Properties[5].Value -ne 'LOCAL SERVICE') -and
($_.Properties[5].Value -ne 'NETWORK SERVICE') -and
($_.Properties[5].Value -ne 'ssiadmin') -and
($_.Properties[5].Value -ne 'Administrator')
} |
select @{N='User';E={$_.Properties[5].Value}} , @{N='TimeCreated';E=
{$_.TimeCreated}} , @{l="Logon Type";e={$_.Properties[8].Value}}
$hastable.GetEnumerator()| Sort -Property User |
Export-Csv C:\PowerShellScripts\Lastlogon\Results\LastLogon.csv -
NoTypeInformation
#The user count is created here
$number = (Import-Csv C:\PowerShellScripts\Lastlogon\Results\LastLogon.csv |
measure | % { $_.Count})
#The file is renamed to include computername, date, and user count
rename-item -path C:\PowerShellScripts\Lastlogon\Results\LastLogon.csv -
NewName
C:\PowerShellScripts\Lastlogon\Results\LastLogon-$ComputerName-$CurrentDate-
UserCount-$number.csv
谢谢,我会尝试一下,看看会不会起作用。如果我选择了-first,那么这很有意义,但我可能必须按名称将它们组合在一起,然后选择每个组的第一个条目。 – RedThorn88