[英]Open another user registry settings
我想寫一個應用程序,寫一個本地機器上的所有用戶一個指定的鍵(例如:我想為所有用戶的IE收藏夾設置相同的文件夾)
PS有人用過這些功能嗎? LoadUserProfile RegOpenCurrentUser CreateProcessAsUser
我做過很多次了。 我們的想法是更新當前登錄用戶的HKCU(這很容易)。 然后你必須枚舉系統上的每個配置文件並找到他們的ntuser.dat文件(這也很容易)。
找到ntuser.dat文件后,將其加載到HKLM配置單元中的臨時密鑰(我通常使用'HKLM \\ TempHive'。然后編輯。
如果有多個用戶登錄,則他們的個人資料將通過他們的SID加載到HKEY_USERS下。 只需更新該位置即可。
要修改任何新用戶的設置,只需修改HKEY_USERS.DEFAULT下的相應密鑰,或者使用下面的Delphi代碼,通過加載默認用戶的HKCU注冊表配置單元(存儲在ntuser.dat中)來執行此操作。
更新 :我發現我的Delphi代碼演示了如何更新未登錄系統的用戶的HKCU配置單元。
這需要Russell Libby的'Privilege'組件, 可在此處獲得 。
//NOTE: sPathToUserHive is the full path to the users "ntuser.dat" file.
//
procedure LoadUserHive(sPathToUserHive: string);
var
MyReg: TRegistry;
UserPriv: TUserPrivileges;
begin
UserPriv := TUserPrivileges.Create;
try
with UserPriv do
begin
if HoldsPrivilege(SE_BACKUP_NAME) and HoldsPrivilege(SE_RESTORE_NAME) then
begin
PrivilegeByName(SE_BACKUP_NAME).Enabled := True;
PrivilegeByName(SE_RESTORE_NAME).Enabled := True;
MyReg := TRegistry.Create;
try
MyReg.RootKey := HKEY_LOCAL_MACHINE;
MyReg.UnLoadKey('TEMP_HIVE'); //unload hive to ensure one is not already loaded
if MyReg.LoadKey('TEMP_HIVE', sPathToUserHive) then
begin
//ShowMessage( 'Loaded' );
MyReg.OpenKey('TEMP_HIVE', False);
if MyReg.OpenKey('TEMP_HIVE\Environment', True) then
begin
// --- Make changes *here* ---
//
MyReg.WriteString('KEY_TO_WRITE', 'VALUE_TO_WRITE');
//
//
end;
//Alright, close it up
MyReg.CloseKey;
MyReg.UnLoadKey('TEMP_HIVE');
//let's unload the hive since we are done with it
end
else
begin
WriteLn('Error Loading: ' + sPathToUserHive);
end;
finally
FreeAndNil(MyReg);
end;
end;
WriteLn('Required privilege not held');
end;
finally
FreeAndNil(UserPriv);
end;
end;
我剛剛寫了一個VBScript來完成這個任務。 我用它來修改一些Internet Explorer設置,但您可以根據需要自定義它。 它還演示了一般過程:
Option Explicit
Dim fso
Dim WshShell
Dim objShell
Dim RegRoot
Dim strRegPathParent01
Dim strRegPathParent02
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.shell")
'==============================================
' Change variables here
'==============================================
'
'This is where our HKCU is temporarily loaded, and where we need to write to it
RegRoot = "HKLM\TEMPHIVE"
'
strRegPathParent01 = "Software\Microsoft\Windows\CurrentVersion\Internet Settings"
strRegPathParent02 = "Software\Microsoft\Internet Explorer\Main"
'
'======================================================================
Call ChangeRegKeys() 'Sets registry keys per user
Sub ChangeRegKeys
'Option Explicit
On Error Resume Next
Const USERPROFILE = 40
Const APPDATA = 26
Dim iResult
Dim iResult1
Dim iResult2
Dim objShell
Dim strUserProfile
Dim objUserProfile
Dim strAppDataFolder
Dim strAppData
Dim objDocsAndSettings
Dim objUser
Set objShell = CreateObject("Shell.Application")
Dim sCurrentUser
sCurrentUser = WshShell.ExpandEnvironmentStrings("%USERNAME%")
strUserProfile = objShell.Namespace(USERPROFILE).self.path
Set objUserProfile = fso.GetFolder(strUserProfile)
Set objDocsAndSettings = fso.GetFolder(objUserProfile.ParentFolder)
'Update settings for the user running the script
'(0 = default, 1 = disable password cache)
WshShell.RegWrite "HKCU\" & strRegPathParent01 & "\DisablePasswordCaching", "00000001", "REG_DWORD"
WshShell.RegWrite "HKCU\" & strRegPathParent02 & "\FormSuggest PW Ask", "no", "REG_SZ"
strAppDataFolder = objShell.Namespace(APPDATA).self.path
strAppData = fso.GetFolder(strAppDataFolder).Name
' Enumerate subfolders of documents and settings folder
For Each objUser In objDocsAndSettings.SubFolders
' Check if application data folder exists in user subfolder
If fso.FolderExists(objUser.Path & "\" & strAppData) Then
'WScript.Echo "AppData found for user " & objUser.Name
If ((objUser.Name <> "All Users") and _
(objUser.Name <> sCurrentUser) and _
(objUser.Name <> "LocalService") and _
(objUser.Name <> "NetworkService")) then
'Load user's HKCU into temp area under HKLM
iResult1 = WshShell.Run("reg.exe load " & RegRoot & " " & chr(34) & objDocsAndSettings & "\" & objUser.Name & "\NTUSER.DAT" & chr(34), 0, True)
If iResult1 <> 0 Then
WScript.Echo("*** An error occurred while loading HKCU: " & objUser.Name)
Else
WScript.Echo("HKCU loaded: " & objUser.Name)
End If
WshShell.RegWrite RegRoot & "\" & strRegPathParent01 & "\DisablePasswordCaching", "00000001", "REG_DWORD"
WshShell.RegWrite RegRoot & "\" & strRegPathParent02 & "\FormSuggest PW Ask", "no", "REG_SZ"
iResult2 = WshShell.Run("reg.exe unload " & RegRoot,0, True) 'Unload HKCU from HKLM
If iResult2 <> 0 Then
WScript.Echo("*** An error occurred while unloading HKCU: " & objUser.Name & vbcrlf)
Else
WScript.Echo(" unloaded: " & objUser.Name & vbcrlf)
End If
End If
Else
'WScript.Echo "No AppData found for user " & objUser.Name
End If
Next
End Sub
前幾天我們遇到了完全相同的問題。
我們發現您可以打開HKEY_USERS
配置單元並將更改寫入每個用戶的SID。
並且,如果要為任何新用戶提供設置,還應將設置應用於HKEY_USERS/.DEFAULT
鍵。
也就是說,只需將您的設置寫入......
HKEY_USERS\S-1-5-XX-XXXXXXXX-XXXXXXXXX-XXXXXXXX-XXXX\Software\...
對於每個存在的SID和:
HKEY_USERS\.DEFAULT\Software\...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.