【问题标题】:Powershell - Sorting table by username and Time CreatedPowershell - 按用户名和创建时间排序表
【发布时间】: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


    【解决方案1】:

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

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

    $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

    【讨论】:

    • 谢谢,我会试试看是否可行。如果我执行 select -first 这将是有意义的,但我可能必须按名称将它们分组在一起并选择每个组的第一个条目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    相关资源
    最近更新 更多