繁体   English   中英

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

[英]I need to loop from one array to another new array

我在上次登录时对 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM