Get-PC/Private/Get-LastLoginTimesBak.ps1

42 lines
1.4 KiB
PowerShell
Raw Normal View History

2024-06-11 18:27:55 +00:00
function Get-LastLoginTimes {
[CmdletBinding()]
param (
[Parameter()]
[String]
$ComputerName = $ENv:COMPUTERNAME
)
$sb = {
$profiles = Get-CimInstance -Class Win32_UserProfile
$profiles | Where {$_.SID.length -gt 10} | Foreach-Object {
$sid = $_.SID
$prop = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$sid\"
if($prop.LocalProfileUnLoadTimeHigh -and $prop.LocalProfileUnLoadTimeLow){
$a = '{0:x}' -f $prop.LocalProfileUnLoadTimeHigh
$b = '{0:x}' -f $prop.LocalProfileUnLoadTimeLow
$dec = [bigint]::Parse("$a$b",'AllowHexSpecifier')
$time = w32tm /ntte $dec
$lastTime = [datetime]($time -split ' - ')[1]
}
else{
$lastTime = $null
}
[PSCustomObject]@{
Name = ($_.LocalPath -split 'C:\\Users\\')[1]
LocalPath = $_.LocalPath
LastLoginTime = $lastTime
}
}
}
if($ComputerName -eq $ENv:COMPUTERNAME){
Invoke-Command -ScriptBlock $sb | Sort-Object LastLoginTime -Descending
}
else{
Invoke-Command -ComputerName $ComputerName -ScriptBlock $sb | Select-Object Name, LocalPath, LastLoginTime | Sort-Object LastLoginTime -Descending
}
}