使用 VBS 更新注册表 [英] Update registry using VBS
问题描述
我正在尝试使用 VBScript 更新我们 PC 上的法律说明.到目前为止,我已经能够读取值,但我似乎无法让它写入任何值.我运行脚本时没有收到错误,它只是没有改变任何东西.这是我第一次这样做,我的经验有限;任何见解将不胜感激:
I'm trying to update the legal caption on our PCs using a VBScript. So far, I've been able to read values but I can't seem to get it to write any values. I don't get an error when I run the script, it just doesn't change anything. It's the first time I'm doing this and I have limited experience; any insight would be appreciated:
Dim objShell
Dim strMessage, strWelcome, strWinLogon
' Set the string values
strWelcome = "legalnoticecaption"
strMessage = "did this work"
strWinLogon = "HKLMSOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem"
' Create the Shell object
Set wshShell = CreateObject("WScript.Shell")
'Display string Values
Wscript.Echo "key to update: " & strWelcome
Wscript.Echo "key value to enter: " & strMessage
Wscript.Echo "Existing key value: " & wshShell.RegRead(strWinLogon & strWelcome)
' the crucial command in this script - rewrite the registry
wshShell.RegWrite strWinLogon & strWelcome, strMessage, "REG_SZ"
' Did it work?
Wscript.Echo "new key value: " & wshShell.RegRead(strWinLogon & strWelcome)
set wshShell = Nothing
注意:这些是目前的测试值.
NOTE: These are testing values at the moment.
推荐答案
您的脚本似乎没有错误.但是,由 cscript 28416995.vbs
启动返回下一个错误(其中 22 = WshShell.RegWrite
行):
Your script seems to be bug-less. However, launched by cscript 28416995.vbs
returns next error (where 22 = WshShell.RegWrite
line):
28416995.vbs(22, 1) WshShell.RegWrite:注册表项HKLMSOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystemlegalnoticecaption"中的根无效.
28416995.vbs(22, 1) WshShell.RegWrite: Invalid root in registry key "HKLMSOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystemlegalnoticecaption".
这不是无效的根,它类似于访问被拒绝,而是因为写入HKLM
需要提升的权限(或以管理员身份运行).
It's not invalid root, it's something like access denied rather because writing to HKLM
requires elevated privileges (or run as administrator).
注意:
- 您应该将
LegalNoticeText
值与LegalNoticeCaption
一并更改. - 在
HKLMSOFTWAREMicrosoftWindows NTCurrentVersionWinlogon
注册表项下,也有两个值.对于这种情况(如果计算机未连接到域或禁用了组策略)应该在下一个脚本中工作.
- You should change
LegalNoticeText
value together withLegalNoticeCaption
one. - Under the
HKLMSOFTWAREMicrosoftWindows NTCurrentVersionWinlogon
registry key there both values reside as well. For this case (if a computer is not connected to a domain or with group policy disabled) should work next script.
以管理员身份运行:
option explicit
On Error Goto 0
Dim wshShell
Dim strResult, strMessage, strWelcome, strWinLogon, strWinLog_2, strWinLTxt
strResult=Wscript.ScriptName
' Set the string values
strWinLTxt = "legalnoticetext"
strWelcome = "legalnoticecaption"
strMessage = "did this work"
strWinLogon = "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionWinlogon"
strWinLog_2 = "HKLMSOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem"
' Create the Shell object
Set wshShell = CreateObject("WScript.Shell")
'Display string Values
' continue execution if requested registry values not present
On Error Resume Next
strResult = strResult & vbNewLine & "Existing Caption Policies: " _
& wshShell.RegRead(strWinLog_2 & strWelcome)
strResult = strResult & vbNewLine & "Existing Text Policies: " _
& wshShell.RegRead(strWinLog_2 & strWinLTxt)
On Error Goto 0
strResult = strResult & vbNewLine & "Existing Caption Winlogon: " _
& wshShell.RegRead(strWinLogon & strWelcome)
strResult = strResult & vbNewLine & "Existing Text Winlogon: " _
& wshShell.RegRead(strWinLogon & strWinLTxt)
strResult = strResult & vbNewLine
strResult = strResult & vbNewLine & "key to update: " & strWelcome
strResult = strResult & vbNewLine & "key value to enter: " & strMessage
' the crucial command in this script - rewrite the registry
wshShell.RegWrite strWinLogon & strWelcome, strMessage, "REG_SZ"
wshShell.RegWrite strWinLogon & strWinLTxt, UCase( strMessage), "REG_SZ"
' Did it work?
strResult = strResult & vbNewLine
strResult = strResult & vbNewLine _
& "new key Capt. value: " & wshShell.RegRead(strWinLogon & strWelcome)
strResult = strResult & vbNewLine _
& "new key Text value: " & wshShell.RegRead(strWinLogon & strWinLTxt)
Wscript.Echo strResult
set wshShell = Nothing
这篇关于使用 VBS 更新注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!