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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

David SankovskySenior 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.