Link to home
Get AccessLog in
Avatar of IT_Support Private
IT_Support Private

asked on

Powershell Script to Get Logged In Status of a Remote Computer

Hi All,
I have a working script that shows me the logged in user and time it was logged in for the local computer, though how can I edit this to prompt me for the remote machine name so that I can search other pc's in the domain.
As I need to use my admin account, I was planning o using get-credentials at the beginning of it, but not sure how I should setup the prompt for the Remote Hostname.
I'm not too sure why the script shows me all the details in the result, though it gives me the details required so not too fussed about it.
Thanks

$explorerprocesses = @(Get-WmiObject -Query "Select * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue)
If ($explorerprocesses.Count -eq 0)
{
    "No explorer process found / Nobody interactively logged on"
}
Else
{
    ForEach ($i in $explorerprocesses)
    {
        $Username = $i.GetOwner().User
        $Domain = $i.GetOwner().Domain
        Write-Host "$Domain\$Username logged on since: $($i.ConvertToDateTime($i.CreationDate))"
    }
}[embed=file 1204108]

Open in new window

Capture.PNG
Avatar of David Sankovsky
David Sankovsky
Flag of Israel image

see if this link points you in the right direction
@David - Please don't paste blind links with no description or summary of what's linked to.  It's against the guidelines here.
Avatar of IT_Support Private
IT_Support Private

ASKER

Update,
I've opted to use this below easier method, though a bit puzzled with the output.
$TargetHostname = 'localhost'

query user /server:$TargetHostname

Open in new window


Output i get is: over 1 day, how can it be over one day idle if the laptop was just turned on about 1.5 hrs ago?

PS C:\WINDOWS\system32> C:\Scripts\LoggedOn\LoggedOn_Check_Query.ps1
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 admin                 console            23  Active    1+07:10  6/11/2017 10:22 PM

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Sankovsky
David Sankovsky
Flag of Israel image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Hi David, this eas run locally on my laptop which i rebooted and ligged into locally at home. I could possibly be wring and perhaps ligged off and on instead of rebooting but pretty sure i rebooted it.
I made several tests on my end, with varying Operating systems (both server and client, ranging from server 2k3 to 2012R2 and Windows 7 to 10 respectively) and at all times, the script reported the correct Idle time for the user I was querying.
Ah ok. Thanks. Maybe I didn’t reboot after all.
Give it a try and update us.
Don't forget to close the question and choose the answer that helped you reach a solution (if any)