当以SYSTEM用户身份运行脚本时(批处理)更改当前登录的Windows用户的注册表值 [英] change registry value for currently logged in Windows user, when running script as SYSTEM user (batch)
问题描述
我在批处理脚本中有此代码.
I have this code in a batch script.
REG ADD HKEY_CURRENT_USER\MyKey /ve /t REG_DWORD /d 1 /f
问题是脚本是使用系统帐户作为Windows中的计划任务运行的.使用系统帐户运行任务时,它不会将该值应用于当前登录的Windows用户注册表.我找不到设置任务以使用当前登录用户的方法,因此不得不将其设置为使用系统帐户.
The problem is the script is run using the system account as a scheduled task in Windows. When using the system account to run the task it does not apply the value to the currently logged in Windows user's registry. I could not find a way to set the task to use the currently logged in user, so had to set it to use the system account.
我尝试使用此解决方案;但是,它不适用于当前登录的用户,因为另一个进程正在使用NTUSER.DAT文件.
I attempted to use this solution; however it does not apply to the currently logged in user because the NTUSER.DAT file is being used by another process.
我还尝试导入.reg文件;但是,这也不会将其应用于当前登录的用户.
I also attempted to import a .reg file; however that also does not apply it to the currently logged in user.
如何将设置应用到HKEY_USERS\*\MyKey
?最好使用批处理?或者,如何以当前登录的Windows用户身份运行计划任务?
How can I make apply the setting to HKEY_USERS\*\MyKey
? Preferably using batch? Alternatively how can I run a scheduled task as the currently logged in Windows user?
推荐答案
我用以下代码解决了这个问题. (当然是替换MyKey)
I solved this with the following code. (of course replacing MyKey)
这仅适用于已登录的用户注册表.当我在启动应用程序时正在运行此代码时,在读取注册表之前,这对于我的情况来说效果很好.
This will apply to only signed in users registry. As I am running this code when an application is started, before it reads the registry, this works well for my case.
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%A in ('reg query HKEY_USERS') do (
set hive=%%A
if "!hive:~11,8!"=="S-1-5-21" (
if not "!hive:~-7!"=="Classes" (
REG ADD !hive!\MyKey /ve /t REG_DWORD /d 1 /f
)
)
)
endlocal
如果您想应用到所有用户(无论是否已登录),都可以完成此操作.
If you want to apply to all users whether signed in or not then this should accomplish that.
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%A in ('reg query HKEY_USERS') do (
set hive=%%A
if not "!hive:~-7!"=="Classes" (
REG ADD !hive!\MyKey /ve /t REG_DWORD /d 1 /f
)
)
endlocal
这篇关于当以SYSTEM用户身份运行脚本时(批处理)更改当前登录的Windows用户的注册表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!