Link to home
Start Free TrialLog in
Avatar of Ken Thompson
Ken Thompson

asked on

Vbscript to find currently logged on user and then add a registry entry to that user's HKEY_CURRENT_USER

Problem:

We are trying to deploy a product with SCCM that requires a registry entry in HKEY_CURRENT_USER for the logged on user.   Yes - it is ridiculous that we can't install this product for "system" but oh well.  In SCCM I can set to ensure that the application only installs when the user is logged so there is that good news but...

From within a script, I need to find out who the currently logged on user is and then add a registry entry to that user's HKEY_CURRENT _USER.

Solution:

I'm half way there with a vbscript.  I was able to find the following script in another post which finds the currently logged on user.  The problem I'm having now is I'm not sure how to run "regedit /s "updateregentry.reg"" and put the entry in that user's hive.

Any help would be appreciated.  Below is the vbscript.

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)
WScript.echo Username

Also - these are the registry entries I'm trying to add:

[HKEY_CURRENT_USER\Software\{08439167-4CA5-48E9-A810-A3A7C0B80B06}]

[HKEY_CURRENT_USER\Software\{08439167-4CA5-48E9-A810-A3A7C0B80B06}\Local]

[HKEY_CURRENT_USER\Software\{08439167-4CA5-48E9-A810-A3A7C0B80B06}\Local\CNS5T2Q540AHCQ6QU75GXH6DS1MCCCXFYF60W-102-292-280-A1]
"SiteMessage"="MyBusiness"
"ProductName"="TheProduct"
"ProductVersion"="12.x"
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Why not add those registry entries using group policy preferences on the use policy either for all users or users of certain group - if you have one for assigning users to for the app use the same one.
Avatar of Ken Thompson
Ken Thompson

ASKER

I don't have access to Active Directory in my current role.  What I need to install needs to be done within SCCM unfortunately.
So I have   "Username".
All I need now is to figure out how to run the lines below for Username but from the current working directory rather than logonserver.

Set objShell = CreateObject("WScript.Shell")
objShell.run ("regedit /s %logonserver%\netlogon\RegistryEntry.reg")

Haven't done anything like this yet....
Hi,

Answering in reverse.... you can copy the whole lot to a folder on C: first and run from there instead to avoid running from netlogon (which is soooo 1990s BTW).
Plan B: use a task-sequence instead for the whole thing. That way it's running whilst the user is logged on, but will install as system and you can run scripts and reboot as many times as you like.
Plan C: use PowerShell instead of vbscript
          To get the username it's just  this:
$env:username
Adding the registry keys is
New-item HKCU:\Software -value {08439167-4CA5-48E9-A810-A3A7C0B80B06}\local  etc.
New-itemproperty -path HKCU:\Software -value {08439167-4CA5-48E9-A810-A3A7C0B80B06}\local -name 'sitename' -value 'mybusiness'
etc

You could do both or neither of the last two things :).
Is this going to run AS the user on the machine, if not you are going to have to user the HKEY_USERS key of the registry to get the logged on user's registry - you are going to have to mess around then with user SID's etc.  If it runs as the user an registry tool aren't disabled you can just use regedit to import from wherever you drop the file, or directly write to registry within the VBScript, i.e.

For your registry entry something like this:

Dim objRegistry
Set objRegistry = CreateObject("Wscript.shell")

call objRegistry.RegWrite("HKCU\Software\{08439167-4CA5-48E9-A810-A3A7C0B80B06}\Local\CNS5T2Q540AHCQ6QU75GXH6DS1MCCCXFYF60W-102-292-280-A1\SiteMessage","MyBusiness","REG_SZ")
call objRegistry.RegWrite("HKCU\Software\{08439167-4CA5-48E9-A810-A3A7C0B80B06}\Local\CNS5T2Q540AHCQ6QU75GXH6DS1MCCCXFYF60W-102-292-280-A1\ProductName","TheProduct","REG_SZ")
call objRegistry.RegWrite("HKCU\Software\{08439167-4CA5-48E9-A810-A3A7C0B80B06}\Local\CNS5T2Q540AHCQ6QU75GXH6DS1MCCCXFYF60W-102-292-280-A1\ProductVersion","12.x","REG_SZ")


