Automatisches Importieren von adml und admx Templates via Powershell script

The estimated reading time 8 minutes

Ich setzte mir immer wieder neue Testdomänen auf um verschiedene Szenarien zu testen. Hierbei habe ich natürlich dann das Problem, dass ich immer wieder GPO-Templates für verschiedene OS/ Programme einspielen muss. Um dies etwas zu automatisieren, habe ich ein Powershell Script geschrieben, das die Template-Daten direkt in den zentralen GPO-Store kopiert bzw. diesen dann auch automatisch anlegt, falls er nicht vorhanden ist.

Das Skript kann auch mehrmals gestartet werden, es ergänzt dann die Änderungen in den ADMX / ADML Files. Ansonsten wird nichts verändert. Es wird jedesmal ein neues Logfile generiert.

Ich nutze dieses Skript nur in einer Testumgebung!

WICHTIG: Standardmäßig liegen die ADMX Templates auf jedem DC  lokal unter

„C:\Windows\PolicyDefinitions“. Bitte vor der Benutzung des Skriptes klären ob es bereits einen zentralen Store gibt oder nicht, welche Templates überhaupt benötigt werden und welche nicht.

Siehe hierzu auch meinen Artikel

Dies ist oft eine gute Gelegenheit etwas aufzuräumen. (Sollte es nicht zentral eingerichtet worden sein, muss der Ordner „C:\Windows\PolicyDefinitions“ jedes DCs verglichen werden, sodass nachher keine GPO-Templates fehlen.)

Nun aber genug der Floskeln, Taten warten!

Zu Beginn das Script:

<#

######################################## blog.it-koehler.com ####################################
Version 0.13.03.2016 
Created by A. Koehler 

This script imports the admx and adml templates (only german and us) and generates a central gpo store. ADMX files and ADML files have to be next to the script (same folder).
Folders with adml files have to be named "de-DE" or "en-US" and also in the same path as the script

Script tested on Windows Server 2012R2

#>

Write-Host "######################################## blog.it-koehler.com #################################### " -ForegroundColor Gray
Write-Host "Version 0.13.03.2016 " -ForegroundColor Gray
Write-Host "Created by A. Koehler " -ForegroundColor Gray

#check if ActiveDirectory Module is loaded, if not load without asking
    $ADModule='ActiveDirectory'
    if (Get-Module -Name $ADModule) {
        write-host 'Module' $ADModule 'already loaded' -ForegroundColor "green"
    }
    else {
        Import-Module $ADModule -force
        write-host 'Module' $ADModule 'loaded successfully' -ForegroundColor "green"
    }

