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

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:
Who is Participating?
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.

prologic08Author Commented:
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


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
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( “” )
strUserName = oWshShell.ExpandEnvironmentStrings( “%USERNAME%” )

Open in new window

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

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

prologic08Author Commented:
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.
prologic08Author Commented:
RobSampson, That gives me the same output as the code I posted so I can use either one.
prologic08Author Commented:
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.
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
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.