Use PowerShell to extract linking of a specific GPO from XML - Stack Overflow

admin2025-04-17  2

I am attempting to use PowerShell to extract link information(SOMPath) from a GPO using the Get-GPOreport command and report type XML without exporting the GPO XML to a file first. I am struggling to get it to give me the results I need.

here is the code I am working with.

 $reports2 = Get-GPOReport -name "GPO-Name" -ReportType XML
 select-xml -content $reports2 -XPath "//SOMPath" | foreach {$_.node.InnerXML}

it is not returning any data nor is it returning any errors

the variable $reports2 does return and properly formatted XML and it does have the SOMPath data in it that I need to extract but I cant seam to get it to give me the SOMPath data. example of the SOMPath data I am looking to extract: Domain/OU/OU/OU

the ultimate goal is I will be running this against a list of GPOs and need it to list something like the following:

 GPO1:
 SOMPath1
 SOMPath2
 SOMPath3

 GPO2:
 SOMPath1
 SOMPath2

per the guidance from Microsoft website, it should be relatively simple (as seen in the MS example below)

(Begin MS XML example)

 $Xml = @"
 <?xml version="1.0" encoding="utf-8"?>
 <Book>
   <projects>
     <project name="Book1" date="2009-01-20">
       <editions>
         <edition language="English">En.Book1</edition>
         <edition language="German">Ge.Book1.Com</edition>
         <edition language="French">Fr.Book1</edition>
         <edition language="Polish">Pl.Book1</edition>
       </editions>
     </project>
   </projects>
 </Book>
 "@

(Begin MS Code example)

 Select-Xml -Content $Xml -XPath "//edition" | foreach {$_.node.InnerXML}

(begin MS results example)

 En.Book1
 Ge.Book1.Com
 Fr.Book1
 Pl.Book1

thank you for your time and help

I am attempting to use PowerShell to extract link information(SOMPath) from a GPO using the Get-GPOreport command and report type XML without exporting the GPO XML to a file first. I am struggling to get it to give me the results I need.

here is the code I am working with.

 $reports2 = Get-GPOReport -name "GPO-Name" -ReportType XML
 select-xml -content $reports2 -XPath "//SOMPath" | foreach {$_.node.InnerXML}

it is not returning any data nor is it returning any errors

the variable $reports2 does return and properly formatted XML and it does have the SOMPath data in it that I need to extract but I cant seam to get it to give me the SOMPath data. example of the SOMPath data I am looking to extract: Domain/OU/OU/OU

the ultimate goal is I will be running this against a list of GPOs and need it to list something like the following:

 GPO1:
 SOMPath1
 SOMPath2
 SOMPath3

 GPO2:
 SOMPath1
 SOMPath2

per the guidance from Microsoft website, it should be relatively simple (as seen in the MS example below)

(Begin MS XML example)

 $Xml = @"
 <?xml version="1.0" encoding="utf-8"?>
 <Book>
   <projects>
     <project name="Book1" date="2009-01-20">
       <editions>
         <edition language="English">En.Book1.com</edition>
         <edition language="German">Ge.Book1.Com</edition>
         <edition language="French">Fr.Book1.com</edition>
         <edition language="Polish">Pl.Book1.com</edition>
       </editions>
     </project>
   </projects>
 </Book>
 "@

(Begin MS Code example)

 Select-Xml -Content $Xml -XPath "//edition" | foreach {$_.node.InnerXML}

(begin MS results example)

 En.Book1.com
 Ge.Book1.Com
 Fr.Book1.com
 Pl.Book1.com

thank you for your time and help

Share Improve this question asked Jan 30 at 21:52 NuckinFutzNuckinFutz 1278 silver badges19 bronze badges 6
  • 3 You'd need to provide a sample of the GPO XML... – Santiago Squarzon Commented Jan 30 at 21:57
  • 2 Without seeing the actual xml you’re working with (per @SantiagoSquarzon) it’s impossible to give a definitive answer, but looking at your code there’s a good chance it’s related to xml namespaces - have a read of the documentation at learn.microsoft.com/en-us/powershell/module/… for details, and / or add your specific xml to your question… – mclayton Commented Jan 31 at 6:36
  • @mclayton that website is the exact one i got the info from posted above and will post a sample of the xml in next comment. thank you. – NuckinFutz Commented Jan 31 at 11:47
  • @SantiagoSquarzon here is a sample of the GPO XML, here is the section of the GPO XML i am trying to extract the data from and the info i need to pull is form the <SOMPath>Domain/OU/OU/OU</SOMPath> but only want the Domain/OU/OU/OU from that line <LinksTo> <SOMName>T0-Devices</SOMName> <SOMPath>Domain/OU/OU/OU</SOMPath> <Enabled>true</Enabled> <NoOverride>false</NoOverride> </LinksTo> </GPO> – NuckinFutz Commented Jan 31 at 11:49
  • Please directly update your question with the sample XML, and make sure that it is a minimal, but complete document that exhibits the problem. However, given that Joseph's answer seemingly worked, @mclayton's hunch was likely correct: XPath queries (such as via Select-Xml) are namespace-sensitive, whereas PowerShell's property-based adaption of the [xml] DOM is not. – mklement0 Commented Jan 31 at 12:24
 |  Show 1 more comment

1 Answer 1

Reset to default 2

I was able to do this with the following code:

$allGpos = Get-GPO -All
foreach ($gpo in $allGpos) { 
   [xml]$report = Get-GPOReport -Guid $GPO.ID -ReportType Xml
   $report.GPO.Name
   $report.GPO.LinksTo.SOMPath
}

It outputs a string object. Don't know if that's what you're looking for or not. Hope it helps. I've never been too good with that Select-Xml cmdlet, lol.

Edit: I was able to come up with something like putting it into a PS Custom Object so that it has properties you can reference later. Or when you output it, it looks like a fairly nice table

using namespace System.Collections.Generic
$GPOLinkReport = [List[PSObject]]::new()
$allGposInDomain = Get-GPO -All
foreach ($gpo in $allGposInDomain) { 
   [xml]$report = Get-GPOReport -Guid $gpo.Id -ReportType Xml
   $GPOLinks = [String]::Join(",",$report.GPO.LinksTo.SOMPath)
   $GPOLinkReportObj = [PSCustomObject]@{
      Name = $report.GPO.Name
      Links = $GPOLinks
   }
   $GPOLinkReport.Add($GPOLinkReportObj)
}

$GPOLinkReport

Edit2: Here's yet another block that puts one link per name per row

$GPOLinkReport = [List[PSObject]]::new()
$allGposInDomain = Get-GPO -All
foreach ($gpo in $allGposInDomain) { 
    [xml]$report = Get-GPOReport -Guid $gpo.Id -ReportType Xml
    $GPOLinks = $report.GPO.LinksTo.SOMPath
    foreach ($link in $GPOLinks) {
        $GPOLinkReportObj = [PSCustomObject]@{
            Name = $report.GPO.Name
            Links = $link
        }
        $GPOLinkReport.Add($GPOLinkReportObj)
    }
}
$GPOLinkReport #| Export-csv c:\temp\GPOLinks.csv -NoTypeInformation

Choose your poison, lol.

转载请注明原文地址:http://anycun.com/QandA/1744888891a89053.html