Advertisement
Guest User

Convert-Reg2Xml

a guest
Aug 3rd, 2018
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Convert-RegEscapeCodes
  2. {
  3. Param(
  4.     [Parameter(Position=1)][string]$regstring)
  5.     return $regstring.Replace("\\","\").Replace('\"','"')
  6. }
  7.  
  8. function Convert-Reg2Xml
  9. {
  10. Param(
  11.   [Parameter(Mandatory=$True)][string]$regPath,
  12.   [Parameter(Mandatory=$True)][string]$xmlPath
  13.   )
  14.  
  15.   $clsidCollection = "{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"
  16.   $clsidRegistry =   "{9CD4B2F4-923D-47f5-A062-E897DD1DAD50}"
  17.  
  18.   $settings = New-Object System.Xml.XmlWriterSettings
  19.   $settings.Indent=$True
  20.   $settings.Encoding = [System.Text.Encoding]::UTF8
  21.  
  22.   $xml = [System.Xml.XmlWriter]::Create($xmlPath,$settings)
  23.   $descr = "Imported Reg File"
  24.   $action = "U"
  25.  
  26.   $unicoder = New-Object System.Text.UnicodeEncoding
  27.  
  28.   $lastHive="";
  29.   $lastKey="";
  30.   $sr=New-Object System.IO.StreamReader($regPath)
  31.  
  32.   $lastHive=""
  33.   $lastKey=""
  34.  
  35.   $collectionCount=0
  36.  
  37.   while (!$sr.EndOfStream)
  38.   {
  39.  
  40.     $line = $sr.ReadLine()
  41.     if ($line.StartsWith("["))
  42.     {
  43.         $currentHive=$line.Substring(1,$line.IndexOf("\")-1)
  44.         $currentKey=$line.Substring($line.IndexOf("\")+1,$line.Length-$line.IndexOf("\")-2)
  45.  
  46.         if ($lastHive -eq "")
  47.         {
  48.             $xml.WriteStartElement("Collection")
  49.             $xml.WriteAttributeString("clsid",$clsidCollection)
  50.             $xml.WriteAttributeString("name",$currentHive)
  51.             $collectionCount++
  52.  
  53.             ForEach ($key in $currentKey.Split('\'))
  54.             {
  55.                 $xml.WriteStartElement("Collection")
  56.                 $xml.WriteAttributeString("clsid",$clsidCollection)
  57.                 $xml.WriteAttributeString("name",$key)
  58.                 $collectionCount++
  59.             }
  60.         }
  61.         else
  62.         {
  63.            # hives don't match - settings.xml doesn't support this!
  64.             if ($currentHive -ne $lastHive)
  65.             {
  66.                # invalid - settings.xml only supports one HIVE type per file
  67.                Throw "Reg file format is not supported by settings .XML. Please use only $currentHive or $lastHive per XML file"
  68.                return
  69.             }
  70.             else
  71.             {
  72.                 # going up a key
  73.                 if ($currentKey.StartsWith($lastKey + "\"))
  74.                 {
  75.                     $newKey=$currentKey.Substring($lastKey.Length+1)
  76.                     ForEach ($key in $newKey.Split('\'))
  77.                     {
  78.                         $xml.WriteStartElement("Collection")
  79.                         $xml.WriteAttributeString("clsid",$clsidCollection)
  80.                         $xml.WriteAttributeString("name",$key)
  81.                         $collectionCount++
  82.                     }
  83.                 }
  84.                 else
  85.                 {
  86.                     # funky logic to handle change in key path
  87.                     # maybe this logic even works :)
  88.                     $currentKeySplit=$currentKey.Split('\')
  89.                     $lastKeySplit=$lastKey.Split('\')
  90.  
  91.                     $match=$true
  92.  
  93.                     $i=-1
  94.  
  95.                     while ($match)
  96.                     {
  97.                         $i++
  98.                         if ($i -ge $currentKeySplit.Length -or $i -ge $lastKeySplit.Length)
  99.                         {
  100.                             $match=$false
  101.                         }
  102.                         else
  103.                         {
  104.                             if ($currentKeySplit[$i] -ne $lastKeySplit[$i]) { $match=$false }
  105.                         }
  106.                     }
  107.  
  108.                     for ($x=$lastKeySplit.Length;$x -gt $i;$x--)
  109.                     {
  110.                         $xml.WriteEndElement()
  111.                         $collectionCount--
  112.                     }
  113.  
  114.                     for ($x=$i;$x -lt $currentKeySplit.Length;$x++)
  115.                     {
  116.                         $xml.WriteStartElement("Collection")
  117.                         $xml.WriteAttributeString("clsid",$clsidCollection)
  118.                         $xml.WriteAttributeString("name",$currentKeySplit[$x])
  119.                         $collectionCount++
  120.                     }
  121.                    
  122.                 }
  123.             }
  124.         }
  125.        
  126.         $lastHive=$currentHive
  127.         $lastKey=$currentKey
  128.     }
  129.     else
  130.     {
  131.         if ($line.Contains("="))
  132.         {
  133.             $regType=[Microsoft.Win32.RegistryValueKind]::Unknown
  134.  
  135.             # detect registry type
  136.             if ($line.StartsWith("@=") -or $line.Contains('"="')) { $regType=[Microsoft.Win32.RegistryValueKind]::String }
  137.             if ($line.Contains("=hex:")) { $regType=[Microsoft.Win32.RegistryValueKind]::Binary }
  138.             if ($line.Contains("=dword:")) { $regType=[Microsoft.Win32.RegistryValueKind]::DWord }
  139.             if ($line.Contains("=hex(7):")) { $regType=[Microsoft.Win32.RegistryValueKind]::MultiString }
  140.             if ($line.Contains("=hex(2):")) { $regType=[Microsoft.Win32.RegistryValueKind]::ExpandString }
  141.             if ($line.Contains("=hex(b):")) { $regType=[Microsoft.Win32.RegistryValueKind]::QWord }
  142.  
  143.             switch ($regType)
  144.             {
  145.                 # *** PROCESS REG_SZ
  146.                 ([Microsoft.Win32.RegistryValueKind]::String)
  147.                 {
  148.                     $default="0"
  149.                     if ($line.StartsWith("@="))
  150.                     {
  151.                         $valueName=""
  152.                         $value=$line.Substring(3,$line.Length-4)
  153.                         "Name = '$valueName' Value = '$value'"
  154.                         $default="1"
  155.                     }
  156.                     else
  157.                     {
  158.                         $i = $line.IndexOf('"="')
  159.                         $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  160.                         $value=Convert-RegEscapeCodes $line.Substring($i+3,$line.Length-$i-4)
  161.                        "Name = '$valueName' Value = '$value'"
  162.                  
  163.                     }
  164.  
  165.                     $xml.WriteStartElement("Registry")
  166.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  167.                     $xml.WriteAttributeString("name",$valueName)
  168.                     $xml.WriteAttributeString("descr",$descr)
  169.                     $xml.WriteAttributeString("image","7")
  170.                    
  171.                     $xml.WriteStartElement("Properties")
  172.                     $xml.WriteAttributeString("action",$action)
  173.                     $xml.WriteAttributeString("hive",$currentHive)
  174.                     $xml.WriteAttributeString("key",$currentKey)
  175.                     $xml.WriteattributeString("name",$valueName)
  176.                     $xml.WriteattributeString("default",$default)
  177.                     $xml.WriteattributeString("type","REG_SZ")
  178.                     $xml.WriteattributeString("displayDecimal","0")
  179.                     $xml.WriteAttributeString("value",$value)
  180.                     $xml.WriteEndElement()
  181.                     $xml.WriteEndElement()
  182.          }
  183.  
  184.                 # *** PROCESS REG_BINARY
  185.                 ([Microsoft.Win32.RegistryValueKind]::Binary)
  186.                 {
  187.                     # read binary key to end
  188.                     while ($line.EndsWith("\"))
  189.                     {
  190.                         $line=$line.Substring(0,$line.Length-1)+$sr.ReadLine().Trim()
  191.                     }
  192.  
  193.                     $i = $line.IndexOf('"=hex:')
  194.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  195.                     $value=$line.Substring($i+6).Replace(",","")
  196.                     "Name = '$valueName' Value = '$value'"
  197.  
  198.                     # build XML
  199.                     $xml.WriteStartElement("Registry")
  200.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  201.                     $xml.WriteAttributeString("name",$valueName)
  202.                     $xml.WriteAttributeString("descr",$descr)
  203.                     $xml.WriteAttributeString("image","17")
  204.                    
  205.                     $xml.WriteStartElement("Properties")
  206.                     $xml.WriteAttributeString("action",$action)
  207.                     $xml.WriteAttributeString("hive",$currentHive)
  208.                     $xml.WriteAttributeString("key",$currentKey)
  209.                     $xml.WriteattributeString("name",$valueName)
  210.                     $xml.WriteattributeString("default","0")
  211.                     $xml.WriteattributeString("type","REG_BINARY")
  212.                     $xml.WriteattributeString("displayDecimal","0")
  213.                     $xml.WriteAttributeString("value",$value)
  214.                     $xml.WriteEndElement()
  215.                     $xml.WriteEndElement()
  216.  
  217.                 }
  218.  
  219.                 # *** PROCESS REG_DWORD
  220.                 ([Microsoft.Win32.RegistryValueKind]::DWord)
  221.                 {
  222.                
  223.                     $i = $line.IndexOf('"=dword:')
  224.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  225.                     $value=$line.Substring($i+8).ToUpper()
  226.                      "Name = '$valueName' Value = '$value'"
  227.  
  228.                     # build XML
  229.                     $xml.WriteStartElement("Registry")
  230.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  231.                     $xml.WriteAttributeString("name",$valueName)
  232.                     $xml.WriteAttributeString("descr",$descr)
  233.                     $xml.WriteAttributeString("image","17")
  234.                    
  235.                     $xml.WriteStartElement("Properties")
  236.                     $xml.WriteAttributeString("action",$action)
  237.                     $xml.WriteAttributeString("hive",$currentHive)
  238.                     $xml.WriteAttributeString("key",$currentKey)
  239.                     $xml.WriteattributeString("name",$valueName)
  240.                     $xml.WriteattributeString("default","0")
  241.                     $xml.WriteattributeString("type","REG_DWORD")
  242.                     $xml.WriteattributeString("displayDecimal","0")
  243.                     $xml.WriteAttributeString("value",$value)
  244.                     $xml.WriteEndElement()
  245.                     $xml.WriteEndElement()
  246.                 }
  247.  
  248.                 # *** PROCESS REG_QWORD
  249.                 ([Microsoft.Win32.RegistryValueKind]::QWord)
  250.                 {
  251.                     $i = $line.IndexOf('"=hex(b):')
  252.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  253.                     $tempValue=$line.Substring($i+9).Replace(",","").ToUpper()
  254.                     $value=""
  255.  
  256.                     # unreverse QWORD for settings.xml format
  257.                     for ($i = $tempValue.Length -2;$i -gt 0;$i-=2)
  258.                     {
  259.                         $value+=$tempValue.Substring($i,2)
  260.                     }
  261.                    
  262.                      "Name = '$valueName' Value = '$value'"
  263.  
  264.                      # build XML
  265.                     $xml.WriteStartElement("Registry")
  266.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  267.                     $xml.WriteAttributeString("name",$valueName)
  268.                     $xml.WriteAttributeString("descr",$descr)
  269.                     $xml.WriteAttributeString("image","17")
  270.                    
  271.                     $xml.WriteStartElement("Properties")
  272.                     $xml.WriteAttributeString("action",$action)
  273.                     $xml.WriteAttributeString("hive",$currentHive)
  274.                     $xml.WriteAttributeString("key",$currentKey)
  275.                     $xml.WriteattributeString("name",$valueName)
  276.                     $xml.WriteattributeString("default","0")
  277.                     $xml.WriteattributeString("type","REG_QWORD")
  278.                     $xml.WriteattributeString("displayDecimal","0")
  279.                     $xml.WriteAttributeString("value",$value)
  280.                     $xml.WriteEndElement()
  281.                     $xml.WriteEndElement()
  282.                 }
  283.  
  284.                 # *** PROESS REG_MULTI_MZ
  285.                 ([Microsoft.Win32.RegistryValueKind]::MultiString)
  286.                 {
  287.                     # read binary key to end
  288.                     while ($line.EndsWith("\"))
  289.                     {
  290.                         $line=$line.Substring(0,$line.Length-1)+$sr.ReadLine().Trim()
  291.                     }
  292.  
  293.                     # read hex codes
  294.                     $i = $line.IndexOf('"=hex(7):')
  295.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  296.                     $value=$line.Substring($i+9).Replace(",","")
  297.                     # convert hex codes to binary array
  298.                     $byteLength=$value.Length/2
  299.                     $byte = New-Object Byte[] $byteLength
  300.                    
  301.                     $x=0
  302.                     for ($i=0;$i -lt $value.Length;$i+=2)
  303.                     {
  304.                         $byte[$x]="0x" + $value.Substring($i,2)
  305.                         $x++
  306.                     }
  307.  
  308.                     # convert binary array to unicode string
  309.                     $value=$unicoder.GetString($byte)
  310.  
  311.                     # retrieve multi values
  312.                     $values=$value.Replace("`0`0","").Split("`0")
  313.                    
  314.                     "Name = '$valueName'"
  315.  
  316.                      # build XML
  317.                     $xml.WriteStartElement("Registry")
  318.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  319.                     $xml.WriteAttributeString("name",$valueName)
  320.                     $xml.WriteAttributeString("descr",$descr)
  321.                     $xml.WriteAttributeString("image","7")
  322.                    
  323.                     $xml.WriteStartElement("Properties")
  324.                     $xml.WriteAttributeString("action",$action)
  325.                     $xml.WriteAttributeString("hive",$currentHive)
  326.                     $xml.WriteAttributeString("key",$currentKey)
  327.                     $xml.WriteattributeString("name",$valueName)
  328.                     $xml.WriteattributeString("default","0")
  329.                     $xml.WriteattributeString("type","REG_MULTI_SZ")
  330.                     $xml.WriteattributeString("displayDecimal","0")
  331.                     $xml.WriteAttributeString("value",$value.Replace("`0"," "))
  332.                    
  333.                     $x=1
  334.  
  335.                     $xml.WriteStartElement("Values")
  336.  
  337.                     ForEach ($value in $values)
  338.                     {
  339.                         $xml.WriteStartElement("Value")
  340.                         $xml.WriteString($value)
  341.                         "Value $x = '$value'"
  342.                         $xml.WriteEndElement()
  343.                     }
  344.                    
  345.                     $xml.WriteEndElement()
  346.                     $xml.WriteEndElement()
  347.                     $xml.WriteEndElement()
  348.                 }
  349.  
  350.                 ([Microsoft.Win32.RegistryValueKind]::ExpandString)
  351.                 {
  352.                     # read binary key to end
  353.                     while ($line.EndsWith("\"))
  354.                     {
  355.                         $line=$line.Substring(0,$line.Length-1)+$sr.ReadLine().Trim()
  356.                     }
  357.  
  358.                     # read hex codes
  359.                     $i = $line.IndexOf('"=hex(2):')
  360.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  361.                     $value=$line.Substring($i+9).Replace(",","")
  362.                     # convert hex codes to binary array
  363.                     $byteLength=$value.Length/2
  364.                     $byte = New-Object Byte[] $byteLength
  365.                    
  366.                     $x=0
  367.                     for ($i=0;$i -lt $value.Length;$i+=2)
  368.                     {
  369.                         $byte[$x]="0x" + $value.Substring($i,2)
  370.                         $x++
  371.                     }
  372.  
  373.                     # convert binary array to unicode string
  374.                     $value=$unicoder.GetString($byte).Replace("`0","")
  375.                     "Name = '$valueName' Value = '$value'"
  376.  
  377.                     $xml.WriteStartElement("Registry")
  378.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  379.                     $xml.WriteAttributeString("name",$valueName)
  380.                     $xml.WriteAttributeString("descr",$descr)
  381.                     $xml.WriteAttributeString("image","7")
  382.                    
  383.                     $xml.WriteStartElement("Properties")
  384.                     $xml.WriteAttributeString("action",$action)
  385.                     $xml.WriteAttributeString("hive",$currentHive)
  386.                     $xml.WriteAttributeString("key",$currentKey)
  387.                     $xml.WriteattributeString("name",$valueName)
  388.                     $xml.WriteattributeString("default",$default)
  389.                     $xml.WriteattributeString("type","REG_EXPAND_SZ")
  390.                     $xml.WriteattributeString("displayDecimal","0")
  391.                     $xml.WriteAttributeString("value",$value)
  392.                     $xml.WriteEndElement()
  393.                     $xml.WriteEndElement()
  394.                 }
  395.  
  396.             }
  397.  
  398.         }
  399.        
  400.     }
  401.  
  402.   }
  403.  
  404.   $sr.Close()
  405.   while ($collectionCount -gt 0)
  406.   {
  407.         $xml.WriteEndElement()
  408.         $collectionCount--
  409.     }
  410.  
  411.     $xml.Close()
  412.  
  413. }
  414.  
  415.  
  416. function Convert-Reg2Xml
  417. {
  418. Param(
  419.   [Parameter(Mandatory=$True)][string]$regPath,
  420.   [Parameter(Mandatory=$True)][string]$xmlPath
  421.   )
  422.  
  423.   $clsidCollection = "{53B533F5-224C-47e3-B01B-CA3B3F3FF4BF}"
  424.   $clsidRegistry =   "{9CD4B2F4-923D-47f5-A062-E897DD1DAD50}"
  425.  
  426.   $settings = New-Object System.Xml.XmlWriterSettings
  427.   $settings.Indent=$True
  428.   $settings.Encoding = [System.Text.Encoding]::UTF8
  429.  
  430.   $xml = [System.Xml.XmlWriter]::Create($xmlPath,$settings)
  431.   $descr = "Imported Reg File"
  432.   $action = "U"
  433.  
  434.   $unicoder = New-Object System.Text.UnicodeEncoding
  435.  
  436.   $lastHive="";
  437.   $lastKey="";
  438.   $sr=New-Object System.IO.StreamReader($regPath)
  439.  
  440.   $lastHive=""
  441.   $lastKey=""
  442.  
  443.   $collectionCount=0
  444.  
  445.   while (!$sr.EndOfStream)
  446.   {
  447.  
  448.     $line = $sr.ReadLine()
  449.     if ($line.StartsWith("["))
  450.     {
  451.         $currentHive=$line.Substring(1,$line.IndexOf("\")-1)
  452.         $currentKey=$line.Substring($line.IndexOf("\")+1,$line.Length-$line.IndexOf("\")-2)
  453.  
  454.         if ($lastHive -eq "")
  455.         {
  456.             $xml.WriteStartElement("Collection")
  457.             $xml.WriteAttributeString("clsid",$clsidCollection)
  458.             $xml.WriteAttributeString("name",$currentHive)
  459.             $collectionCount++
  460.  
  461.             ForEach ($key in $currentKey.Split('\'))
  462.             {
  463.                 $xml.WriteStartElement("Collection")
  464.                 $xml.WriteAttributeString("clsid",$clsidCollection)
  465.                 $xml.WriteAttributeString("name",$key)
  466.                 $collectionCount++
  467.             }
  468.         }
  469.         else
  470.         {
  471.            # hives don't match - settings.xml doesn't support this!
  472.             if ($currentHive -ne $lastHive)
  473.             {
  474.                # invalid - settings.xml only supports one HIVE type per file
  475.                Throw "Reg file format is not supported by settings .XML. Please use only $currentHive or $lastHive per XML file"
  476.                return
  477.             }
  478.             else
  479.             {
  480.                 # going up a key
  481.                 if ($currentKey.StartsWith($lastKey + "\"))
  482.                 {
  483.                     $newKey=$currentKey.Substring($lastKey.Length+1)
  484.                     ForEach ($key in $newKey.Split('\'))
  485.                     {
  486.                         $xml.WriteStartElement("Collection")
  487.                         $xml.WriteAttributeString("clsid",$clsidCollection)
  488.                         $xml.WriteAttributeString("name",$key)
  489.                         $collectionCount++
  490.                     }
  491.                 }
  492.                 else
  493.                 {
  494.                     # funky logic to handle change in key path
  495.                     # maybe this logic even works :)
  496.                     $currentKeySplit=$currentKey.Split('\')
  497.                     $lastKeySplit=$lastKey.Split('\')
  498.  
  499.                     $match=$true
  500.  
  501.                     $i=-1
  502.  
  503.                     while ($match)
  504.                     {
  505.                         $i++
  506.                         if ($i -ge $currentKeySplit.Length -or $i -ge $lastKeySplit.Length)
  507.                         {
  508.                             $match=$false
  509.                         }
  510.                         else
  511.                         {
  512.                             if ($currentKeySplit[$i] -ne $lastKeySplit[$i]) { $match=$false }
  513.                         }
  514.                     }
  515.  
  516.                     for ($x=$lastKeySplit.Length;$x -gt $i;$x--)
  517.                     {
  518.                         $xml.WriteEndElement()
  519.                         $collectionCount--
  520.                     }
  521.  
  522.                     for ($x=$i;$x -lt $currentKeySplit.Length;$x++)
  523.                     {
  524.                         $xml.WriteStartElement("Collection")
  525.                         $xml.WriteAttributeString("clsid",$clsidCollection)
  526.                         $xml.WriteAttributeString("name",$currentKeySplit[$x])
  527.                         $collectionCount++
  528.                     }
  529.                    
  530.                 }
  531.             }
  532.         }
  533.        
  534.         $lastHive=$currentHive
  535.         $lastKey=$currentKey
  536.     }
  537.     else
  538.     {
  539.         if ($line.Contains("="))
  540.         {
  541.             $regType=[Microsoft.Win32.RegistryValueKind]::Unknown
  542.  
  543.             # detect registry type
  544.             if ($line.StartsWith("@=") -or $line.Contains('"="')) { $regType=[Microsoft.Win32.RegistryValueKind]::String }
  545.             if ($line.Contains("=hex:")) { $regType=[Microsoft.Win32.RegistryValueKind]::Binary }
  546.             if ($line.Contains("=dword:")) { $regType=[Microsoft.Win32.RegistryValueKind]::DWord }
  547.             if ($line.Contains("=hex(7):")) { $regType=[Microsoft.Win32.RegistryValueKind]::MultiString }
  548.             if ($line.Contains("=hex(2):")) { $regType=[Microsoft.Win32.RegistryValueKind]::ExpandString }
  549.             if ($line.Contains("=hex(b):")) { $regType=[Microsoft.Win32.RegistryValueKind]::QWord }
  550.  
  551.             switch ($regType)
  552.             {
  553.                 # *** PROCESS REG_SZ
  554.                 ([Microsoft.Win32.RegistryValueKind]::String)
  555.                 {
  556.                     $default="0"
  557.                     if ($line.StartsWith("@="))
  558.                     {
  559.                         $valueName=""
  560.                         $value=$line.Substring(3,$line.Length-4)
  561.                         "Name = '$valueName' Value = '$value'"
  562.                         $default="1"
  563.                     }
  564.                     else
  565.                     {
  566.                         $i = $line.IndexOf('"="')
  567.                         $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  568.                         $value=Convert-RegEscapeCodes $line.Substring($i+3,$line.Length-$i-4)
  569.                        "Name = '$valueName' Value = '$value'"
  570.                  
  571.                     }
  572.  
  573.                     $xml.WriteStartElement("Registry")
  574.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  575.                     $xml.WriteAttributeString("name",$valueName)
  576.                     $xml.WriteAttributeString("descr",$descr)
  577.                     $xml.WriteAttributeString("image","7")
  578.                    
  579.                     $xml.WriteStartElement("Properties")
  580.                     $xml.WriteAttributeString("action",$action)
  581.                     $xml.WriteAttributeString("hive",$currentHive)
  582.                     $xml.WriteAttributeString("key",$currentKey)
  583.                     $xml.WriteattributeString("name",$valueName)
  584.                     $xml.WriteattributeString("default",$default)
  585.                     $xml.WriteattributeString("type","REG_SZ")
  586.                     $xml.WriteattributeString("displayDecimal","0")
  587.                     $xml.WriteAttributeString("value",$value)
  588.                     $xml.WriteEndElement()
  589.                     $xml.WriteEndElement()
  590.          }
  591.  
  592.                 # *** PROCESS REG_BINARY
  593.                 ([Microsoft.Win32.RegistryValueKind]::Binary)
  594.                 {
  595.                     # read binary key to end
  596.                     while ($line.EndsWith("\"))
  597.                     {
  598.                         $line=$line.Substring(0,$line.Length-1)+$sr.ReadLine().Trim()
  599.                     }
  600.  
  601.                     $i = $line.IndexOf('"=hex:')
  602.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  603.                     $value=$line.Substring($i+6).Replace(",","")
  604.                     "Name = '$valueName' Value = '$value'"
  605.  
  606.                     # build XML
  607.                     $xml.WriteStartElement("Registry")
  608.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  609.                     $xml.WriteAttributeString("name",$valueName)
  610.                     $xml.WriteAttributeString("descr",$descr)
  611.                     $xml.WriteAttributeString("image","17")
  612.                    
  613.                     $xml.WriteStartElement("Properties")
  614.                     $xml.WriteAttributeString("action",$action)
  615.                     $xml.WriteAttributeString("hive",$currentHive)
  616.                     $xml.WriteAttributeString("key",$currentKey)
  617.                     $xml.WriteattributeString("name",$valueName)
  618.                     $xml.WriteattributeString("default","0")
  619.                     $xml.WriteattributeString("type","REG_BINARY")
  620.                     $xml.WriteattributeString("displayDecimal","0")
  621.                     $xml.WriteAttributeString("value",$value)
  622.                     $xml.WriteEndElement()
  623.                     $xml.WriteEndElement()
  624.  
  625.                 }
  626.  
  627.                 # *** PROCESS REG_DWORD
  628.                 ([Microsoft.Win32.RegistryValueKind]::DWord)
  629.                 {
  630.                
  631.                     $i = $line.IndexOf('"=dword:')
  632.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  633.                     $value=$line.Substring($i+8).ToUpper()
  634.                      "Name = '$valueName' Value = '$value'"
  635.  
  636.                     # build XML
  637.                     $xml.WriteStartElement("Registry")
  638.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  639.                     $xml.WriteAttributeString("name",$valueName)
  640.                     $xml.WriteAttributeString("descr",$descr)
  641.                     $xml.WriteAttributeString("image","17")
  642.                    
  643.                     $xml.WriteStartElement("Properties")
  644.                     $xml.WriteAttributeString("action",$action)
  645.                     $xml.WriteAttributeString("hive",$currentHive)
  646.                     $xml.WriteAttributeString("key",$currentKey)
  647.                     $xml.WriteattributeString("name",$valueName)
  648.                     $xml.WriteattributeString("default","0")
  649.                     $xml.WriteattributeString("type","REG_DWORD")
  650.                     $xml.WriteattributeString("displayDecimal","0")
  651.                     $xml.WriteAttributeString("value",$value)
  652.                     $xml.WriteEndElement()
  653.                     $xml.WriteEndElement()
  654.                 }
  655.  
  656.                 # *** PROCESS REG_QWORD
  657.                 ([Microsoft.Win32.RegistryValueKind]::QWord)
  658.                 {
  659.                     $i = $line.IndexOf('"=hex(b):')
  660.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  661.                     $tempValue=$line.Substring($i+9).Replace(",","").ToUpper()
  662.                     $value=""
  663.  
  664.                     # unreverse QWORD for settings.xml format
  665.                     for ($i = $tempValue.Length -2;$i -gt 0;$i-=2)
  666.                     {
  667.                         $value+=$tempValue.Substring($i,2)
  668.                     }
  669.                    
  670.                      "Name = '$valueName' Value = '$value'"
  671.  
  672.                      # build XML
  673.                     $xml.WriteStartElement("Registry")
  674.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  675.                     $xml.WriteAttributeString("name",$valueName)
  676.                     $xml.WriteAttributeString("descr",$descr)
  677.                     $xml.WriteAttributeString("image","17")
  678.                    
  679.                     $xml.WriteStartElement("Properties")
  680.                     $xml.WriteAttributeString("action",$action)
  681.                     $xml.WriteAttributeString("hive",$currentHive)
  682.                     $xml.WriteAttributeString("key",$currentKey)
  683.                     $xml.WriteattributeString("name",$valueName)
  684.                     $xml.WriteattributeString("default","0")
  685.                     $xml.WriteattributeString("type","REG_QWORD")
  686.                     $xml.WriteattributeString("displayDecimal","0")
  687.                     $xml.WriteAttributeString("value",$value)
  688.                     $xml.WriteEndElement()
  689.                     $xml.WriteEndElement()
  690.                 }
  691.  
  692.                 # *** PROESS REG_MULTI_MZ
  693.                 ([Microsoft.Win32.RegistryValueKind]::MultiString)
  694.                 {
  695.                     # read binary key to end
  696.                     while ($line.EndsWith("\"))
  697.                     {
  698.                         $line=$line.Substring(0,$line.Length-1)+$sr.ReadLine().Trim()
  699.                     }
  700.  
  701.                     # read hex codes
  702.                     $i = $line.IndexOf('"=hex(7):')
  703.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  704.                     $value=$line.Substring($i+9).Replace(",","")
  705.                     # convert hex codes to binary array
  706.                     $byteLength=$value.Length/2
  707.                     $byte = New-Object Byte[] $byteLength
  708.                    
  709.                     $x=0
  710.                     for ($i=0;$i -lt $value.Length;$i+=2)
  711.                     {
  712.                         $byte[$x]="0x" + $value.Substring($i,2)
  713.                         $x++
  714.                     }
  715.  
  716.                     # convert binary array to unicode string
  717.                     $value=$unicoder.GetString($byte)
  718.  
  719.                     # retrieve multi values
  720.                     $values=$value.Replace("`0`0","").Split("`0")
  721.                    
  722.                     "Name = '$valueName'"
  723.  
  724.                      # build XML
  725.                     $xml.WriteStartElement("Registry")
  726.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  727.                     $xml.WriteAttributeString("name",$valueName)
  728.                     $xml.WriteAttributeString("descr",$descr)
  729.                     $xml.WriteAttributeString("image","7")
  730.                    
  731.                     $xml.WriteStartElement("Properties")
  732.                     $xml.WriteAttributeString("action",$action)
  733.                     $xml.WriteAttributeString("hive",$currentHive)
  734.                     $xml.WriteAttributeString("key",$currentKey)
  735.                     $xml.WriteattributeString("name",$valueName)
  736.                     $xml.WriteattributeString("default","0")
  737.                     $xml.WriteattributeString("type","REG_MULTI_SZ")
  738.                     $xml.WriteattributeString("displayDecimal","0")
  739.                     $xml.WriteAttributeString("value",$value.Replace("`0"," "))
  740.                    
  741.                     $x=1
  742.  
  743.                     $xml.WriteStartElement("Values")
  744.  
  745.                     ForEach ($value in $values)
  746.                     {
  747.                         $xml.WriteStartElement("Value")
  748.                         $xml.WriteString($value)
  749.                         "Value $x = '$value'"
  750.                         $xml.WriteEndElement()
  751.                     }
  752.                    
  753.                     $xml.WriteEndElement()
  754.                     $xml.WriteEndElement()
  755.                     $xml.WriteEndElement()
  756.                 }
  757.  
  758.                 ([Microsoft.Win32.RegistryValueKind]::ExpandString)
  759.                 {
  760.                     # read binary key to end
  761.                     while ($line.EndsWith("\"))
  762.                     {
  763.                         $line=$line.Substring(0,$line.Length-1)+$sr.ReadLine().Trim()
  764.                     }
  765.  
  766.                     # read hex codes
  767.                     $i = $line.IndexOf('"=hex(2):')
  768.                     $valueName=Convert-RegEscapeCodes $line.Substring(1,$i-1)
  769.                     $value=$line.Substring($i+9).Replace(",","")
  770.                     # convert hex codes to binary array
  771.                     $byteLength=$value.Length/2
  772.                     $byte = New-Object Byte[] $byteLength
  773.                    
  774.                     $x=0
  775.                     for ($i=0;$i -lt $value.Length;$i+=2)
  776.                     {
  777.                         $byte[$x]="0x" + $value.Substring($i,2)
  778.                         $x++
  779.                     }
  780.  
  781.                     # convert binary array to unicode string
  782.                     $value=$unicoder.GetString($byte).Replace("`0","")
  783.                     "Name = '$valueName' Value = '$value'"
  784.  
  785.                     $xml.WriteStartElement("Registry")
  786.                     $xml.WriteAttributeString("clsid",$clsidRegistry)
  787.                     $xml.WriteAttributeString("name",$valueName)
  788.                     $xml.WriteAttributeString("descr",$descr)
  789.                     $xml.WriteAttributeString("image","7")
  790.                    
  791.                     $xml.WriteStartElement("Properties")
  792.                     $xml.WriteAttributeString("action",$action)
  793.                     $xml.WriteAttributeString("hive",$currentHive)
  794.                     $xml.WriteAttributeString("key",$currentKey)
  795.                     $xml.WriteattributeString("name",$valueName)
  796.                     $xml.WriteattributeString("default",$default)
  797.                     $xml.WriteattributeString("type","REG_EXPAND_SZ")
  798.                     $xml.WriteattributeString("displayDecimal","0")
  799.                     $xml.WriteAttributeString("value",$value)
  800.                     $xml.WriteEndElement()
  801.                     $xml.WriteEndElement()
  802.                 }
  803.  
  804.             }
  805.  
  806.         }
  807.        
  808.     }
  809.  
  810.   }
  811.  
  812.   $sr.Close()
  813.   while ($collectionCount -gt 0)
  814.   {
  815.         $xml.WriteEndElement()
  816.         $collectionCount--
  817.     }
  818.  
  819.     $xml.Close()
  820.  
  821. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement