[Last Call] Learn how to a build a cloud-first strategyRegister Now


How to query the current logged in user using VBScript when running VBS under System account

Posted on 2014-08-26
Medium Priority
Last Modified: 2014-09-02
We use SCCM to push out applications and updates to users. Regular staff are denied access to install any files as well as edit the registry. In SCCM, we have installs run under the System account which grants the accesses it needs to install programs. In my current case, I have a Batch file that does 2 things.. 1. install the application. 2. run a VBS that pulls the UserName and then creates a REG file that writes a bunch of lines as well as takes the username (objNetwork.UserName) and inputs it into one of the lines:
a.WriteLine("""HostName""=""IPC-" & objNetwork.UserName & """")

Open in new window

Then imports it into the Registry.

Here is the problem...because this all gets done under the System account in SCCM, the VBS thinks that the username is "System" so inputs it as system when it really should have been the logged in users name. I need to find a way to get the VBS to pull the actual user logged in. I was thinking that it can query the last logged in user which would obviously be the current user but I do not know how to code this. Any one have any ideas as to how I can make this work?

The actual code I am using is referenced in this previous post so I would be adding it to the code that I posted near the bottom:
Question by:prologic08
  • 4

Accepted Solution

prologic08 earned 0 total points
ID: 40286609
I'm going to try this:

Option Explicit
Dim WSHShell, RegKey, Username
Set WSHShell = CreateObject("WScript.Shell")
RegKey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\"
Username = WSHShell.RegRead(RegKey & "LastLoggedOnSAMUser")
Username = Mid(Username,7) 'I have my domain inside of this value so I used this to remove the first 6 characters
WScript.echo Username

Open in new window

LVL 46

Expert Comment

ID: 40287649
What does the environment variable hold?  If it is the actual user and not system, you should be able to use something like this:
Set oWshShell = wscript.CreateObject( “wscript.shell” )
strUserName = oWshShell.ExpandEnvironmentStrings( “%USERNAME%” )

Open in new window

LVL 65

Assisted Solution

RobSampson earned 1000 total points
ID: 40289348
You can also see what this WMI code gives you.



strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
	strUserName = objComputer.UserName
WScript.Echo strUserName

Open in new window

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 40291337
On my machine the variable shows as SYSTEM.. My last comment above seemed to do the trick anyway so I would say I am good. Thanks for responding.

Author Comment

ID: 40291350
RobSampson, That gives me the same output as the code I posted so I can use either one.

Author Closing Comment

ID: 40297977
Since I was able to find my own answer before Rob posted his, I am splitting it between the both of us since either will work.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Simple Linear Regression

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question