Share via

Extract xml with two value existence

Daniel K 21 Reputation points
2023-05-25T05:30:34.8966667+00:00
hi, can advise how to scan through xml with multiple instance of <ExtensionData> and only return "True" if the xml consist of both "Turn off Automatic Root Certificates Update" and "Enabled" (see in bold) with the same instance of <ExtensionData> , please note **q3** is random (can be q1, q14).
Thanks.


<ExtensionData>
      <Extension xmlns:q3="http://www.microsoft.com/GroupPolicy/Settings/Registry" xsi:type="q3:RegistrySettings">
        <q3:Policy>
          **<q3:Name>Turn off Automatic Root Certificates Update</q3:Name>**
**          <q3:State>Enabled</q3:State>**
          <q3:Explain>This policy setting specifies your computer will contact the Windows Update website.</q3:Explain>
          <q3:Supported>At least Windows Server 2003 operating systems with SP1 or Windows XP Professional with SP2</q3:Supported>
          <q3:Category>System/Internet Communication Management/Internet Communication settings</q3:Category>
        </q3:Policy>
        <q3:Blocked>false</q3:Blocked>
      </Extension>
      <Name>Registry</Name>
    </ExtensionData>
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2023-05-25T14:59:49.8666667+00:00

    Rather than use XPath this simple problem, here's a way to get the Policy elements you're looking for:

    # the presence of the "xsi" namespace namespace declaration in the following line causes
    # the XML to fail during parsing.
    # It's been removed for the sake making this example work;
    #<Extension xmlns:q3="http://www.microsoft.com/GroupPolicy/Settings/Registry" xsi:type="q3:RegistrySettings">
    
    $x = @"
    <?xml version="1.0" encoding="UTF-8"?>
    <ExtensionData>
        <Extension xmlns:q3="http://www.microsoft.com/GroupPolicy/Settings/Registry">
            <q3:Policy>
                <q3:Name>Turn off Automatic Root Certificates Update</q3:Name>
                <q3:State>Enabled</q3:State>
                <q3:Explain>This policy setting specifies your computer will contact the Windows Update website.</q3:Explain>
                <q3:Supported>At least Windows Server 2003 operating systems with SP1 or Windows XP Professional with SP2</q3:Supported>
                <q3:Category>System/Internet Communication Management/Internet Communication settings</q3:Category>
            </q3:Policy>
            <q3:Blocked>false</q3:Blocked>
        </Extension>
        <Name>Registry</Name>
    </ExtensionData>
    "@
    
    $Policies = @() # there may be multiple policy elements that meet the criteria
    $xml = [xml]$x
    $xml.ExtensionData.Extension.Policy|
        ForEach-Object{
            # Because XML is case-sensitive, the "-ceq" operator is used. Using the "-eq" operator will also work 
            # but may produce inaccurate results.
            if ($_.Name -ceq 'Turn off Automatic Root Certificates Update' -and
                $_.State -ceq 'Enabled'
            ){
                $Policies += $_     # just place the matching Policy XmlElement in an array
                                    # because there's nothing said about what to do once it's been found!
            }
        }
    

    Was this answer helpful?

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.