Last active
April 4, 2019 08:46
-
-
Save techdecline/5c2ad7abac4ad335886e26d042c5f0d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .Synopsis | |
| Imports all GPO exports from an input folder. | |
| .DESCRIPTION | |
| Imports all GPO exports from an input folder and allow for automatic linking to a given Organizational Unit. | |
| .EXAMPLE | |
| .\Import-ExportedPolicies.ps1 -GpoBackupFolder C:\Temp\GPO | |
| Imports all policies that where exported to C:\Temp | |
| .EXAMPLE | |
| .\Import-ExportedPolicies.ps1 -GpoBackupFolder C:\Temp\GPO -Prefix "MyTest-" -OrganizationUnit "OU=Workstations,DC=contoso,DC=com" | |
| Imports all policies that where exported to C:\Temp with Prefix "MyTest-". Additionally, all objects will be linked to "OU=Workstations,DC=contoso,DC=com" | |
| .COMPONENT | |
| Die Komponente, zu der dieses Cmdlet gehört | |
| #> | |
| function Import-ExportedPolicies { | |
| [CmdletBinding(SupportsShouldProcess)] | |
| param ( | |
| # This parameter contains the path to one or multiple GPO export folders | |
| [Parameter(Mandatory,ValueFromPipeline)] | |
| [ValidateScript({Test-Path $_})] | |
| [String]$GpoBackupFolder, | |
| # This parameter contains the domain name where to import the GPO exports | |
| [Parameter(Mandatory=$false)] | |
| [String]$DomainName = $env:USERDOMAIN, | |
| # This parameters specifies the Distinguished Name of the Organizational Unit the new objects will be linked | |
| [Parameter(Mandatory=$false)] | |
| [String]$OrganizationalUnit, | |
| # This parameter contains the prefix that will be used for the new GPO names | |
| [Parameter(Mandatory=$false)] | |
| [String]$GpoPrefix | |
| ) | |
| begin { | |
| function Get-GpoNameFromReport { | |
| param ( | |
| [Parameter(Mandatory)] | |
| [String]$GpoReportPath | |
| ) | |
| $reportXml = $GpoReportPath | |
| <#$xmlDoc = [System.Xml.XmlDocument]::new() | |
| $xmlDoc.Load($reportXml) | |
| #> | |
| [xml]$xmlDoc = Get-Content $GpoReportPath | |
| $gpoName = $xmlDoc.GPO.Name | |
| return $gpoName | |
| } | |
| } | |
| process { | |
| $gpoArr = Get-ChildItem $GpoBackupFolder -Directory | |
| foreach ($gpoExport in $gpoArr) { | |
| $reportXmlFile = Get-ChildItem -Path $gpoExport.FullName -Filter "gpreport.xml" | |
| if (-not ($GpoPrefix)) { | |
| $gpoName = (Get-GpoNameFromReport -GpoReportPath $reportXmlFile.FullName) | |
| } | |
| else { | |
| $gpoName = "$GpoPrefix" + (Get-GpoNameFromReport -GpoReportPath $reportXmlFile.FullName) | |
| } | |
| if ($PSCmdlet.ShouldProcess("Importing Group Policy Object $gpoName")) { | |
| $newGpoObject = New-GPO -Name $gpoName -Domain $domainName -Comment "Imported from $GpoBackupFolder on $(Get-Date -Format yyyyMMdd) by $env:USERNAME" | |
| Import-GPO -Path $GpoBackupFolder -TargetName $gpoName -Domain $DomainName -BackupId $gpoExport.Name | |
| } | |
| if ($OrganizationalUnit) { | |
| if ($PSCmdlet.ShouldProcess("Linking Group Policy Object $gpoName to $OrganizationalUnit")) { | |
| New-GPLink -Guid $newGpoObject.id -Target $OrganizationalUnit -LinkEnabled Yes -Domain $DomainName | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment