Link to home
Start Free TrialLog in
Avatar of ankuratvb
ankuratvbFlag for United States of America

asked on

WMI:Get current Logged on User(Domain User Account)

Hi,

I need to get the current logged on user using WMI.

I tried using Win32_ComputerSystem but that gives the username only if the logged in user is using a local account.

I need to know the username even if the user is using a domain account.

Pls help.

Note:I just came across the Win32_NetworkLoginProfile class which contains some relevant information.Can anyone help?
Avatar of RonaldBiemans
RonaldBiemans

have you tried just using    environment.username
Avatar of ankuratvb

ASKER

Hi RonaldBiemans,

I need the current logged on username of a remote computer that i'll connect to using WMI.

I guess environment.username would give me the username of the local system.
yes environment.username just get the local system

have you tried using the networkconnection of WMI (Win32_NetworkConnection)

this has a username property aswell
Hi Mikal613,

Thanx for the link but it doesnt have the solution,the problem is the same but
the solution wasnt attained.
ASKER CERTIFIED SOLUTION
Avatar of armoghan
armoghan
Flag of Pakistan 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
User has not tested/commented on my Comment.
Its a post on how to get the Current User using Win32_Process

Yes,its true.

I havent been able to test out the method provided in the link. i was out for a while

I guess it'd be fair to award the points to armoghan with grade 'B'.
I searched and searched for a solution to the same problem.  I use Perl for everything, so I was looking for a Perl solution.  I came across this posting and several like it.  After adapting a VB script, I just wanted to throw this out as a Perl solution to the problem.  I understand that points have been awarded, yada, yada, yada.  I don't care about points or credit.

#!perl
# Adapted from -
#  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_tasks__accounts_and_domains.asp

use strict;
use Win32::OLE 'in';
$Win32::OLE::Warn = 3;

my $strComputer = shift || '.';
my $objWMIService = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\" .
    $strComputer . "\\root\\cimv2");
my $colComputer = $objWMIService->ExecQuery("Select * from Win32_ComputerSystem");
 
foreach my $objComputer (in $colComputer) {
    print ($objComputer->{UserName} || 'no user logged on'),"\n";
}