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
Answer Summary:
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
Cancel
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Community Chosen Answer

3

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


 

Answered 01/31/2013 by: jagadeish
Red Belt

  • Yes. I tried this one thank you.
Please log in to comment

Answers


  • Would you mind to check? I updated my question.
Please log in to comment
1
Answered 01/29/2013 by: RaSko
Purple Belt

  • Can you check? I updated my question.
Please log in to comment
0

http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/a-simple-way-to-write-to-the-registry-with-vbscript/

Answered 01/29/2013 by: SMal.tmcc
Red Belt

  • Would you mind to check? I updated my question.
    • 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
Please log in to comment
Answer this question or Comment on this question for clarity