Current logged in user using VBScript

Hi All,

Am using this below code snippet to get current logged in user, it works fine over here...

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_ComputerSystem",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_ComputerSystem instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "UserName: " & objItem.UserName
Next

I tested in another system having 3 users , but its not fetching the username...its returning NULL value.

What would be the problem...?

Thanks

Divakar

June 5th, 2012 1:09pm

If the script is running on the computer, WMI will be much slower than using the wshNetwork object. For example:

Set objNetwork = CreateObject("Wscript.Network")
Wscript.Echo "The current user is " & objNetwork.UserName

-----

In a similar manner, you can retrieve the name of the local computer, from objNetwork.ComputerName. And, if you want the distinguished name of the current user (so you can query AD for other information), you can use the ADSystemInfo object. For example:

Set objSysInfo = Createobject("ADSystemInfo")
Wscript.Echo "DN of current user: " & objSysInfo.UserName

-----


Free Windows Admin Tool Kit Click here and download it now
June 5th, 2012 3:30pm

Thanks Richard, It works :-)
June 5th, 2012 4:05pm

Thanks man !
Free Windows Admin Tool Kit Click here and download it now
May 19th, 2013 9:03am

Is it possible the other system was a 64-bit machine? I think your original method is only good for 32-bit machines (this function stopped working for me when I migrated to a 64-bit machine). Richard's solution is probably a better method for running on local machines anyway.
October 29th, 2013 11:39pm

Is it possible the other system was a 64-bit machine? I think your original method is only good for 32-bit machines (this function stopped working for me when I migrated to a 64-bit machine). Richard's solution is probably a better method for running on local machines anyway.

No one answered the question as asked.  The code works exactly the same on any machine.  Read the whole question very carefully.

The OPs question:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I tested in another system having 3 users , but its not fetching the username...its returning NULL value.

What would be the problem...?

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

This statement/question makes the cause extremely obvious.  Only a real Windows geek would get this.  Are you a geek or a gawker?

Look very closely.  The answer is in the question.

Free Windows Admin Tool Kit Click here and download it now
October 30th, 2013 12:45am

Note that this is a question on an Admin WMI test that is given to applicants for Admin jobs. It is usually given as an open book question so feel free to look up the answer.

October 30th, 2013 12:47am

Note that this is a question on an Admin WMI test that is given to applicants for Admin jobs. It is usually given as an open book question so feel free to look up the a

Free Windows Admin Tool Kit Click here and download it now
November 9th, 2013 12:17am

Note that this is a question on an Admin WMI test that is given to applicants for Admin jobs. It is usually given as an open book question so feel free to look up the a

November 9th, 2013 1:20am

I've never seen a difference in performance between the wshShell and wshNetwork objects. Both will be faster and more reliable than WMI. I like the ADSystemInfo object because it is fast. In particular, if you are not sure the user will be authenticated to a domain, you can use error trapping and the UserName property will raise an error immediately (if the user is authenticated locally). Other methods result in a long timeout.

Free Windows Admin Tool Kit Click here and download it now
November 9th, 2013 1:24am

Good advice Richard.

In general it is a good idea to avoid WMI in logon scripts.

November 9th, 2013 1:34am

I understand that this is an old topic, but:

there IS a difference between wshShell and wshNetwork, not in performance, but in operation.

If you use wshNetwork to get username, and the script is invoked by system, the user name would be "system" (or localized string for "system").

If wshShell is used, then its ExpandEnvironmentStrings( "%USERNAME%" ) in this scenario will give "COMPUTERNAME$".

Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 1:22am

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

Other recent topics Other recent topics