2017-10-05 61 views
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 

回答

0

实际上,您可以通过传递要排序的属性的逗号分隔列表来排序多个属性。

甚至可以使用表达式将第一个属性按升序排序,而第二个属性按降序排序,反之亦然。

$list | sort Age,Weight 

# Using expression 
$list | sort Age, @{'e'={$_.Weight};a=1} -Descending 

完整的示例:

$list = New-Object -TypeName System.Collections.Generic.List[Object] 

$Properties = @('Age','Weight','Number','Tag') 
For ($i=0;$i -le 1000;$i++) { 
    $item = New-Object psobject 
    foreach ($P in $Properties) { 
     Add-Member -InputObject $item -MemberType NoteProperty -Name $p -value (Get-Random -Minimum 0 -Maximum 10) 
    } 
$list.Add($item) 
} 

$list | sort Age,Weight 

# Using expression 
$list | sort Age, @{'e'={$_.Weight};a=1} -Descending 

我不知道我理解你的“下一个部分的目标”,但在我看来,你可能只想做一个Select -First 1

+0

谢谢,我会尝试一下,看看会不会起作用。如果我选择了-first,那么这很有意义,但我可能必须按名称将它们组合在一起,然后选择每个组的第一个条目。 – RedThorn88