Steve
Or better still.... speak top your network admins and get them to spend 30 seconds pushing the setting down to all or relevant users with a policy.

Steve
If you need to update a different user's registry to the user running the script you can do something like this:

https://stackoverflow.com/questions/10908727/how-can-i-programatically-find-a-users-hkey-users-registry-key-using-powershell

Steve
Don't use HKEY_CURRENT_USER use HKEY_USERS.

All active user hive are loaded in HKEY_USERS
Hello all - a couple of quick notes.

1.  Thanks for help with finding the logged on user however, in my comment I noted that I already have that part of the script.  I have a snippet of code that pulls the logged on user and holds it in a variable called Username.  Now that I have that variable, what I need is the part of the script that updates HK_LOCAL_MACHINE for THAT user.  That is this part:

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)
 WScript.echo Username

2.  I have the part of the script that makes a registry update:

Set objShell = CreateObject("WScript.Shell")
 objShell.run ("regedit /s %logonserver%\netlogon\RegistryEntry.reg")

3.  What I'm having trouble with is putting these two parts together.  I need to execute the objShell.run for "Username".  I'm looking in my older scripts for clues but any help is appreciated.

Also - I don't have access to AD, this needs to run from SCCM and when you install "for" User in SCCM it doesn't seem to actually run as the user - just seems to install only for that user.
Please get access to AD then :-)

But aside from that I don't use SCCM but have you considered this method?

https://www.itninja.com/blog/view/appdeploy-articles-activesetup

It would appear that is the best of both worlds -- you can install the app as system but then when it runs it checks the current users HKCU Active Setup installation key and if it has not yet run as that user too then it runs a repair on the app which then can populate HKCU or whatever else is in the setup for THAT user.

Sorry I can't help too much with that option as we don't use SCCM on any of the sites I support at the moment.

Steve
Looks like there may be a better way to do this but I'm still not sure how.  Rather than a registry entry I can do a folder copy to the user's AppData\Local folder.  The installation I've been working with for silent install are for an older version. Now the folder containing the license can be copied.

So now I need to gather the logged on user and store that in the Username variable (see code above) then, using the contents of the Username variable, copy the folder containing license information to that user's AppData folder - C:\Users\NAME\AppData\Local\.

So I have the part that finds the logged in user - now I'm having trouble using the contents of the Username variable to copy to the correct path.
OK well you can get that NORMALLY assuming the user hasn't been renamed or a previous local user with the same name on that pc etc --. in which case the directory name won't match the user name, either firstname.oldname say, or username.domain. if it is a "normal" profile then you can get the directory with:

%userprofile%\..   gets you back to the users directory
%userprofile%\..\%theusername%\appdata\local    gets you to the user dir.

It is likely however that your directory structure is going to be c:\users\username anyway especially if all machines have common build.

so potentially just access it as c:\users\username\appdata\local

Maybe think of others ways, e.g. create a batch file which does what you want and copies the files or registry entries and then use your sccm package to add this to Startup group in Start menu etc?

Another way would be to make a shortcut or batch file for the user to click the first time they use it on a machine which, say, prepares these files or registry entries it needs and then adds the 'real' shortcut and removes the temporary ones say?

@echo off
REM copy all files from under allusersc:\sccmstuff\somedir to same dir under local appdata in a user profile
xcopy /y /d c:\sccmstuff\somedir\*.* "%appdata%\..\Local\somedir"
xcopy /y /d c:\sccmstuff\somedir\shortcut.lnk "%userprofile%\desktop"
del "%userprofile%\desktop\runmyapp.lnk"

