Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag for United States of America

asked on

Query logon user on a remote domain joined workstation using powershell.

Hello,

I am using the Get-ADComputer -Filter * -Properties * | select-object Name.... command trying to collect domain joined workstations in our AD.  

Does anyone know what parameter to use to have the samaccountname of the logon user to be returned in result?

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nav2567

ASKER

Hello David,

I have been using the below to check status of computers.  Would you include the find-logon-user command in there?

Import-Module ActiveDirectory
Import-Csv "C:\offlinecomputers.csv" | ForEach-Object {
      $out = $_ | Select-Object -Property Computers, LastLogonTimestamp, @{n='Found'; e={$true}}
      If ($adComputer = Get-ADComputer -Filter "Name -eq '$($_.Computers)'" -Properties lastLogonTimestamp) {
            $out.LastLogonTimestamp = [datetime]::FromFileTime($adComputer.lastLogonTimestamp)
      } Else {
            $out.Found = $false
      }
      $out
}
Avatar of nav2567

ASKER

Hello David,

I need to do this for a list of offline computers in a csv file.  I do not know how to incorporate your logon user methodology into something which is already working for me in getting other computer attributies.  

Is it possible?
How about this one:

$Servers = Import-Csv "C:\offlinecomputers.csv"

#Initialize $Sessions which will contain all sessions
[System.Collections.ArrayList]$Sessions = New-Object System.Collections.ArrayList($null)

#Go through each server
Foreach ($Server in $Servers) {
    #Get the current sessions on $Server and also format the output
    $DirtyOuput = (quser.exe /server:$Server) -replace '\s{2,}', ',' | ConvertFrom-Csv

    #Go through each session in $DirtyOuput
    Foreach ($session in $DirtyOuput) {
        #Initialize a temporary hash where we will store the data
        $tmpHash = @{ }

        #Check if SESSIONNAME isn't like "console" and isn't like "rdp-tcp*"
        If (($session.sessionname -notlike "console") -AND ($session.sessionname -notlike "rdp-tcp*")) {
            #If the script is in here, the values are shifted and we need to match them correctly
            $tmpHash = @{
                Username    = $session.USERNAME
                SessionName = "" #Session name is empty in this case
                ID          = $session.SESSIONNAME
                State       = $session.ID
                IdleTime    = $session.STATE
                LogonTime   = $session."IDLE TIME"
                ServerName  = $Server
            }
        }
        Else {
            #If the script is in here, it means that the values are correct
            $tmpHash = @{
                Username    = $session.USERNAME
                SessionName = $session.SESSIONNAME
                ID          = $session.ID
                State       = $session.STATE
                IdleTime    = $session."IDLE TIME"
                LogonTime   = $session."LOGON TIME"
                ServerName  = $Server
            }
        }
        #Add the hash to $Sessions
        $Sessions.Add((New-Object PSObject -Property $tmpHash)) | Out-Null
    }
}

#Display the sessions, sort by name, and just show Username, ID and Server
$sessions | Sort-Object Username | Select-Object Username, ID, ServerName | Format-Table

Open in new window

Avatar of nav2567

ASKER

Hello David,

I have tried your command but I do not see any user name being returned on a couple of live computers.  Any idea?

Get-WmiObject -class win32_computersystem -ComputerName ‘computername’ | ForEach-Object {$_.Username}
Get-CimInstance  –ComputerName CLIENT1 -ClassName win32_computersystem  | Select-Object -ExpandProperty username

Open in new window

I need to do this for a list of offline computers  Offline computers don't have a logged in user.
Avatar of nav2567

ASKER

Thanks, everyone!