【发布时间】:2017-10-06 06:53:45
【问题描述】:
有没有办法按用户名对 csv 表进行排序,然后对每个用户的创建时间列进行排序?
我想看看我是否可以对最近创建的时间列进行排序。
我正在编写一个脚本来查看安全事件日志中每个用户的最后一次登录。
我可以按名字排序,但写的时间没有任何顺序。
我的下一个目标是只显示为一个用户创建的最新时间条目,而不是其余条目。
$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
【问题讨论】:
标签: powershell