########################### # vorbereitende Maßnahmen ########################### # Den Domänennamen abfragen und als Distinguished Name speichern $DomainDN = (Get-ADDomain).DistinguishedName $AusgabePfad = 'C:\temp\gpoDemo' If ( -not ( test-path -Path $AusgabePfad )) { mkdir -Path $AusgabePfad } ################# # GPO-Reports ################# # Alle GPOs abfragen und in einer CSV-Datei speichern Get-Gpo -All | Export-Csv -Path "$AusgabePfad\AllePolicies.csv" -Delimiter ';' # Als HTML-Report speichern Get-GPOReport -Name 'Default Domain Policy' -path "$AusgabePfad\DefaultDomainPolicy.hta" -ReportType Html # Für jede GPO einen eigenen Report erstellen Get-Gpo -All | ForEach-Object { $path = Join-Path -Path $AusgabePfad -ChildPath ( $_.DisplayName + '.hta' ) Get-GPOReport -Name $_.DisplayName -Path $path -ReportType Html } ############### # GPO Backup ############### # Gruppenrichtlinien sichern mit Dokumentation $TimeStamp = Get-Date -Format ddMMyyyy_HHmmss If ( -not ( test-path -Path "$AusgabePfad\gpobackup" )) { $GPOBackupPath= mkdir -Path "$AusgabePfad\gpobackup" } Else { $GPOBackupPath = "$AusgabePfad\gpobackup" } # Alle Gruppenrichtlinien sichern und in einer CSV-Datei Dokumentieren Backup-GPO -All -Path $GPOBackupPath -Comment 'Automatisch generiertes Backup' | Export-CSV -Path $GPOBackupPath\$TimeStamp.csv -Delimiter ';' # SicherungsDoku anzeigen Get-ChildItem -path $GPOBackupPath\*.csv | Select-Object -ExpandProperty FullName | import-csv -Delimiter ';' | Out-GridView # Sicherungen wiederherstellen Get-ChildItem -path $GPOBackupPath\*.csv | Select-Object -ExpandProperty FullName | import-csv -Delimiter ';' | Out-GridView -PassThru | Restore-GPO -Confirm # Sichern aller Richtlinien, die sich seit x Tagen nicht geändert haben $now = Get-Date Get-GPO -All | Where-Object { $_.ModificationTime -gt $now.AddDays(-1) } | Backup-GPO -Path $GPOBackupPath -Comment 'Automatisch generiertes Backup' | Export-CSV -Path $GPOBackupPath\$TimeStamp.csv -Delimiter ';' ###################### # Health-Check ##################### # Alle GPOs anzeigen, die sich seit $NotModifiedSinceDays nicht geändert haben $now = Get-Date $NotModifiedSinceDays = 365 Get-GPO -All | Where-Object { $_.ModificationTime -lt $now.AddDays( -$NotModifiedSinceDays )} # Leere GPOs anhand eines XML-Reports finden - Dies ist ein einfaches Beispiel. Für die Praxis nutzen sie die Funktion Get-EmptyGPO # aus dem zugehörigen Powershell-Modul [xml]$report = Get-GPOReport -Name 'MyEmptyGpo' -ReportType XML if (( -not $report.gpo.user.extensiondata ) -and ( -not $report.gpo.computer.extensiondata )) {'GPO is empty'} # nicht verknüfte Policies finden - Dieses Snippet nutzt reguläre Ausdrücke, um die Group-Policy Links zu identifzieren ( Get-ADDomain ).LinkedGroupPolicyObjects [regex]$GUID = '[A-Fa-f\d]{8}-[A-Fa-f\d]{4}-[A-Fa-f\d]{4}-[A-Fa-f\d]{4}-[A-Fa-f\d]{12}' [array]$Policies = $guid.matches(( Get-ADDomain ).LinkedGroupPolicyObjects).Value $Policies += $guid.matches(( Get-ADOrganizationalUnit -filter * ).LinkedGroupPolicyObjects).Value Get-GPO -all | Where-Object { $_.id -notin $Policies } ############ # Neue Gruppenrichtlinie anlegen ############ # Dieses Beispiel legt ein neues GPO mit dem Namen "PSH Demo IE keine Hilfe" mit einer Registry-Konfiguration an, setzt die Berechtigungen # und verknüpft das GPO. Security-Settings und Auditing-Settings können über die Gruppenrichtlinien-Cmdlets nicht direkt erzeugt werden. new-gpo -Name 'PSH Demo IE keine Hilfe' | set-gpregistryvalue -key 'HKLM\Software\Policies\Microsoft\INternet Explorer\Restrictions' -Valuename 'NoHelpMenu' -Type DWord -Value 1 | Set-GPPermission -Replace -Permissionlevel None -Targetname 'Authenticated Users' -Targettype group | Set-GPPermission -Permissionlevel gpoapply -TargetName 'Userhelpdesk' -Targettype group | Set-GPPermission -Permissionlevel GpoRead -TargetName 'Domänencomputer' -Targettype group | new-GPLink -Target "OU=Hannover,$DomainDN" -Order 1 # Es können auch Group Policy Preferences-Einstellungen in einem GPO per Powershell gesetzt werden, wenn es sich um eine Registry-basierte # Einstellung handelt. Set-GPPrefRegistryValue -Name 'PSH Demo IE keine Hilfe' -Context User -Action Create -Key 'HKEY_Current_User\Software\Microsoft\Office\16.0\Common\General' -ValueName shownfirstrunoptin -Value 1 -Type DWord Set-GPPrefRegistryValue -Name "'PSH Demo IE keine Hilfe" -Context User -Action Create -Key 'HKEY_Current_User\Software\Microsoft\Office\16.0\Common\General' -ValueName ShownFileFmtPrompt -Value 1 -Type DWord ############## # Registry-Werte aus GPOs auslesen ############## # Einen Registry-Key in einer GPO finden $regKey = 'HKLM\Software\Policies\Microsoft\Internet Explorer\Restrictions' $regValueName = 'NoHelpMenu' Get-GPO -all | ForEach-Object { $Setting = Get-GPRegistryValue -name $_.DisplayName -Key $regKey -ValueName $regValueName -ErrorAction SilentlyContinue IF ( $Setting ) { Add-Member -MemberType NoteProperty -Name gponame -InputObject $setting -Value $_.Displayname $setting | Select-Object -Property * } } # Den Registry-Wert über alle GPOs der Domäne suchen und das Ergebnis als Gridview ausgeben Get-GPO -all | ForEach-Object { $Setting = Get-GPRegistryValue -name $_.DisplayName -Key $regKey -ValueName ` $regValueName -ErrorAction SilentlyContinue IF ( $Setting ) { $_.Displayname $setting } } | Out-GridView ######################### # Berechtigungen setzen ######################### # Mit diesem Snippet werden auf allen GPOs die Berechtigungen geprüft. Haben die Domain-Computers keine Lese-Berechtigungen, werden Sie hinzugefügt $allGPOs = get-gpo -all foreach ($gpo in $allGPOs) { #first check to see if GPO has per-user settings, since this fix really only needs to apply to per user GPOs. Remove this check if you really want to modify all GPOs if ($gpo.user.DSVersion -gt 0) { # first read the GPO permissions to find out if Authn Users and Domain Computers is missing. Note--depending upon the version of Windows/GPMC you are on--Get-GPPermission might be Get-GPPermissionS $perm1 = Get-GPPermission -Guid $gpo.id -TargetName 'Authenticated Users' -TargetType group -ErrorAction SilentlyContinue $perm2 = Get-GPPermission -Guid $gpo.id -TargetName 'Domain Computers' -TargetType group -ErrorAction SilentlyContinue if ($perm1 -eq $null -and $perm2 -eq $null) # if no authn users or domain computers is found, then add Authn Users read perm { Set-GPPermission -Guid $gpo.Id -PermissionLevel GpoRead -TargetName 'Authenticated Users' -TargetType Group '{0} has been modified to grant Authenticated Users read access' -f $gpo.DisplayName } } } ############### # WMI-Filter ############### # WMI-Filter abfragen # Einen einzelnen WMI-Filter und seine WQL-Definition abfragen (Get-GPO -Name 'Lockdown XP').WmiFilter (Get-GPO -Name 'OnlyVM').WmiFilter.GetQueryList() # Alle GPOs anzeigen lassen, auf denen WMI-Filter definiert sind. Get-GPO -All | Select-Object -ExpandProperty WMIFilter | Select-Object -Property Name,Description Get-GPO -All | Where-Object { $_.WMIFilter } | ForEach-Object { $Filter = New-Object -TypeName PsObject -Property @{ Displayname = $_.DisplayName WMIFilter = ($_.wmiFilter).name } $Filter } Get-ADObject -Filter 'objectClass -eq "msWMI-Som"' -Properties msWMI-Name,msWMI-Parm2,Created,Modified,msWMI-Author | Select-Object -Property msWMI-Name,msWMI-Parm2,Created,Modified,msWMI-Author,ObjectGUID | Out-GridView # WMI-Filter sichern Get-ADObject -Filter 'objectClass -eq "msWMI-Som"' -Properties 'msWMI-Name','msWMI-Parm1','msWMI-Parm2' | Select-Object -Property DistinguishedName, msWMI-Name, msWMI-Parm1, msWMI-Parm2, Name | Export-CSV -NoTypeInformation -Encoding UTF8 -Path "$AusgabePfad\WMIFilter.csv" ####################### # Kennwortrichtlinien ####################### # AD Kennwortrichtlinien abfragen Get-ADDefaultDomainPasswordPolicy Get-ADFineGrainedPasswordPolicy -Filter * # Das maximale Kennwortalter abfragen (Get-ADDefaultDomainPasswordPolicy | Select-Object -ExpandProperty MaxPasswordAge).TotalDays