PowerShellを使用してレポートを作成し、グループポリシーを分析する

管理にPowerShellを使用するトピックは非常に関連性が高く、このトピックに関する記事はHabréにますます掲載されています。 先週金曜日に公開したジェフリーヒックスによる記事の以前の翻訳は、興味を引くものでした。 そして、TechEd North America 2012で同じ著者の素晴らしいパフォーマンスを思い出せないのか。JeffreyHicksがJeremy Moskowitzと共に行ったレポートは、グループポリシーオブジェクトの分析とレポートの生成に当てられました。 元の資料(ビデオ)はここにありますが、コンテンツとスクリプトを簡単に説明します。 いずれにせよ、ビデオ自体を見ることをお勧めします。



レポートは2つの問題に焦点を当てました。

  1. グループポリシーレポートを作成します
  2. グループポリシー分析


詳細-カットの下。





PowerShellをグループポリシーと組み合わせて使用​​します。これには何が必要ですか?



グループポリシーを操作するときにPowerShellを使用するには、次のものが必要です。





アクションのシーケンス:







グループポリシーレポートを作成する



問題番号1。 グループポリシーで現在何が起こっているかについての情報を取得する必要があります





PS C:\> Import-Module GroupPolicy PS C:\> Get-GPO JeremyGPO
      
      





この場合、JeremyGPOの代わりにGPOの表示名(DisplayName)が立っています。 このプレートが表示されます



 DisplayName : JeremyGPO DomainName : GLOBOMANTICS.local Owner : GLOBOMANTICS\Domain Admins Id : cd73c562-5bfe-40e2-b81e-28da10da425c GpoStatus : ComputerSettingsDisabled Description : CreationTime : 12/28/2011 2:52:37 PM ModificationTime : 5/21/2012 11:08:26 AM UserVersion : AD Version: 4, SysVol Version: 4 ComputerVersion : AD Version: 1, SysVol Version: 1 WmiFilter :
      
      







解決策:レポートを作成します。



たとえば、過去30日間に変更されたすべてのグループオブジェクトのリストを、3つの値(表示名、変更時刻、説明)で降順(最後から最後)に並べ替えて取得するタスクに直面しています。 受信した情報は、.csvファイル(この例ではGPOModReport.csv)にエクスポートする必要があります。 PowerShellでの表示:



 PS C:\> get-gpo -all | Where {$_.ModificationTime -gt (Get-Date).AddDays(-30)} ... | Sort ModificationTime -Descending | Where {$_.ModificationTime -ge (Get-Date).AddDays(-30)} | Select Displayname,ModificationTime,Description ... | Export-CSV R:\GPOModReport.csv
      
      







追加コマンドの例
 #      PS C:\>get-gpo -all | Sort CreationTime,Modificationtime | Select Displayname,*Time
      
      







 #    ,    30  PS C:\>get-gpo -all | Where {$_.ModificationTime -ge (Get-Date).AddDays(-30)}
      
      







 #       (Defaul Domain Policy) PS C:\>Get-GPOReport -name "Default Domain Policy" -ReportType HTML -Path "c:\work\ddp.htm" invoke-item "c:\work\ddp.htm"
      
      







 #       PS C:\>Get-GPOReport -All -ReportType HTML -Path "c:\work\allgpo.htm" invoke-item "c:\work\allgpo.htm"
      
      







 #        #   _   GPO PS C:\>Get-GPO -all | foreach { $f="{0}.htm" -f ($_.Displayname).Replace(" ","_") $htmfile=Join-Path -Path "C:\work" -ChildPath $f Get-GPOReport -Name $_.Displayname -ReportType HTML -Path $htmfile Get-Item $htmfile }
      
      









問題番号2。 使用されていないGPOが多すぎます。





タスク:空のGPOを見つける









 PS C:\> Import-Module GroupPolicy PS C:\> [xml]$r = Get-GPOReport -Name MyGPO -ReportType XML PS C:\> if ((-Not $r.gpo.user.extensiondata) -AND (-not $r.gpo.computer.extensiondata)) { "GPO is empty" }
      
      







追加のチーム
 #requires -version 2.0 #find empty gpos in the domain Function Get-EmptyGPO { Param ( [Parameter(Position=0,ValueFromPipeline=$True, ValueFromPipelinebyPropertyName=$True)] [string]$DisplayName ) Begin { #import the GroupPolicy Module Import-Module GroupPolicy } Process { #create an XML report [xml]$report=Get-GPOReport -Name $displayname -ReportType XML #totally empty if ((-Not $report.gpo.user.extensiondata) -AND (-not $report.gpo.computer.extensiondata)) { #no extension data so write Get-GPO -Name $Displayname } } #process End {} } #function Function Test-EmptyGPO { Param ( [Parameter(Position=0,ValueFromPipeline=$True, ValueFromPipelinebyPropertyName=$True)] [string]$DisplayName ) Begin { #import the GroupPolicy Module Import-Module GroupPolicy } Process { #set default values $User=$False $Computer=$False #create an XML report [xml]$report=Get-GPOReport -Name $displayname -ReportType XML if ($report.gpo.user.extensiondata) { $User=$True } If ( $report.gpo.computer.extensiondata) { $Computer=$True } #write a custom object to the pipeline New-Object -TypeName PSObject -Property @{ Displayname=$report.gpo.name UserData=$User ComputerData=$Computer } } #Process End {} } #function #Get-GPO -All | Get-EmptyGPO #Get-GPO -All | Test-EmptyGPO
      
      









