Advertisement
pedronrivera

Copy GPO Registry Settings

Mar 14th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #--------------------------------------------------------------------            
  2. # Copy GPO Registry Settings            
  3. # Ashley McGlone, Microsoft PFE            
  4. # http://blogs.technet.com/b/ashleymcglone            
  5. # January 2011            
  6. #            
  7. # Parameters:            
  8. #   dom       FQDN of the domain where the GPOs reside            
  9. #   src       string name of the GPO to copy settings from            
  10. #   dest      string name of the GPO to copy settings to            
  11. #   newDest   switch to create dest GPO if it does not exist            
  12. #   copymode  part of GPO to copy: all, user, computer            
  13. #--------------------------------------------------------------------            
  14.            
  15. Param (            
  16.     $dom,            
  17.     $src,            
  18.     $dest,            
  19.     [switch]$newDest,            
  20.     $copymode            
  21. )            
  22.            
  23. # We must continue on errors due to the way we enumerate GPO registry            
  24. # paths and values in the function CopyValues.            
  25. $ErrorActionPreference = "SilentlyContinue"            
  26. $error.PSBase.Clear()            
  27.            
  28. Import-Module ActiveDirectory            
  29. Import-Module GroupPolicy            
  30.            
  31. #--------------------------------------------------------------------            
  32. # Help            
  33. #--------------------------------------------------------------------            
  34. if ($dom -eq $null -and `
  35.     $src -eq $null -and `
  36.     $dest -eq $null -and `
  37.     $copymode -eq $null) {            
  38.     ""            
  39.     "Copy-GPORegistryValue by Ashley McGlone, Microsoft PFE"            
  40.     "For more info: http://blogs.technet.com/b/ashleymcglone"            
  41.     ""            
  42.     "This script copies registry-based GPO settings from one GPO into another."            
  43.     "Use this script to copy and/or merge policy settings."            
  44.     "NOTE: This version does not copy GPO preferences."            
  45.     ""            
  46.     "Syntax:"            
  47.     ".\Copy-GPRegistryValue.ps1 [-dom DomainFQDN] -src `"Source GPO`""            
  48.     "   -dest `"Destination GPO`" [-newDest]"            
  49.     "   [-copymode all/user/computer]"            
  50.     ""            
  51.     "The -dom switch will default to the current domain if blank."            
  52.     "The -copymode will default to all if blank."            
  53.     "The -newDest switch will create a new destination GPO of the specified"            
  54.     "name. If the GPO already exists, then the copy will proceed."            
  55.     ""            
  56.     Return            
  57. }            
  58.            
  59. #--------------------------------------------------------------------            
  60. # Validate parameters            
  61. #--------------------------------------------------------------------            
  62. if ($dom -eq $null) {            
  63.     $dom = (Get-ADDomain).DNSRoot            
  64. } else {            
  65.     $dom = (Get-ADDomain -Identity $dom).DNSRoot            
  66.     If ($error.Count -ne 0) {            
  67.         "Domain name does not exist.  Please specify a valid domain FQDN."            
  68.         $error            
  69.         Return            
  70.     }            
  71. }            
  72.            
  73. if ($src -eq $null) {            
  74.     "Source GPO name cannot be blank."            
  75.     Return            
  76. } else {            
  77.     $src = Get-GPO -Name $src            
  78.     If ($error.Count -ne 0) {            
  79.         "Source GPO does not exist.  Be sure to use quotes around the name."            
  80.         Return            
  81.     }            
  82. }            
  83.            
  84. if ($dest -eq $null) {            
  85.     "Destination GPO name cannot be blank."            
  86.     Return            
  87. } else {            
  88.     if ($newDest -eq $true) {            
  89.         $desttemp = $dest            
  90.         $dest = New-GPO -Name $desttemp            
  91.         If ($error.Count -ne 0) {            
  92.             "The new destination GPO already exists."            
  93.             "Do you want to merge into this GPO (y/n)?"            
  94.             $choice = Read-Host            
  95.             if ($choice -eq "y") {            
  96.                 $dest = Get-GPO -Name $desttemp            
  97.             } else {            
  98.                 Return            
  99.             }            
  100.         }            
  101.     } else {            
  102.         $dest = Get-GPO -Name $dest            
  103.         If ($error.Count -ne 0) {            
  104.             "Destination GPO does not exist.  Be sure to use quotes around the name."            
  105.             Return            
  106.         }            
  107.     }            
  108. }            
  109.            
  110. if ($copymode -eq $null) {            
  111.     $copymode = "all"            
  112. } else {            
  113.     if ($copymode -ne "all" -and `
  114.         $copymode -ne "user" -and `
  115.         $copymode -ne "computer") {            
  116.         "copymode must be one of the following values:"            
  117.         "all, user, computer"            
  118.         Return            
  119.     }            
  120. }            
  121. #--------------------------------------------------------------------            
  122.            
  123.            
  124. #--------------------------------------------------------------------            
  125. # Echo parameters for this run            
  126. #--------------------------------------------------------------------            
  127. ""            
  128. "Domain: $dom"            
  129. "Source GPO: $($src.DisplayName)"            
  130. "Destination GPO: $($dest.DisplayName)"            
  131. "New Destination: $newDest"            
  132. "CopyMode: $copymode"            
  133. ""            
  134. #--------------------------------------------------------------------            
  135.            
  136.            
  137. #--------------------------------------------------------------------            
  138. # Copy GPO registry values recursively beginning at a specified root.            
  139. #--------------------------------------------------------------------            
  140. # THIS IS THE HEART OF THE SCRIPT.            
  141. # Essentially this routine does a get from the source and a set on            
  142. # the destination.  Of course nothing is ever that simple, so we have            
  143. # to account for the policystate "delete" which disables a setting;            
  144. # this is like a "negative set".            
  145. # We recurse down each registry path until we find a value to            
  146. # get/set.            
  147. # If we try to get a value from a path (non-leaf level), then we get            
  148. # an error and continue to dig down the path.  If we get a value and            
  149. # no error, then we do the set.            
  150. # User values have a single root: HKCU\Software.            
  151. # Computer values have two roots: HKLM\System & HKLM\Software.            
  152. # You can find these roots yourself by analyzing ADM and ADMX files.            
  153. # It is normal to see an error in the output, because all of these            
  154. # roots are not used in all policies.            
  155. #--------------------------------------------------------------------            
  156. Function CopyValues ($Key) {            
  157.     $Key            
  158.     $error.PSBase.Clear()            
  159.     $path = Get-GPRegistryValue -GUID $src.ID -Key $Key            
  160.     $path            
  161.     If ($error.Count -eq 0) {            
  162.         ForEach ($keypath in $path) {            
  163.             $keypath            
  164.             $keypath | ForEach-Object {Write-Host $_}            
  165.             If ($keypath.HasValue) {            
  166.                 $keypath.PolicyState            
  167.                 $keypath.Valuename            
  168.                 $keypath.Type            
  169.                 $keypath.Value            
  170.                 If ($keypath.PolicyState -eq "Delete") {   # PolicyState = "Delete"            
  171.                     Set-GPRegistryValue -Disable -Domain $dom -GUID $dest.ID `
  172.                       -Key $keypath.FullKeyPath -ValueName $keypath.Valuename            
  173.                 } Else {   # PolicyState = "Set"            
  174.                     $keypath | Set-GPRegistryValue -Domain $dom -GUID $dest.ID            
  175.                 }            
  176.             } Else {            
  177.                 CopyValues $keypath.FullKeyPath            
  178.             }            
  179.         }            
  180.     } Else {            
  181.         $error            
  182.     }            
  183. }            
  184. #--------------------------------------------------------------------            
  185.            
  186.            
  187. #--------------------------------------------------------------------            
  188. # Call the main copy routine for the specified scope of $copymode            
  189. #--------------------------------------------------------------------            
  190. Function Copy-GPRegistryValue {            
  191.            
  192.     # Copy user settings            
  193.     If (($copymode -eq "user") -or ($copymode -eq "all")) {            
  194.         CopyValues "HKCU\Software"            
  195.         }            
  196.            
  197.     # Copy computer settings            
  198.     If (($copymode -eq "computer") -or ($copymode -eq "all")) {            
  199.         CopyValues "HKLM\System"            
  200.         CopyValues "HKLM\Software"            
  201.         }            
  202. }            
  203. #--------------------------------------------------------------------            
  204.            
  205. # Start the copy            
  206. Copy-GPRegistryValue            
  207.            
  208. #   ><>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement