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
IT_Support PrivateAsked:
Who is Participating?
 
David SankovskyConnect With a Mentor Senior SysAdminCommented:
It is possible that the session was not logged off and the you reconnected into an existing session.
Closing the RDP connection using the 'x' at the RDP session window doesn't log off the session and it will in essence continue counting the the as idle.
0
 
David SankovskySenior SysAdminCommented:
see if this link points you in the right direction
0
 
footechCommented:
@David - Please don't paste blind links with no description or summary of what's linked to.  It's against the guidelines here.
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
IT_Support PrivateAuthor Commented:
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

0
 
IT_Support PrivateAuthor Commented:
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.
0
 
David SankovskySenior SysAdminCommented:
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.
1
 
IT_Support PrivateAuthor Commented:
Ah ok. Thanks. Maybe I didn’t reboot after all.
0
 
David SankovskySenior SysAdminCommented:
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)
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.