Workflow, Get-lastlogged on User foreach Computer in the company

This topic contains 2 replies, has 2 voices, and was last updated by  Luca Wirths 3 months, 1 week ago.

  • Author
    Posts
  • #78923

    Luca Wirths
    Participant

    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
    
    
    
  • #78928

    edwin arlington
    Participant

    You can try this command:

    $MachineList = Get-Content -Path c:\ListOfMachines.txt; # One system name per line
    foreach ($Machine in $MachineList){
    ($Machine + ": " + @(Get-WmiObject -ComputerName $Machine -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName);
    }

    Here are related articles for help.
    https://gallery.technet.microsoft.com/scriptcenter/Get-Active-Directory-User-bbcdd771

    https://gallery.technet.microsoft.com/scriptcenter/Get-Active-Directory-user-246f17c7

    https://www.lepide.com/freetools/last-logon-reporter.html

  • #78931

    Luca Wirths
    Participant

    Sorry, I was unclear about my result I want to achieve.
    I would like to get the last Computer where a User was logged on. The time when it happend is clear.
    So my plan is to get all PC last logged on user and then select the PC where the specific user was logged on.

You must be logged in to reply to this topic.