# variable declaration 
    $rootdnsname = (Get-ADDomain).DNSRoot
    $date = Get-Date -format "yyyyMMdd-HH-mm-ss"
    $DateStr = '{0:yyyyMMdd}' -f $Date
    $admxpath = ((Get-Location).Path)
    $sysvol = "\\" + $rootdnsname + "\SYSVOL"
    $netlogonpolicies = $sysvol + "\" +$rootdnsname + "\Policies"
    $netlogonpolicydef = $netlogonpolicies + "\PolicyDefinitions"
    
    #if you want to import other languages you can replace de-de or just delete "| Where-Object{ $_.Name -eq "de-DE" -or $_.Name -eq "en-US" })" then it will copy all folders in the folderpath
    $admfolders = @(Get-ChildItem -Path $admxpath | ?{ $_.PSIsContainer }  | Where-Object{ $_.Name -eq "de-DE" -or $_.Name -eq "en-US" })
    

    #Test if the Policy Definition folder already exists in the sysvol directory
    if(Test-Path -Path $netlogonpolicydef){
     #copy all admx Files 
     Write-Output "Copy admx files to $netlogonpolicydef " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
     robocopy $admxpath $netlogonpolicydef *.admx /s /np /ndl /R:2 /W:3 | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
     Write-Host "Copy admx-files to $netlogonpolicydef " -ForegroundColor "yellow" 
    
            #copy en-us and de-de folder to sysvol
            ForEach ($folders in $admfolders)
            {
                #check if en-us or de-de exists
                if(Test-Path -Path $netlogonpolicydef\$folders){
                #copy adml files to the sysvol directory 
                Write-Output "Copy adml files to $folders " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
                robocopy $admxpath\$folders $netlogonpolicydef\$folders *.adml /s /np /ndl /R:2 /W:3 | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
                Write-Host "Copy adml files to $folders " -ForegroundColor "yellow" 
                      
                }
                else 
                {
                #creat de-de and en-us folders
                New-Item -ItemType directory -Path "$netlogonpolicydef\$folders"
                Write-Host "Creating $folders in sysvol " -ForegroundColor "yellow"
                #copy adml files to the sysvol directory
                Write-Output "Copy adml files to $folders " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
                robocopy $admxpath\$folders $netlogonpolicydef\$folders *.adml /s /np /ndl /R:2 /W:3 | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
                Write-Host "Copy adml files to $folders " -ForegroundColor "yellow" 
                
                }    
    
            }
            #Display information in the powershell console and log file
            Write-Host "Adml files copied to de-DE and en-US folders in $netlogonpolicydef " -ForegroundColor "green"
            Write-Output "Templates will be stored in $netlogonpolicydef " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
            Write-Output "Templates are successfully stored in $netlogonpolicydef " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
            Write-Host "Templates are successfully stored in $netlogonpolicydef " -ForegroundColor Green
            Write-Host ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' )
            Invoke-Item ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' )
            pause
            }
    
    else 
    {
    Write-Host "$netlogonpolicydef does not exist it will be created " -ForegroundColor "yellow"
    #Creation of the "PolicyDefinitions" Folder in the sysvol directory 
    New-Item -ItemType directory -Path "$netlogonpolicydef"
    Write-Host "$netlogonpolicydef successfully created " -ForegroundColor Green
    Write-Output "$netlogonpolicydef successfully created" | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200 
    #copy all admx Files 
     Write-Output "Copy admx files to $netlogonpolicydef " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
     robocopy $admxpath $netlogonpolicydef *.admx /s /np /ndl /R:2 /W:3 | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
     Write-Host "Copy admx-files to $netlogonpolicydef " -ForegroundColor "yellow" 
    
                #copy en-us and de-de folder to sysvol
                ForEach ($folders in $admfolders)
                {
                    #check if en-us or de-de exists
                    if(Test-Path -Path $netlogonpolicydef\$folders){
                    #copy adml files to the sysvol directory 
                    Write-Output "Copy adml files to $folders " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
                    robocopy $admxpath\$folders $netlogonpolicydef\$folders *.adml /s /np /ndl /R:2 /W:3 | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
                    Write-Host "Copy adml files to $folders " -ForegroundColor "yellow" 
                          
                    }
                    else 
                    {
                    #creat de-de and en-us folders
                    New-Item -ItemType directory -Path "$netlogonpolicydef\$folders"
                    Write-Host "Creating $folders in sysvol " -ForegroundColor "yellow"
                    #copy adml files to the sysvol directory
                    Write-Output "Copy adml files to $folders " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
                    robocopy $admxpath\$folders $netlogonpolicydef\$folders *.adml /s /np /ndl /R:2 /W:3 | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
                    Write-Host "Copy adml files to $folders " -ForegroundColor "yellow" 
                    
                    } 
             }
            #Display information in the powershell console and log file
            Write-Host "Adml files copied to de-DE and en-US folders in $netlogonpolicydef " -ForegroundColor "green"
            Write-Output "Templates will be stored in $netlogonpolicydef " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
            Write-Output "Templates are successfully stored in $netlogonpolicydef " | Out-File -Append ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' ) -Width 200
            Write-Host "Templates are successfully stored in $netlogonpolicydef " -ForegroundColor Green 
            Write-Host ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' )
            Invoke-Item ($admxpath + '\' + $DateStr + '-import-win10-admx-logs.txt' )
            pause 
    }

 

Nun zur Handhabung des Skriptes:

ZUERST

Erstellt ein leeres TXT Dokument auf einem DC /Rechner mit RSAT Tools. In dieses leere Dokument kopiert ihr das komplette Skript und speichert dieses als .PS1 ab.

Bevor das Script ausgeführt werden kann, muss die Execution Policy gelockert werden, dazu die normale Powershell „Als Administrator“ öffnen und dann folgenden Befehl eingeben

Set-ExecutionPolicy Unrestricted -Force

PS-ImportADMX01

 

Danach kann man in den Ordner wechseln, wo ihr das PS1 Skript abgelegt habt.

In diesen Pfad müssen nun alle gewünschten ADMX Dateien, außerdem auch die Ordner de-DE und en-US mit den identischen ADML Dateien.

Siehe dazu:

PS-ImportADMX02

 

Das Skript liest den Pfad aus, in dem es liegt und sucht dann nach ADMX + ADML Dateien

und kopiert diese dann in den \\domaene.com\sysvol\domaene.com\Policies

Wenn alle ADMX /ADML Dateien (innerhalb der en-US/de-DE) liegen, kann das Skript ausgeführt werden.

HINWEIS: das Skript legt eine TXT Datei im selben Ordner Pfad ab, in dieser lassen sich die Kopiervorgänge nachvollziehen! Außerdem wird dieses Skript

PS-ImportADMX04

Nun öffnet sich die Powershell und führt das Script aus

PS-ImportADMX05

Die Konsole kann dann mit dem Drücken einer beliebigen Taste beendet werden, zusätzlich öffnet sich das LogFile automatisch.

PS-ImportADMX06

Nun kann noch der zentrale GPO Speicher kontrolliert werden ob die Dateien darin abgelegt sind

Vorher:

PS-ImportADMX03

Nachher:

PS-ImportADMX07

Viel Spaß beim Testen, ich freue mich über Anmerkungen, Verbesserungsvorschläge!

Print Friendly, PDF & Email
Was this article helpful?
YesNo
0 0 votes
Article Rating
Abonnieren
Benachrichtige mich bei
guest
0 Comments
Inline Feedbacks
View all comments