更新时间:2022-12-25 16:01:54
旁注:
$Compu
$Compu
话虽如此,我认为您无法从 WMI Win32_ComputerSystem 类中获得所需的信息..
您需要做的是解析计算机事件日志中的信息:
Having said that, I don't think you can get the info you need from the WMI Win32_ComputerSystem class..
What you will need to do is to parse the info from the computers eventlog:
# get an array of computernames loaded from the text file
$machines = Get-Content -Path C:UserskhalifamDesktopWinverMachineNames.txt
$result = foreach ($computer in $machines) {
# test if the compurer is on-line
if (!(Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
Write-Warning "Computer '$computer' is off-line."
# skip this computer and carry on with the next iteration
continue
}
# search the computers eventlog and parse the username and last logon time from that
# you can play around with other values for -MaxEvents if you feel you're missing information.
Get-WinEvent -ComputerName $computer -FilterHashtable @{Logname='Security';ID=4672} -MaxEvents 20 |
Where-Object { $_.Properties[1].Value -notmatch 'SYSTEM|NETWORK SERVICE|LOCAL SERVICE' } |
Select-Object @{Name ='ComputerName'; Expression = {$_.MachineName}},
@{Name ='UserName'; Expression = {$_.Properties[1].Value}},
@{Name ='LastLogon'; Expression = {$_.TimeCreated}} -First 1
}
# show on screen:
$result | Format-Table -AutoSize
# save as CSV file
$result | Export-Csv -Path 'D:LastLogonInfo.csv' -NoTypeInformation
更新
如果我正确理解您的评论,您希望获得所有用户(少数除外)的列表,并从列表中检索他们在计算机上的最新登录信息.
If I understand your comment correctly, you would like a list of all users (except for a few) and retrieve their latest login on a computer from the list.
在这种情况下,您可以执行以下操作:
In that case you can do the following:
# get an array of computernames loaded from the text file
$machines = Get-Content -Path C:UserskhalifamDesktopWinverMachineNames.txt
$result = foreach ($computer in $machines) {
# test if the compurer is on-line
if (!(Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
Write-Warning "Computer '$computer' is off-line."
# skip this computer and carry on with the next iteration
continue
}
# you do not want to include these account logins
$exclude = '$|SYSTEM|NETWORK SERVICE|LOCAL SERVICE|KHALIFAM'
# search the computers eventlog and parse the username and last logon time from that
# you can play around with other values for -MaxEvents if you feel you're missing information.
Get-WinEvent -ComputerName $computer -FilterHashtable @{Logname='Security';ID=4672} -MaxEvents 100 |
Where-Object { $_.Properties[1].Value -notmatch $exclude } |
Select-Object @{Name ='ComputerName'; Expression = {$_.MachineName}},
@{Name ='UserName'; Expression = {$_.Properties[1].Value}},
@{Name ='LastLogon'; Expression = {$_.TimeCreated}} |
Group-Object -Property UserName | ForEach-Object {
$_.Group | Sort-Object LastLogon -Descending | Select-Object -First 1
}
}
# show on screen:
$result | Format-Table -AutoSize
# save as CSV file
$result | Export-Csv -Path 'D:LastLogonInfo.csv' -NoTypeInformation