3

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 = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\"

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

user692942
  • 16,398
  • 7
  • 76
  • 175
Vanessa
  • 31
  • 1
  • 1
  • 2

3 Answers3

3

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: Invalid root in registry key "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\legalnoticecaption".

It's not invalid root, it's something like access denied rather because writing to HKLM requires elevated privileges (or run as administrator).

Note:

  • You should change LegalNoticeText value together with LegalNoticeCaption one.
  • Under the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ 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.

Run as administrator:

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 = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\"
strWinLog_2 = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\"

' 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
JosefZ
  • 28,460
  • 5
  • 44
  • 83
0

For me your code run perfect. For other user that want details over this i recommend this site: http://ss64.com/vb/regread.html and ss64.com/vb/regwrite.html Both links detail exactly the procedure that you create.

Mastercafe
  • 91
  • 9
0

Make sure to add this:

Function RunAsAdmin()
    If WScript.Arguments.length = 0 Then
        CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & _
        WScript.ScriptFullName & """" & " RunAsAdministrator",,"runas", 1
        WScript.Quit
    End If
End Function

It will run as Admin and if it doesnt work then your key is incorrect.