Hi everyone
I am a newbie in powershell, so my code is not perfect.
I am creating a script where I get the last logged on User on every computer in the company. It works, but it is slow. I saw the Worflow cmdlet and tried with this one, but it only returns 8 PCs and not all. Does someone have an idea?
Thanks
function Get-LastLoggedOnUser($computerName)
{
Get-WmiObject Win32_NetworkLoginProfile -Computer $computerName |
Sort -Descending LastLogon |
Select * -First 1 |
? {$_.LastLogon -match "(\d{14})"} |
% {
New-Object PSObject -Property @{
Name=$_.Name;
computername = $computerName
}
}
}
workflow Get-LastLoggedUseronPC
{
$userList = @()
foreach -parallel ($computer in $computerList)
{
if (-not (Test-Connection -ComputerName $computer.DNSHostName -Quiet -count 1)){}
else{
$WORKFLOW:userList += Get-LastLoggedOnUser -computerName $computer.Name
}
}
}
#Script
$computerList = Get-ADComputer -Filter{name -like "*"}| where {$_.name.length -le 8}
Get-LastLoggedUseronPC
#Result
PS C:\WINDOWS\system32>$userList
computername Name
------------ ----
PC100001 PC100001\MyUser
PC100002 PC100002\otherUser
PC100003 PC100003\anotherUser
. .
. .
. .
PC100008 PC100008\testuser