微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我需要从一个数组循环到另一个新数组

如何解决我需要从一个数组循环到另一个新数组

我正在对Windows的上次登录进行排序,我只想使用变量而不是文件,在文件中,它很容易操作,但速度很慢,因为它将使用磁盘访问而不是ram。

# lastlogon
$UserProperty = @{n="User";e={(New-Object System.Security.Principal.SecurityIdentifier $_.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])}}
$TypeProperty = @{n="Action";e={if($_.EventID -eq 7001) {"logon"} else {"logoff"}}}
$TimeProperty = @{n="Time";e={$_.TimeGenerated}}
$MachineNameProperty = @{n="MachinenName";e={$_.MachineName}}
$last1=Get-EventLog System -Source Microsoft-Windows-Winlogon -ComputerName localhost | select $UserProperty,$TypeProperty,$TimeProperty,$MachineNameProperty

foreach ($line in $last1) {
         $l = $line -split ' '
         $user1 = $l[0]
         $user0 = $user1 -replace ';'
         $user2 = $user0.Substring($user0.IndexOf("\")+1,$user0.Length-$user0.IndexOf("\")-1)
         $t1 = $l[2] -replace 'Time='
         $t2 = $l[3] -replace ';'
         $time1 = $t1+' '+$t2
         $last21 = $user2+' '+$time1
}

$last21将仅获取最新数据。如果我将其更改为+=,它将是一行。我希望我的$last21是这样的:

PS > $last21
user1 1/2/20 150000
user1 1/1/20 120000
user2 1/1/20 010000
.
.
.

感谢和强大的力量!

解决方法

无论您是谁,您都可以做到;)

$last21 = $last1
$c = 0
foreach ($line in $last1) {
    $l = $line -split ' '
    $user1 = $l[0]
    $user0 = $user1 -replace ';'
    $user2 = $user0.Substring($user0.IndexOf("\") + 1,$user0.Length - $user0.IndexOf("\") - 1)
    $t1 = $l[2] -replace 'Time='
    $t2 = $l[3] -replace ';'
    $t3 = $t1 + ' ' + $t2
    $last21[$c] = $user2 + ' ' + $t3
    $c += 1
}
$last2 = $last21 | sort -Descending

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。