Hello everyone, my name is Liju and I am a Premier Field Engineer specializing in Active Directory and Azure AD. I have worked on group policy for 15 years (I remember having to use Secedit.exe to refresh group policy) and today I wanted to share some experience from the field.
Recently I came across a scenario where we needed to find out what group policy settings were being applied to a large number of machines. We looked at two options to automate the collection of this data:
But first, a primer – as you likely know, RSoP data can be collected using one of two methods:
With both options (Get-GPResultantSetOfPolicy and GPResult.exe) we faced the following challenges:
An IT organization that has followed a tiering model may not have a single account that has administrative access across multiple tiers.
When gathering data from multiple machines, there may not be a single account that has a profile on every one of them.
This is where PowerShell can be used to instantiate the GPMgmt.GPM class that allows access to most of the GPMC functionality.
Using this method:
You will find below sample code to generate RSoP data in planning mode and in logging mode.
Remember to assign values from your own environment to the following variables:
And edit these paths based on your folder structure:
Disclaimer
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
$machineName = "LITWARE\ROOT8WS01"
$machineOU = "OU=Workstations,OU=Machines,OU=IT,DC=Litware,DC=com"
$userName = "LITWARE\JoeSchmoe"
$userOU = "OU=Users,OU=IT,DC=Litware,DC=com"
$gpmObject = New-Object -ComObject GPMgmt.GPM
$gpmConstants = $gpmObject.GetConstants()
$rsopObject = $gpmObject.GetRSOP($gpmConstants.RSOPmodePlanning,$null,0)
$rsopObject.planningComputer = $machineName
$rsopObject.PlanningUser = $userName
$rsopObject.planningComputerSOM = $machineOU
$rsopObject.planningUserSOM = $userOU
$rsopObject.PlanningDomainController = "RootDC01.Litware.com"
$rsopObject.CreateQueryResults()
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportHTML, "C:\Temp\GPO_RSoP\RSoP.html")
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportXML, "C:\Temp\GPO_RSoP\RSoP.xml")
$rsopObject.ReleaseQueryResults()
This creates both an HTML report and an XML report. Comment out either the second to last or the third to last line to create just of the reports.
$machineName = "LITWARE\ROOT8WS01"
$machineOU = "OU=Workstations,OU=Machines,OU=IT,DC=Litware,DC=com"
$gpmObject = New-Object -ComObject GPMgmt.GPM
$gpmConstants = $gpmObject.GetConstants()
$rsopObject = $gpmObject.GetRSOP($gpmConstants.RSOPmodePlanning,$null,0)
$rsopObject.planningComputer = $machineName
$rsopObject.planningComputerSOM = $machineOU
$rsopObject.PlanningDomainController = "RootDC01.Litware.com"
$rsopObject.PlanningFlags = 131072 # Only computer; no user
$rsopObject.CreateQueryResults()
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportHTML, "C:\Temp\GPO_RSoP\RSoP.html")
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportXML, "C:\Temp\GPO_RSoP\RSoP.xml")
$rsopObject.ReleaseQueryResults()
$userName = "LITWARE\JoeSchmoe"
$userOU = "OU=Users,OU=IT,DC=Litware,DC=com"
$gpmObject = New-Object -ComObject GPMgmt.GPM
$gpmConstants = $gpmObject.GetConstants()
$rsopObject = $gpmObject.GetRSOP($gpmConstants.RSOPmodePlanning,$null,0)
$rsopObject.PlanningUser = $userName
$rsopObject.planningUserSOM = $userOU
$rsopObject.PlanningDomainController = "RootDC01.Litware.com"
$rsopObject.PlanningFlags = 65536 # Only user; no computer
$rsopObject.CreateQueryResults()
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportHTML, "C:\Temp\GPO_RSoP\RSoP.html")
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportXML, "C:\Temp\GPO_RSoP\RSoP.xml")
$rsopObject.ReleaseQueryResults()
Although the Get-GPResultantSetOfPolicy cmdlet collects RSoP data in logging mode, for the sake of completeness, here is how to do this using the GPMgmt.GPM class. Keep in mind that:
$machineName = "LITWARE\ROOT8WS01"
$userName = "LITWARE\JoeSchmoe"
$gpmObject = New-Object -ComObject GPMgmt.GPM
$gpmConstants = $gpmObject.GetConstants()
$rsopObject = $gpmObject.GetRSOP($gpmConstants.RSOPModeLogging,$null,0)
$rsopObject.LoggingComputer = $machineName
$rsopObject.LoggingUser = $userName
$rsopObject.CreateQueryResults()
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportHTML , "C:\Temp\GPO_RSoP\RSoP.html")
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportXML , "C:\Temp\GPO_RSoP\RSoP.xml")
$rsopObject.ReleaseQueryResults()
This creates both an HTML report and an XML report. Comment out either the second to last or the third to last line to create just of the reports.
If the user specified in $userName does not have a profile on the machine specified in $machineName, the reports are not generated, and this is the error you will see:
Exception from HRESULT: 0x8004100E
At line:17 char:1
+ $rsop.CreateQueryResults()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Value does not fall within the expected range.
At line:19 char:1
+ $Result = $rsop.GenerateReportToFile($constants.ReportXML , "C:\Temp\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
$machineName = "LITWARE\ROOT8WS01"
$gpmObject = New-Object -ComObject GPMgmt.GPM
$gpmConstants = $gpmObject.GetConstants()
$rsopObject = $gpmObject.GetRSOP($gpmConstants.RSOPModeLogging,$null,0)
$rsopObject.LoggingComputer = $machineName
$rsopObject.LoggingFlags = 131072 # Only computer; no user
$rsopObject.CreateQueryResults()
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportHTML , "C:\Temp\GPO_RSoP\RSoP.html")
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportXML , "C:\Temp\GPO_RSoP\RSoP.xml")
$rsopObject.ReleaseQueryResults()
$userName = "LITWARE\JoeSchmoe"
$gpmObject = New-Object -ComObject GPMgmt.GPM
$gpmConstants = $gpmObject.GetConstants()
$rsopObject = $gpmObject.GetRSOP($gpmConstants.RSOPModeLogging,$null,0)
$rsopObject.LoggingUser = $userName
$rsopObject.LoggingFlags = 65536 # Only user; no computer
$rsopObject.CreateQueryResults()
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportHTML , "C:\Temp\GPO_RSoP\RSoP.html")
$rsopReport = $rsopObject.GenerateReportToFile($gpmConstants.ReportXML , "C:\Temp\GPO_RSoP\RSoP.xml")
$rsopObject.ReleaseQueryResults()
Collecting user-only logging mode data is done against the localhost by default, and requires that the user under whose context the code is being executed have a profile.
There you have it – you can add one more tool to your arsenal for group policy reporting and troubleshooting, one especially useful if you want to collect this data for several users and/or machines using a script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.