Import settings from a registry file

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 
Visual Basic Code:
'<!--******************************************************************** '* '* File: ImportRegfile.vbs '* Author: Frank Wenzel '* Created: 19.03.2010 '* Version: 1.00 '* Modified: 00.00.0000 '* '* Description: Liest eine bestehende Registry Datei ein und erstellt '* die Werte in der Registrierung. Auch auf remote Computer. '* Read Windows Registry Editor Version 5.00 Unicode file. '* '* needed: WSH 5.6 '* '* '* '********************************************************************--> Option Explicit Dim strComputer strComputer = "."   Function ImportRegFile() Dim oReg, oFSO, oFile, objRegEx, strFile, filetxt, line Dim bolHex7, bolHex2, bolCreateHex Dim strLine, strString, strCharacterCheck, strPattern, strCase, strValueName, strValue, strName, strHex7Value Dim intStart, intEnd, intLengh, x, Y Dim Matches, Match, HexValue, HexPart, dwValue, HKEY, strMainKey Dim arrString, arrDWord, arrHex, arrMultisz, szMulti, arrBinary, szBinary Const ForReading = 1   Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\default:StdRegProv") Set oFSO = CreateObject("Scripting.FileSystemObject") strFile = "D:\Script\WSUS\WSUS-Patch-Client\WSUS-Settings3.reg"   If oFSO.FileExists(strFile) Then bolHex7 = False bolHex2 = False bolCreateHex = False Set oFile = oFSO.GetFile(strFile) Set filetxt = oFile.OpenasTextStream(ForReading, -1) Do While Not filetxt.AtEndOfStream line = filetxt.ReadLine strString = line strCharacterCheck = "" Set objRegEx = New RegExp strPattern = "^.+""="".+|.+=hex:|.+=dword:.+|.+=hex\(7\):|.+=hex\(2\):|\[HKEY_CLASSES_ROOT\\.+|\[HKEY_CURRENT_USER\\.+|\[HKEY_LOCAL_MACHINE\\.+|\[HKEY_USERS\\.+|\[HKEY_CURRENT_CONFIG\\.+" objRegEx.Pattern = strPattern intEnd = 0 objRegEx.Global= True objRegEx.IgnoreCase = True Set Matches = objRegEx.Execute(strString) If Matches.Count = 1 Then For Each Match in Matches strCase = "" intStart = Instr(Match.Value, "=") If InStr(Match.Value, """=""") > 0 Then intStart = InStr(Match.Value, """=""") intLengh = 3 ElseIf InStr(Match.Value, ":") > 0 Then intEnd = InStr(Match.Value, ":") +1 intLengh = intEnd - intStart ElseIf InStr(Match.Value, "[") > 0 Then strMainKey = Trim(Replace(Mid(Match.Value, Instr(Match.Value, "\") +1), "]", "")) intStart = InStr(Match.Value, "[") +1 intEnd = InStr(Match.Value, "\") intLengh = intEnd - intStart End If strCase = Mid(Match.Value, intStart, intLengh) Select Case strCase Case "HKEY_CLASSES_ROOT" HKEY = &amp;H80000000 strCharacterCheck = "REGMAINKEY" Case "HKEY_CURRENT_USER" HKEY = &amp;H80000001 strCharacterCheck = "REGMAINKEY" Case "HKEY_LOCAL_MACHINE" HKEY = &amp;H80000002 strCharacterCheck = "REGMAINKEY" Case "HKEY_USERS" HKEY = &amp;H80000003 strCharacterCheck = "REGMAINKEY" Case "HKEY_CURRENT_CONFIG" HKEY = &amp;H80000005 strCharacterCheck = "REGMAINKEY" Case """=""" strCharacterCheck = "STRING" Case "=dword:" strCharacterCheck = "DWORD" Case "=hex:" strCharacterCheck = "HEX7" Case "=hex(7):" strCharacterCheck = "HEX7" Case "=hex(2):" strCharacterCheck = "HEX7" Case Else WScript.Echo "maybe another line, but not a valid registry value." WScript.Echo strString End Select Next ElseIf strString <> "" Then If InStr(strString, ",\") Then strCharacterCheck = "HEX FURTHERLINE" ElseIf Left(strString, 2) = " " Then strCharacterCheck = "HEX LASTLINE" End If End If Set objRegEx = Nothing Select Case strCharacterCheck Case "REGMAINKEY" oReg.CreateKey HKEY, strMainKey Case "STRING" strLine = Replace(line, """", "") arrString = Split(strLine, "=", -1, 1) strValueName = arrString(0) strValue = arrString(1) 'WScript.Echo "STRING 1: " &amp; strValueName &amp; " -> " &amp; strValue oReg.SetStringValue HKEY, strMainKey, strValueName, strValue Case "DWORD" strLine = Replace(line, """", "") strLine = Replace(strLine, "dword:", "") arrDWord = Split(strLine, "=", -1, 1) strValueName = arrDWord(0) HexValue = arrDWord(1) For x = 1 to len(HexValue) Step 2 HexPart = mid(HexValue, x, 2) strName = strName &amp; Chr("&amp;h" &amp; HexPart) Next dwValue = CLng("&amp;h" &amp; HexValue) 'WScript.Echo "DWORD 1: " &amp; strValueName &amp; " -> " &amp; dwValue oReg.SetDWORDValue HKEY, strMainKey, strValueName, dwValue Case "HEX7" strHex7Value = Replace(line, """", "") If InStr(strHex7Value, "hex(7):") > 0 Then strHex7Value = Replace(strHex7Value, "hex(7):", "") bolHex7 = True ElseIf InStr(strHex7Value, "hex(2):") > 0 Then strHex7Value = Replace(strHex7Value, "hex(2):", "") bolHex2 = True ElseIf InStr(strHex7Value, "hex:") > 0 Then strHex7Value = Replace(strHex7Value, "hex:", "") End If If InStr(line, "\") > 0 Then strHex7Value = Replace(strHex7Value, "\", "") Else arrString = Split(strHex7Value, "=", -1, 1) bolCreateHex = True End If Case "HEX FURTHERLINE" strLine = Replace(Trim(line), "\", "") strHex7Value = strHex7Value &amp; strLine 'WScript.Echo "HEX FURTHERLINE 1: " &amp; strHex7Value Case "HEX LASTLINE" strHex7Value = strHex7Value &amp; Trim(line) bolCreateHex = True 'WScript.Echo "HEX LASTLINE 1: " &amp; line End Select If bolCreateHex = True Then arrString = Split(strHex7Value, "=", -1, 1) arrHex = Split(arrString(1), ",") For Y = 0 To UBound(arrHex) Step 2 If arrHex(Y) <> "00" Then arrMultiSZ = arrMultiSZ &amp; Chr(CInt("&amp;H" &amp; arrHex(Y))) arrBinary = arrBinary &amp; "," &amp; "&amp;H" &amp; arrHex(Y) strValue = strValue &amp; Chr(CInt("&amp;H" &amp; arrHex(Y))) Else arrMultiSZ = arrMultiSZ &amp; ";" arrBinary = arrBinary &amp; "," &amp; "&amp;H" &amp; arrHex(Y) End If Next szMulti = Split(arrMultiSZ, ";") szBinary = Split(Mid(arrBinary, 2), ",") strValueName = arrString(0) If bolHex7 = True Then oReg.SetMultiStringValue HKEY, strMainKey, strValueName, szMulti bolHex7 = False ElseIf bolHex2 = True Then oReg.SetExpandedStringValue HKEY, strMainKey, strValueName, strValue bolHex2 = False Else oReg.SetBinaryValue HKEY, strMainKey, strValueName, szBinary End If bolCreateHex = False arrString = "" arrHex = "" arrMultiSZ = "" arrBinary = "" szMulti = "" szBinary = "" strValueName = "" strHex7Value = "" strValue = "" End If Loop Else Wscript.Echo "File " &amp; strFile &amp; "doesn't exist" End If Set oReg = Nothing Set oFSO = Nothing End Function   ImportRegFile