Current logged on user using Powershell script

Hi,

i found a vb script which works fine and gives me desired output however i am looking to get this converted to powershell, so that i can add this to an existing shell script

Purpose : the shell script should display/Echo currently logged on user, by reading the wmi

Below is the vb script which i found, also i dont want its first line because i will be running the script locally on the computer with admin rights

--------

strComputer = "atl-ws-01" Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = 'explorer.exe'") If colItems.Count = 0 Then Wscript.Echo "No one is logged on to the computer." Else For Each objProcess in colItems objProcess.GetOwner strUser, strDomain Wscript.Echo strDomain & "\" & strUser Next End If

--------

Regards

Tanoj

January 29th, 2015 4:04pm

Have you looked in the script repository?

Get-LoggedOnUser

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 4:09pm

https://social.technet.microsoft.com/Forums/windowsserver/en-US/45c541e0-fb49-4ac7-bcaf-738315a70863/getprocess-and-username
January 29th, 2015 4:20pm

Hi,

I think i stumbled on it but it failed to work on vm's, and the entry for username under the class "win32_loggedonuser"  was blank if i remember correctly, it then i tried searching for other class which can help me and found this, but not sure how to draft it to work with shell script

"Select * from Win32_Process Where Name = 'explorer.exe'

I forgot to mention that iam Novice in PS

Regards

Tanoj


Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 4:28pm

That's actually quite trivial in Powershell

Get-WmiObject -Class win32_process -filter "Name = 'explorer.exe'"

If you're working with SCCM 2012, and are struggling with that, you've got a lot of catching up to do.

January 29th, 2015 4:38pm

A VM does not have a current user and you must querry with teh VM public name.

The explorer process with tell you who is logged in.

Get-WmiObject -Classwin32_process -filter "Name = 'explorer.exe'" -computer VMNAME

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 6:24pm

Thank you for the reply

yes mjolinor you caught me, i am working with sccm 2012 trying to create a universal script which will work flawlessly on VM, windows 8, Windows 7 etc ....

so which one you think is good to fullfill my need,

Get-WmiObject -Class win32_process -filter "Name = 'explorer.exe'" -computer VMNAME    ?

 also it will be great help if you could help me get the complete script wich includes the above line,

i get following error when executing the above command

Regards

Tanoj


January 30th, 2015 9:11am

Thank you for the reply

yes mjolinor you caught me, i am working with sccm 2012 trying to create a universal script which will work flawlessly on VM, windows 8, Windows 7 etc ....

so which one you think is good to fullfill my need,

Get-WmiObject -Class win32_process -filter "Name = 'explorer.exe'" -computer VMNAME    ?

 also it will be great help if you could help me get the complete script wich includes the above line,

i get following error when executing the above command

Regards

Tanoj


Free Windows Admin Tool Kit Click here and download it now
January 30th, 2015 9:11am

Hi Tanoj,

To query wmi class in powershell, you can also use the query prameter, and the script is for your reference:

$process = Get-WmiObject -Query "Select * from Win32_Process Where Name = 'explorer.exe'"
 if ($process.Count -eq 0){
Write-Host "No one is logged on to the computer."
}

If you want to run the script above remotely, you can add the parameter -computername to the cdmlet "Get-WMIObject".

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

February 9th, 2015 4:22am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics