Jump to content

Distributing registry key for all users on a machine.


Recommended Posts

I'm using the following code which I found in forums:

_RegWriteAllUsers("Software\WinImage", "NameRegistered", "REG_SZ", "Charles Keisler")

Func _RegWriteAllUsers($keyname, $valuename = "", $type = "", $value = "")
;Mount default profile for witing as it is not mountes to HKU by default
If FileGetVersion(@WindowsDir & "\system32\ntoskrnl.exe") >= 6 Then
RunWait('reg load "HKEY_USERS\DEFAULT" "' & @UserProfileDir & '\..\Default\NTUSER.DAT"', "", @SW_HIDE)
Else
RunWait('reg load "HKEY_USERS\DEFAULT" "' & @UserProfileDir & '\..\Default User\NTUSER.DAT"', "", @SW_HIDE)
EndIf
;Write to all offline profiles
For $i = 1 To 999999999
Local $var = RegEnumKey("HKEY_USERS", $i)
If @error <> 0 Then ExitLoop
If StringInStr($var, "_Classes") Then ContinueLoop
If StringLen($var) = 8 Then ContinueLoop ;fileter out system SIDs
msgbox(0,0,$var)
RegWrite("HKEY_USERS\" & $var & "\" & $keyname, $valuename, $type, $value)
Next
;UnMount default profile
RunWait('reg unload "HKEY_USERS\DEFAULT"', "", @SW_HIDE)
;write to current profile
RegWrite("HKEY_CURRENT_USER\" & $keyname, $valuename, $type, $value)
EndFunc

As you can see, I'm trying to distribute the same HKCU value across multiple users.

What is working: registry entry appears in my HKCU branch. Appears for all newly created user profiles.

What isn't working: registry entry does not get imported into existing OFFLINE profiles. Only if the users are logged in.

I checked the middle of the code which references offline profiles. I fail to see the issue. Code seems simple enough, but I have no idea what's wrong with it. And I'm using Win7 x64.

EDIT: OMG, I'm an idiot. The very first line of the code:

_RegWriteAllUsers("Software\WinImage", "NameRegistered", "REG_SZ", "Charles Keisler")

I just checked, and THIS is what it writes into offline profiles. WTH, why. It doesn't apply anywhere else, why here? I'll double check my assumption.

EDIT 2: Ok, I corrected that and now script seems to write my value into HKEY_USERS properly. However that still doesn't help to solve the problem. When profiles are offline, it doesn't get written.

Edited by supraspecies
Link to comment
Share on other sites

Why not just place a script in HKLM\Software\Microsoft\WIndows\CurrentVersion\Run everytime the user logs on it they get the new settings.  If you only want to apply once then you can just use HKLM\Software\Microsoft\Windows\CurrentVersion\ActiveSetup (this is similar to how Microsoft Internet Explorer settings).

Link to comment
Share on other sites

Yes, I suppose my way lies to ActiveSetup now. I heard about it before. What could be the issue though? I corrected it, and now the script DID write my value in all HKEY_USERS entries. So... what else?

 

EDIT: Weird... The script wrote my value in every single HKEY_USERS branch except the account I'm using. I'll keep investigating. But generally - yes, it works.

EDIT 2: WTF. Script did everything right. But this branch just doesn't exist in my Admin registry. It's not there. Then how can it freaking exist?

EDIT 3: Script hangs. Always. Trying to find out where, but if anyone can figure it out, I'd appreciate that.

EDIT 4: Solved all my problems. Script was hanging due to a huge number in the line:

For $i = 1 To 999999999

I set it to 9999 (which is plentiful), and now the script doesn't hang and works fast.

P.S. Once again I start a topic, and solve it myself. xD Guess I'm a fast learner.

Edited by supraspecies
Link to comment
Share on other sites

Sorry wasn't alerted about your Edits, I would have done it like below since the Guids found under HKEY_USERS are generally a result of profiles not closing correctly, normally you would should only see your own account.  The below loads each users hive if it's not available.

Anyway I hope it helps.

_RegWriteAllUsers("Software\WinImage", "NameRegistered", "REG_SZ", "Charles Keisler")

Func _RegWriteAllUsers($_sKeyName, $_sValName = "", $_sRegType = "", $_sRegData = "")
    Local $i = 1, $sHKUserGuid
    ;Mount default profile for witing as it is not mountes to HKU by default
    If FileGetVersion(@WindowsDir & "\system32\ntoskrnl.exe") >= 6 Then
        RunWait(@ComSpec & ' /c REG LOAD "HKEY_USERS\DEFAULT" "' & @UserProfileDir & '\..\Default\NTUSER.DAT"', "", @SW_HIDE)
    Else
        RunWait(@ComSpec & ' /C REG LOAD "HKEY_USERS\DEFAULT" "' & @UserProfileDir & '\..\Default User\NTUSER.DAT"', "", @SW_HIDE)
    EndIf
    RegWrite("HKEY_USERS\DEFAULT\" & $_sKeyName, $_sValName, $_sRegType, $_sRegData)
    RunWait(@ComSpec & ' /c REG UNLOAD "HKEY_USERS\DEFAULT"', "", @SW_HIDE)
    ;~ Write to all offline profiles
    While 1
        $sHKUserGuid = RegEnumKey("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", $i)
            If @error Then ExitLoop
        If StringLen($sHKUserGuid) = 8 Then
            $i += 1
            ContinueLoop
        EndIf
        RegWrite("HKEY_USERS\" & $sHKUserGuid & "\" & $_sKeyName, $_sValName, $_sRegType, $_sRegData)
        If @error Then
            $sHKUserGuidPath = RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & $sHKUserGuid, "ProfileImagePath")
            If FileExists($sHKUserGuidPath & "\NTUser.dat") = 0 Then ConsoleWrite($sHKUserGuidPath & "\NTUser.dat does not exist." & @CRLF)
            ConsoleWrite(@ComSpec & ' /c REG LOAD "HKEY_USERS\' & StringTrimLeft($sHKUserGuidPath, StringInStr($sHKUserGuidPath, "\", 0, -1)) & ' ' & $sHKUserGuidPath & '\NTUSER.DAT"' & @CRLF)
            RunWait(@ComSpec & ' /c REG LOAD "HKEY_USERS\' & StringTrimLeft($sHKUserGuidPath, StringInStr($sHKUserGuidPath, "\", 0, -1)) & ' ' & $sHKUserGuidPath & '\NTUSER.DAT"', "", @SW_HIDE)
        EndIf
        $i += 1
    WEnd
EndFunc

 

Link to comment
Share on other sites

Have a look at Engine's Registry UDFs.  The HKCUReg.au3 library work very well for writing to user's registry hive.  It also allow you to write to users' registry on remote computers.  Example below.  

#include <HKCUReg.au3>

Global $sUserName = "username"
Global $sComputerName = "computername"

;Write the key for all user profiles on local computer.
_HKCU_Write("Software\WinImage", "NameRegistered", "REG_SZ", "Charles Keisler")

;Write the key for a specified user profile on local computer.
_HKCU_Write("\\" & $sUserName & "Software\WinImage", "NameRegistered", "REG_SZ", "Charles Keisler")

;Write the key on a remove computer for all users profiles.
_HKCU_Write("\\\" & $sComputerName & "Software\WinImage", "NameRegistered", "REG_SZ", "Charles Keisler")

;Write the key on a remote computer for a specified user profile.
_HKCU_Write("\\\" & $sComputerName & "\\" & $sUserName  & "Software\WinImage", "NameRegistered", "REG_SZ", "Charles Keisler")

Adam

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...