問題番号3。

GPOにアクセスしたのは誰ですか?

ポリシーの半分が無効化されているGPOがあります(「ポリシーの「半分」が無効になっているGPOはありますか?」)

すべてのポリシーが非アクティブ化されているGPOはありますか(「すべてのポリシーが無効になっているGPOはありますか?」)



GPOの状態(GPOStatus)でフィルターを適用します。 上記の3つの質問はそれぞれ、3つのチームに対応しています。



 PS C:\> get-gpo -all | Sort GPOStatus | format-table -GroupBy GPOStatus Displayname,*Time PS C:\> get-gpo -all | where {$_.GPOStatus -match "disabled"} | Select GPOStatus,Displayname PS C:\> get-gpo -all | where {$_.GPOStatus -match "AllSettingsDisabled"}
      
      







グループポリシーオブジェクトの分析



問題番号4。 リンクなしのGPOの検出





 PS C:\> Import-Module ActiveDirectory Get-ADOrganizationalUnit -filter * | select-object -ExpandProperty DistinguishedName | get-adobject -prop gplink | where {$_.gplink} | Select-object -expand gplink | foreach-object { foreach ($item in ($_.Split("]["))) { $links+=$regex.match($item).Value } } Get-GPO -All | Where {$links -notcontains $_.id}
      
      







追加のチーム
 #requires -version 2.0 <# Find unlinked GPOs. This requires the Active Directory Module This version does not query for site links #> Import-Module ActiveDirectory,GroupPolicy #GUID regular expression pattern [Regex]$RegEx = "(([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12})" #create an array of distinguishednames $dn=@() $dn+=Get-ADDomain | select -ExpandProperty DistinguishedName $dn+=Get-ADOrganizationalUnit -filter * | select -ExpandProperty DistinguishedName $links=@() #get domain and OU links foreach ($container in $dn) { #pull the GUID and add it to the array of links get-adobject -identity $container -prop gplink | where {$_.gplink} | Select -expand gplink | foreach { #there might be multiple GPO links so split foreach ($item in ($_.Split("]["))) { $links+=$regex.match($item).Value } #foreach item } #foreach } #foreach container #$links <# get all gpos where the ID doesn't belong to the array write the GPO to the pipeline #> Get-GPO -All | Where {$links -notcontains $_.id}
      
      









問題5.レジストリに追加設定があるグループポリシーオブジェクト(「追加レジストリ設定」)



レジストリで過剰な設定を見つける

 #Use Xpath with the XML report data PS C:\> [xml]$report = Get-GPOReport -Name MyGPO -ReportType XML PS C:\> $ns = @{q3 = "http://www.microsoft.com/GroupPolicy/Settings/Registry"} PS C:\> $nodes = Select-Xml -Xml $report -Namespace $ns -XPath "//q3:RegistrySetting" | select -expand Node | Where {$_.AdmSetting -eq 'false'}
      
      







追加のチーム
 #requires -version 2.0 #find GPOs with extra registry, ie non-ADM settings Function Test-GPOExtraRegistry { Param ( [Parameter(Position=0,ValueFromPipeline=$True, ValueFromPipelinebyPropertyName=$True)] [string]$DisplayName ) Begin { #import the GroupPolicy Module Import-Module GroupPolicy } Process { #create an XML report [xml]$report=Get-GPOReport -Name $displayname -ReportType XML #define the XML namespace $ns=@{q3="http://www.microsoft.com/GroupPolicy/Settings/Registry"} $nodes=Select-Xml -Xml $report -Namespace $ns -XPath "//q3:RegistrySetting" | select -expand Node | Where {$_.AdmSetting -eq 'false'} if ($nodes) { #extra settings were found so get the GPO and write it to the pipeline Get-GPO -Name $Displayname } } End {} } #function #Import-Module GroupPolicy #Get-GPO -all | Test-GPOExtraRegistry
      
      









繰り返しになりますが、投稿では、レポートで示されたものの乾燥した残留物のみを提示したことを示しています。 レポート自体はここで見ることができます



ボーナス:





NetWrix Group Policy Change Reporterプログラムを使用して、グループポリシーレポートを生成することもできます。



All Articles