Microsoft Secure Tech Accelerator
Apr 03 2024, 07:00 AM - 11:00 AM (PDT)
Microsoft Tech Community
Exporting Resultant Set of Policy (RSoP) data using PowerShell
Published Mar 09 2020 08:48 AM 22.5K Views
Microsoft

 

 

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:

  • Get-GPResultantSetOfPolicy PowerShell cmdlet
  • GPResult.exe command-line utility

 

But first, a primer – as you likely know, RSoP data can be collected using one of two methods:

  • Logging Mode
    • Used to review policy settings applied to a specific computer and user
    • Gathered by the Group Policy Results Wizard in GPMC
  • Planning mode
    • Used to simulate a policy implementation by using data from Active Directory.
    • Gathered by the Group Policy Modeling Wizard in GPMC

 

The Challenge

 

With both options (Get-GPResultantSetOfPolicy and GPResult.exe) we faced the following challenges:

  • Logging vs Planning modes – Both Get-GPResultantSetOfPolicy and GPResult.exe only collect data in logging mode.
  • Permissions – The user under whose context the commands are being run must have Generate Resultant Set of Policy (Logging) (for planning its: Generate Resultant Set of Policy (Planning)) permissions. This can be delegated at the organizational unit and by default, all Administrators of a machine has them.
    • Here are the errors you will see with the commands if you do not have the proper permissions:
      • Get-GPResultantSetOfPolicy error: Get-GPResultantSetOfPolicy : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
      • GPResult error: ERROR: The user name or password is incorrect.

An IT organization that has followed a tiering model may not have a single account that has administrative access across multiple tiers.

  • Profile - For logging mode you must provide a user account that has logged onto the machine at least once and has a profile present.
    • Here are the errors you will see with the commands if a profile does not exist:
      • Get-GPResultantSetOfPolicy error: Get-GPResultantSetOfPolicy : The Resultant Set of Policy (RSoP) report cannot be generated for user <user> on the <machine> computer because there is no RSoP logging data  for that user on that computer. This could be because the user has never logged onto that computer.
      • GPResult error: INFO: The user <usr> does not have RSoP data.

When gathering data from multiple machines, there may not be a single account that has a profile on every one of them.

 

 

Solution: Use PowerShell

 

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 can generate RSoP data in logging mode or in planning mode.
  • With planning mode, the user generating the data does not need Generate Resultant Set of Policy rights on the machine.
  • With planning mode, the user generating the data does not need to have logged on to the machine.

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:

  • $machineName
  • $machineOU
  • $userName
  • $userOU
  • $rsopObject.PlanningDomainController

And edit these paths based on your folder structure:

  • C:\Temp\GPO_RSoP\RSoP.html
  • C:\Temp\GPO_RSoP\RSoP.xml

 

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.

 

 

Planning Mode

 

$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.

 

Computer-only report:

 

$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()

 

User-only report:

 

$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()

 

 

Logging Mode

 

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:

  • To collect computer RSoP data, user collecting RSoP data must have sufficient permissions
  • User whose RSoP data is being collected must have a profile on the machine being evaluated against

 

$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

 

Computer-only report:

 

$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()

 

User-only report:

 

$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.

 

4 Comments
Version history
Last update:
‎Mar 09 2020 08:51 AM
Updated by: