Jump to content

Write to all exisiting users HKEY_CURRENT_USER?


Recommended Posts

I'm not a programmer or developer but have been using AutoIT for a while (and love it), mainly for file and screen manipulation.

I now have a need to modify a registry key (referencing a server name that needs to change) for all existing users and any new users who log into a machine. We do not have roaming profiles but as this will be delivered via the system account so current user is not necessarily accessible.

Do I have to enumerate (RegEnumKey) all the users and apply the change where I find it?

I need to do this for all our Legacy XPx32 and our newer Windows7x64 machines I suspect there will be differences I need to be aware of there too.

Any pointers, example code, would be appreciated.

Link to comment
Share on other sites

Hi.

Welcome to the forum!

For a different purpose (turn off windows sounds for all users) I wrote this script (partial posted here):

If IsAdmin() Then ; search and alter all user's "HKCU" settings.
$Log_h = "C:\temp\my-result.txt"
FileWriteLine($Log_h, @CRLF & @CRLF & "Admin rights. Let's do all user account's settings.")
$U_Prefix = "S-1-5-21-" ; all users start with this string.
$SkipSuffix = "_classes" ; didn't google what these are fore -> skip

$k = 0
While 1
$k += 1
$Next = RegEnumKey("HKU", $k)
If @error Then ExitLoop
If StringRight($Next, StringLen($SkipSuffix)) = $SkipSuffix Then ContinueLoop
If StringLeft($Next, StringLen($U_Prefix)) = $U_Prefix Then
FileWriteLine($Log_h, "---------------------------------------------------------------------------------")
$Start = "HKU\" & $Next & "\AppEvents\Schemes"
FileWriteLine($Log_h, $Start & @CRLF)
RegWrite($Start, "", $Type, ".None") ; Namen des aktuellen Sound Schemas
Loopen($Start)
EndIf
WEnd
If $silent Then
ToolTip("Windows Sound Schema wurde für alle lokalen Benutzer auf 'Keine Sounds' geändert.")
Else
MsgBox(64, "Windows Sound Schema - ADMIN", "Windows Sound Schema wurde für alle lokalen Benutzer auf 'Keine Sounds' geändert.", 5)
EndIf
Else
; not able to alter other user's settings.
EndIf

Func Loopen($_Start)
; here I changed the sound schema
EndFunc

if you want to be prompted for Admin credentials, look up the help file: isAdmin(), #RequireAdmin.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Great, thanks Rudi, I wasn't sure if I was missing a quick function or similar but it looks like the only way is to recurse each instance of a user in turn.

The second part or my problem is new users. Is there somewhere that I can write to that will be copied to new users HKCU when they first log onto the machine?

Link to comment
Share on other sites

  • Moderators

Hi, MrAndyH. Rather than trying to catch new users as they log in, why not try one of the following:

  • Write the script to check for the existence of the reg key and, if not found, write it to the HKCU hive.

  • If you're in a domain environment, set the script as part of either a login script or a GPO, so that it runs whenever someone logs in.
  • If you're not in a domain environment, or don't have the privileges to do the above, place the script in the AllUsers startup folder, or reference it in HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun.
Either way, the script will automatically run whenever someone logs in, and you won't have to worry about whether or not the value is missing for a new user.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Here is a silent Adobe Reader 10.x installer. It installs and then regwrites adobe EULA to all user profiles HKCU including uncreated ones. Supports WinXP -> Win8. Tested on hundreds of machines (literally). Self portable.

Sorry that autoitscript removes my tab characters.

#requireadmin
$reldir = "DocumentsAdobe Reader"
$exe = "AdbeRdr1001_en_US.exe"
$processname = $exe
$uninstallkey = "{AC76BA86-7AD7-1033-7B44-AA0000000001}"
$displayversion = "10.0.1"
$uninstallversion = RegRead("HKLMSOFTWAREMicrosoftWindowsCurrentVersionUninstall" & $uninstallkey, "DisplayVersion")
If $uninstallversion <> $displayversion then
Install()
EndIf

Func Install()
If Not ProcessExists($processname) Then
Run(@ScriptDir & "" & $reldir & "" & $exe & " /sAll")
EndIf
While ProcessExists($processname)
If WinExists("setup", "Setup needs to restart your system") Then
ControlClick("setup", "", "Button2")
Else
Sleep(500)
EndIf
WEnd

applyeula()

EndFunc

RunWait(@ProgramFilesDir & "Common FilesAdobe AIRVersions1.0Adobe AIR Application Installer.exe", "-uninstall com.adobe.mauby 4875E02D9FB21EE389F73B8D1702B320485DF8CE.1")
;RunWait(@ProgramFilesDir & "Common FilesAdobe AIRVersions1.0ResourcesAdobe AIR Updater.exe -arp:uninstall")
If FileExists(@DesktopCommonDir & "Adobe Reader X.lnk") Then FileDelete(@DesktopCommonDir & "Adobe Reader X.lnk")
FileSetAttrib(@ProgramFilesDir & "AdobeReader 10.0ReaderAdobeUpdater.dll", "-R")
FileDelete(@ProgramFilesDir & "AdobeReader 10.0ReaderAdobeUpdater.dll")
FileDelete(@ProgramFilesDir & "AdobeReader 10.0Readerplug_insUpdater.api")
DirRemove(@CommonFilesDir & "AdobeUpdater6" ,1)
DirRemove(@AppDataCommonDir & "AdobeUpdater6", 1)
DirRemove(@UserProfileDir & "Local SettingsApplication DataAdobeAcrobat10.0Updater", 1)
DirRemove(@UserProfileDir & "Local SettingsApplication DataAdobeUpdater6", 1)
DirRemove(@CommonFilesDir & "AdobeARM1.0", 1)
DirRemove(@AppDataCommonDir & "AdobeReader10.01ARM", 1)
DirRemove(@ProgramFilesDir & "Common FilesAdobeARM", 1)

RegDelete("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun","Adobe ARM")
RegDelete("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun","Adobe Reader Speed Launcher")
;RegWrite('HKLMSOFTWAREPoliciesAdobeAcrobat Reader10.0FeatureLockdown', 'bUpdater', 'Reg_Dword', '0x00000000')

Func applyeula()

$KEY_READ = 0x20019
$KEY_WRITE = 0x20006

Global Const $SE_RESTORE_NAME = "SeRestorePrivilege"
Global Const $SE_PRIVILEGE_ENABLED = 0x00000002
Global Const $SE_BACKUP_NAME = "SeBackupPrivilege"

Global Const $TOKEN_QUERY = 0x00000008
Global Const $TOKEN_ADJUST_PRIVILEGES = 0x00000020

; Detect OS version
If @OSVersion == "WIN_8" Or @OSVersion == "WIN_7" Or @OSVersion == "WIN_VISTA" Or @OSVersion = "WIN_2008" Then
$OS = 'NT6'
ElseIf @OSVersion == "WIN_XP" Then
$OS = 'NT5'
Else
MsgBox(48, "Error", "Wrong OS version")
Exit
EndIf

; UDFs from here - http://www.autoitscript.com/forum/topic/75250-registry-udfs/page__p__545798#entry545798
Local $aUserList[10][10], $UserList[10], $x = 0
If $OS = 'NT5' Then
$UserList = _FileListToArray(@HomeDrive & "Documents and Settings", "*", 2)
;_ArrayDisplay($UserList)
For $i = 1 To $UserList[0]
Select
Case $UserList[$i] = "All Users"
Case $UserList[$i] = "NetworkService"
Case $UserList[$i] = "LocalService"
Case Else
     $aUserList[$x][0] = @HomeDrive & "Documents and Settings"
     $aUserList[$x][1] = $UserList[$i]
     $x += 1
     If $x >= UBound($aUserList) Then
     ReDim $aUserList[UBound($aUserList) + 10]
     EndIf
EndSelect
Next
ElseIf $OS = 'NT6' Then
$UserList = _FileListToArray(@HomeDrive & "Users", "*", 2)
For $i = 1 To $UserList[0]
Select
Case $UserList[$i] = "All Users"
Case $UserList[$i] = "Default"
Case $UserList[$i] = "Public"
Case Else
     $aUserList[$x][0] = @HomeDrive & "Users"
     $aUserList[$x][1] = $UserList[$i]
     $x += 1
     If $x >= UBound($aUserList) Then
     ReDim $aUserList[UBound($aUserList) + 10]
     EndIf
EndSelect
Next
EndIf
;_ArrayDisplay($aUserList)
If Not $x = 0 Then
ReDim $aUserList[$x][2]
;_ArrayDisplay($aUserList)
Else
MsgBox(48, "Error", "No valid users found to apply reg changes to",30)
Return
EndIf

For $i = 0 To UBound($aUserList) - 1
$regpath = $aUserList[$i][0] & '' & $aUserList[$i][1] & 'NTUSER.DAT'
;~ ConsoleWrite($aUserList[$i][1] & ' = ' & @UserName & @CRLF)

If $aUserList[$i][1] = @UserName Then
RegWrite("HKCUSoftwareAdobeCommonFilesUsageDemographic", "CompanySize", "REG_SZ", "NOVALUE")
RegWrite("HKCUSoftwareAdobeCommonFilesUsageDemographic", "Industry", "REG_SZ", "NOVALUE")
RegWrite("HKCUSoftwareAdobeCommonFilesUsageDemographic", "JobFunction", "REG_SZ", "NOVALUE")
RegWrite("HKCUSoftwareAdobeCommonFilesUsageReader 10", "OptIn", "REG_DWORD", "0")
RegWrite('HKCUSoftwareAdobeAcrobat Reader10.0AdobeViewer', 'TrustedMode', 'Reg_Dword', '0x00000000')
RegWrite('HKCUSoftwareAdobeAcrobat Reader10.0AdobeViewer', 'EULA', 'Reg_Dword', '0x00000001')
RegWrite('HKCUSoftwareAdobeAcrobat Reader10.0AdobeViewer', 'Launched', 'Reg_Dword', '0x00000001')
RegWrite('HKCUSoftwareAdobeAcrobat Reader10.0AdobeViewer', 'EULA', 'REG_DWORD',1) ;Accept eula
Else
;~ ConsoleWrite($regpath & @CRLF)
_RegLoadHive($regpath, "HKUZZZ")
RegWrite("HKUZZZSoftwareAdobeCommonFilesUsageDemographic", "CompanySize", "REG_SZ", "NOVALUE")
RegWrite("HKUZZZSoftwareAdobeCommonFilesUsageDemographic", "Industry", "REG_SZ", "NOVALUE")
RegWrite("HKUZZZSoftwareAdobeCommonFilesUsageDemographic", "JobFunction", "REG_SZ", "NOVALUE")
RegWrite("HKUZZZSoftwareAdobeCommonFilesUsageReader 10", "OptIn", "REG_DWORD", "0")
RegWrite('HKUZZZSoftwareAdobeAcrobat Reader10.0AdobeViewer', 'TrustedMode', 'Reg_Dword', '0x00000000')
RegWrite('HKUZZZSoftwareAdobeAcrobat Reader10.0AdobeViewer', 'EULA', 'Reg_Dword', '0x00000001')
RegWrite('HKUZZZSoftwareAdobeAcrobat Reader10.0AdobeViewer', 'Launched', 'Reg_Dword', '0x00000001')
RegWrite('HKUZZZSoftwareAdobeAcrobat Reader10.0AdobeViewer', 'EULA', 'REG_DWORD',1) ;Accept eula
_RegUnloadHive("HKUZZZ")
EndIf
Next
EndFunc
Func _FileListToArray($sPath, $sFilter = "*", $iFlag = 0)
Local $hSearch, $sFile, $sFileList, $sDelim = "|"
$sPath = StringRegExpReplace($sPath, "[/]+z", "") & "" ; ensure single trailing backslash
If Not FileExists($sPath) Then Return SetError(1, 1, "")
If StringRegExp($sFilter, "[/:><|]|(?s)As*z") Then Return SetError(2, 2, "")
If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
$hSearch = FileFindFirstFile($sPath & $sFilter)
If @error Then Return SetError(4, 4, "")
While 1
$sFile = FileFindNextFile($hSearch)
If @error Then ExitLoop
If ($iFlag + @extended = 2) Then ContinueLoop
$sFileList &= $sDelim & $sFile
WEnd
FileClose($hSearch)
If Not $sFileList Then Return SetError(4, 4, "")
Return StringSplit(StringTrimLeft($sFileList, 1), "|")
EndFunc;==>_FileListToArray
Func _SetPrivilege($avPrivilege)
Local $iDim = UBound($avPrivilege, 0), $avPrevState[1][2]
If Not ( $iDim <= 2 And UBound($avPrivilege, $iDim) = 2 ) Then Return SetError(1300, 0, $avPrevState)
If $iDim = 1 Then
Local $avTemp[1][2]
$avTemp[0][0] = $avPrivilege[0]
$avTemp[0][1] = $avPrivilege[1]
$avPrivilege = $avTemp
$avTemp = 0
EndIf
Local $k, $tagTP = "dword", $iTokens = UBound($avPrivilege, 1)
Do
$k += 1
$tagTP &= ";dword;long;dword"
Until $k = $iTokens
Local $tCurrState, $tPrevState, $pPrevState, $tLUID, $hAdvapi32, $hKernel32, $ahGCP, $avOPT, $aiGLE
$tCurrState = DLLStructCreate($tagTP)
$tPrevState = DllStructCreate($tagTP)
$pPrevState = DllStructGetPtr($tPrevState)
$tLUID = DllStructCreate("dword;long")
DLLStructSetData($tCurrState, 1, $iTokens)
$hAdvapi32 = DllOpen("Advapi32.dll")
For $i = 0 To $iTokens - 1
DllCall( $hAdvapi32, "int", "LookupPrivilegeValue", _
"str", "", _
"str", $avPrivilege[$i][0], _
"ptr", DllStructGetPtr($tLUID) )
DLLStructSetData( $tCurrState, 3 * $i + 2, DllStructGetData($tLUID, 1) )
DLLStructSetData( $tCurrState, 3 * $i + 3, DllStructGetData($tLUID, 2) )
DLLStructSetData( $tCurrState, 3 * $i + 4, $avPrivilege[$i][1] )
Next
$hKernel32 = DllOpen("Kernel32.dll")
$ahGCP = DllCall($hKernel32, "hwnd", "GetCurrentProcess")
$avOPT = DllCall( $hAdvapi32, "int", "OpenProcessToken", _
"hwnd", $ahGCP[0], _
"dword", BitOR($TOKEN_ADJUST_PRIVILEGES, $TOKEN_QUERY), _
"hwnd*", 0 )
DllCall( $hAdvapi32, "int", "AdjustTokenPrivileges", _
"hwnd", $avOPT[3], _
"int", False, _
"ptr", DllStructGetPtr($tCurrState), _
"dword", DllStructGetSize($tCurrState), _
"ptr", $pPrevState, _
"dword*", 0 )
$aiGLE = DllCall($hKernel32, "dword", "GetLastError")
DllCall($hKernel32, "int", "CloseHandle", "hwnd", $avOPT[3])
DllClose($hKernel32)
Local $iCount = DllStructGetData($tPrevState, 1)
If $iCount > 0 Then
Local $pLUID, $avLPN, $tName, $avPrevState[$iCount][2]
For $i = 0 To $iCount - 1
$pLUID = $pPrevState + 12 * $i + 4
$avLPN = DllCall( $hAdvapi32, "int", "LookupPrivilegeName", _
"str", "", _
"ptr", $pLUID, _
"ptr", 0, _
"dword*", 0 )
$tName = DllStructCreate("char[" & $avLPN[4] & "]")
DllCall( $hAdvapi32, "int", "LookupPrivilegeName", _
"str", "", _
"ptr", $pLUID, _
"ptr", DllStructGetPtr($tName), _
"dword*", DllStructGetSize($tName) )
$avPrevState[$i][0] = DllStructGetData($tName, 1)
$avPrevState[$i][1] = DllStructGetData($tPrevState, 3 * $i + 4)
Next
EndIf
DllClose($hAdvapi32)
Return SetError($aiGLE[0], 0, $avPrevState)
EndFunc ;==> _SetPrivilege
Func _RegLoadHive($sFile, $sKey)
Local $avArray = Split_sRootKey($sKey)
Local $hKey = RegConnectRegistry($avArray[0], $avArray[1])
Local $avCurr[2][2] = [[$SE_RESTORE_NAME, $SE_PRIVILEGE_ENABLED], [$SE_BACKUP_NAME, $SE_PRIVILEGE_ENABLED]]
Local $avPrev = _SetPrivilege($avCurr)
Local $avRLH = DllCall("Advapi32.dll", "long", "RegLoadKey", "hwnd", $hKey, "str", $avArray[2], "str", $sFile)
_SetPrivilege($avPrev)
RegCloseKey($hKey)
Return SetError( $avRLH[0], 0, Number($avRLH[0] = 0) )
EndFunc ;==> _RegLoadHive
Func _RegUnloadHive($sKey)
Local $avArray = Split_sRootKey($sKey)
Local $hKey = RegConnectRegistry($avArray[0], $avArray[1])
Local $avCurr[2][2] = [[$SE_RESTORE_NAME, $SE_PRIVILEGE_ENABLED], [$SE_BACKUP_NAME, $SE_PRIVILEGE_ENABLED]]
Local $avPrev = _SetPrivilege($avCurr)
Local $avRUH = DllCall("Advapi32.dll", "long", "RegUnLoadKey", "hwnd", $hKey, "str", $avArray[2])
_SetPrivilege($avPrev)
RegCloseKey($hKey)
Return SetError( $avRUH[0], 0, Number($avRUH[0] = 0) )
EndFunc ;==> _RegUnloadHive
Func RegCloseKey($hKey)
Local $avRCK = DllCall("Advapi32.dll", "long", "RegCloseKey", "hwnd", $hKey)
Return SetError( $avRCK[0], 0, Number($avRCK[0] = 0) )
EndFunc ;==> RegCloseKey
Func RegConnectRegistry($sComputer, $hKey)
Local $avRCR = DllCall( "Advapi32.dll", "long", "RegConnectRegistry", _
"str", $sComputer, _
"hwnd", $hKey, _
"hwnd*", 0 )
Return SetError($avRCR[0], 0, $avRCR[3])
EndFunc ;==> RegConnectRegistry
Func Split_sRootKey($sRootKey)
Local Const $HKEY_CLASSES_ROOT = 0x80000000
Local Const $HKEY_CURRENT_USER = 0x80000001
Local Const $HKEY_LOCAL_MACHINE = 0x80000002
Local Const $HKEY_USERS = 0x80000003
Local Const $HKEY_CURRENT_CONFIG = 0x80000005
Local $avHKEY[5][3] = [["HKCR", "HKEY_CLASSES_ROOT", $HKEY_CLASSES_ROOT], _
["HKCU", "HKEY_CURRENT_USER", $HKEY_CURRENT_USER], _
["HKLM", "HKEY_LOCAL_MACHINE", $HKEY_LOCAL_MACHINE], _
["HKU", "HKEY_USERS", $HKEY_USERS], _
["HKCC", "HKEY_CURRENT_CONFIG", $HKEY_CURRENT_CONFIG]]
Local $avArray[3]
If StringInStr($sRootKey, "") = 1 Then
Local $asComputer = StringRegExp($sRootKey, "[^]*", 1)
If Not @error Then
$avArray[0] = StringTrimRight($asComputer[0], 1)
$sRootKey = StringReplace($sRootKey, $asComputer[0], "", 1)
EndIf
EndIf
If StringInStr($sRootKey, "") = 1 Or StringInStr($sRootKey, "", 0, -1) = StringLen($sRootKey) Or StringInStr($sRootKey, "") Then
$avArray[0] = ""
Return $avArray
Else
Local $asSplit = StringSplit($sRootKey, "")
For $i = 0 To UBound($avHKEY) - 1
If $asSplit[1] = $avHKEY[$i][0] Or $asSplit[1] = $avHKEY[$i][1] Then
$avArray[1] = $avHKEY[$i][2]
ExitLoop
EndIf
Next
If $avArray[1] = "" Then
$avArray[0] = ""
Return $avArray
EndIf
For $i = 2 To $asSplit[0] - 1
$avArray[2] &= $asSplit[$i] & ""
Next
If $asSplit[0] > 1 Then $avArray[2] &= $asSplit[ $asSplit[0] ]
EndIf
Return $avArray
EndFunc ;==> Split_sRootKey
Edited by NDog
Link to comment
Share on other sites

  • 1 month later...

Hi.

The default settings are below HKU.DEFAULT, but it's a must to check, what happens for the products, in case you "pre-fill" just some certain values (like "AcceptEULA"="1" e.g.), but not all the values, that are usually created by the normal "first user logon + first program start".

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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...