Can you copy the files to all user profiles on the machine when it is installed.... also what happens when a new user logs on to that machine - you'll be needing to install those files or registry entries again if you push them as part of the original install.

That Active Setup option does seem a logical way to go?

Steve
Like I said, you will not be able to do it the way you are trying to do it. HKCU is only visible to the actual user, not in another user context.

You only have three choices
  • Run it as the user
  • Load the user hive (cannot be in use)
  • Use HKU
All:

We can all toss the registry entry part of this out the window.  I don't have to do that.  I can simply copy a license file to the user's AppData\Local folder.

Steve:

I'm restricted to performing this task from within SCCM.  I was previously an AD Administrator so you can bet that I'm aware of all the ways I can do this with logon scripts etc. and a bit irritated I'm not able to - but I have to move on.  

So all I have to do now is a simple folder copy.  But as I noted I need to copy to the folder of the logged in user.  The product I'm pushing is ridiculous in that it is still licensed per user and has to be installed per use - can't copy the license file to every user.

I know how to get the current user using vbscript.  I'm not that great at it so I'm having trouble taking the Username variable and using the contents to copy to that user's path.  Do you know a good way to do that with vbs?  

The following code gives me the currently logged on user and loads the Username variable with the user's logon ID:  

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)
  WScript.echo Username


So now I'm just trying to figure out how to plug the contents of the Username variable into the copy command.




I've already successfully copied the file using robocopy and the %username% variable from a batch file.

I would be good for doing the same thing with a batch file but I don't know how to get the currently logged on user with a batch file so I'm sticking to VB.
I'm getting there.  Anyone know how to attach the Username variable to the end of a string?  In the code above I obtained the logged on user ID and stored it in the Username variable.  Now I need to append that username to the target path where I need to copy the folder to.  Still having trouble with this.  Trying to figure out how to attach the Username variable contents to this to create a new variable...say Userpath....

strFolder = "C:\Users"

- I've set the SCCM deploy to only push the app when the user is logged in - this ensures the app get's pushed to the right person.
- I have the portion of the script above that gathers the logged on user's logon ID.
- I know how to use  FSO.CopyFolder to copy from a network share - it would look something like this...
   FSO.CopyFolder "\\Servername\folder", Userpath & "\AppData\Local\Folder",1
- All I'm having trouble with now is adding the Username variable to the end of C:\Users.

Did I post this in the correct spot...VBScript Experts?
Any VBScript experts out there?  I'll give this a little more and then repost this with the question written differently so I can possibly get help with a VBScript.

Here is what I have so far....

Dim FSO, 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)

Set FSO = CreateObject("Scripting.FileSystemObject")
Dim UserPath : UserPath =  ????  <----- How do I make this happen - tack the contents of Username to the end of C:\Users and make UserPath = C:\Users\Username?
Dim DataFolder : DataFolder = UserPath & "\AppData\Local\Folder"
'msgbox DataFolder
if FSO.FolderExists(strFolder) = False then  
FSO.CopyFolder "\\Server\SCCMResources\Applications\Folder", UserPath & "\AppData\Local\Folder",1
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland 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
You can get the actual path from ProfileLists in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion
That was it Mr. Knight - worked like a charm!  For a week everyone kept saying "you can't do that".  Right on.
Glad it helped.  I still don't think it is necessarily the RIGHT way of doing it but if it works in your environment and are aware that it may need adjustments if your systems change then you know that anyway.

Good luck with it,

Steve
Hey Steve Knight - I created a duplicate question on this subject and you answered it there.  Look up:

Half finished VBScript to obtain currently logged on user and copy a FOLDER to AppData\Local in their profile?

Can you get in that question and note that you answered it with a link to this one?  I can't figure out how to give you credit and I'm not sure how to get a moderator to do it.  If you go to it and put the link to this I can just make that the answer.

Thanks.