VBS - Writing in HKU registry.
Hello, All.
Any idea on how to write a value of REG_BINARY data type using VBS?
HKU\.Default\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections
Following the links you provide here's my code. I think there's something wrong because it can't write the registry
Option Explicit Dim uBinary, strPath, runReturn, objWMIReg Const HKEY_USERS = &H8000003 Set objWMIReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv") 'Hexa Value to write '(3c,00,00,00,03,00,00,00,01,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00) strPath = ".DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections" 'Converted to hexa to decimal uBinary = Array (60,0,0,0,3,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) runReturn = objWMIReg.SetBinaryValue(HKEY_USERS, strPath, "DefaultConnectionSettings", uBinary) If (runReturn = 0) then msgbox "Successful" Else msgbox "Failed" End if Set objWMIReg = Nothing
Answers (4)
regWriteBinary "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", "DefaultConnectionSettings", "60,0,0,0,3,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0" Sub regWriteBinary(sRegKey, sRegValue, sBinaryData) ' Expects sBinaryData to be a comma separated string of hex values ' sRegKey needs to start with full registry root, e.g. HKEY_CURRENT_USER and not VBScript short form HKCU Dim oShell, oFSO, oFile, oExec Dim sTempFile Set oShell = CreateObject("WScript.Shell") Set oFSO = CreateObject("Scripting.FileSystemObject") sTempFile = oShell.ExpandEnvironmentStrings("%temp%\RegWriteBinary.reg") Set oFile = oFSO.CreateTextFile(sTempFile,True) oFile.WriteLine("Windows Registry Editor Version 5.00") oFile.WriteLine("") oFile.WriteLine("[" & sRegKey & "]") oFile.WriteLine(Chr(34) & sRegValue & Chr(34) & "=" & "hex:" & sBinaryData) oFile.Close Set oExec = oShell.Exec("reg.exe import """ & sTempFile & """") If InStr(1,oExec.StdErr.ReadAll,"operation completed successfully",vbTextCompare) Then ' Yes the success text IS sent out via StdErr and NOT StdOut WScript.Echo "Registry updated sucessfully" oFSO.DeleteFile sTempFile Else WScript.Echo "regWriteBinary: Registry import of " & sTempFile & " failed: " & oExec.StdErr.ReadAll End If End Sub
The below VBScript code should work
regWriteBinary "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", "DefaultConnectionSettings", "60,0,0,0,3,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
Sub regWriteBinary(sRegKey, sRegValue, sBinaryData)
' Expects sBinaryData to be a comma separated string of hex values
' sRegKey needs to start with full registry root, e.g. HKEY_CURRENT_USER and not VBScript short form HKCU
Dim oShell, oFSO, oFile, oExec
Dim sTempFile
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTempFile = oShell.ExpandEnvironmentStrings("%temp%\RegWriteBinary.reg")
Set oFile = oFSO.CreateTextFile(sTempFile,True)
oFile.WriteLine("Windows Registry Editor Version 5.00")
oFile.WriteLine("")
oFile.WriteLine("[" & sRegKey & "]")
oFile.WriteLine(Chr(34) & sRegValue & Chr(34) & "=" & "hex:" & sBinaryData)
oFile.Close
Set oExec = oShell.Exec("reg.exe import """ & sTempFile & """")
If InStr(1,oExec.StdErr.ReadAll,"operation completed successfully",vbTextCompare) Then
' Yes the success text IS sent out via StdErr and NOT StdOut
WScript.Echo "Registry updated sucessfully"
oFSO.DeleteFile sTempFile
Else
WScript.Echo "regWriteBinary: Registry import of " & sTempFile & " failed: " & oExec.StdErr.ReadAll
End If
End Sub
Comments:
-
Yes. I tried this one thank you. - ajcbasilio 11 years ago
Comments:
-
Would you mind to check? I updated my question. - ajcbasilio 11 years ago
This may also help with binary values:
http://blogs.technet.com/b/heyscriptingguy/archive/2005/02/24/how-can-i-write-binary-data-to-the-registry.aspx
Good luck & Let us know
Comments:
-
Can you check? I updated my question. - ajcbasilio 11 years ago
http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/a-simple-way-to-write-to-the-registry-with-vbscript/
Comments:
-
Would you mind to check? I updated my question. - ajcbasilio 11 years ago
-
you may want to approach it this way also
http://www.itninja.com/blog/view/how-to-make-changes-to-the-default-users-hive-as-a-post-taks - SMal.tmcc 11 years ago