1

I would really appreciate any help you may have, regarding the following problem:

XML data is stored in .xml file.

I would like to filter-out some XML nodes if they have the proper "distinguishedname" (verifying it either by name).

Below is the XML structure:

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>Selected.Microsoft.ActiveDirectory.Management.ADGroup</T>
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="Samaccountname">user.name</S>
      <S N="distinguishedname">CN=Domain Users,CN=Users,DC=company,DC=com</S>
    </MS>
  <Obj RefId="1">
    <TNRef RefId="0" />
    <MS>
      <S N="Samaccountname">user.name1</S>
      <S N="distinguishedname">CN=app_name_1,OU=publ,OU=app,DC=comp,DC=com</S>
    </MS>
  </Obj>
  <Obj RefId="2">
    <TNRef RefId="0" />
    <MS>
      <S N="Samaccountname">user.name1</S>
      <S N="distinguishedname">CN=app_name_2,OU=publ,OU=app,DC=comp,DC=com</S>
    </MS>
  </Obj>
  <Obj RefId="3">
    <TNRef RefId="0" />
    <MS>
      <S N="Samaccountname">user.name2</S>
      <S N="distinguishedname">CN=CN=app_name_3,OU=publ,OU=app,DC=comp,DC=com</S>
    </MS>
  </Obj>
  <Obj RefId="4">
    <TNRef RefId="0" />
    <MS>
      <S N="Samaccountname">user.name2</S>
      <S N="distinguishedname">CN=app_name_4,OU=publ,OU=app,DC=comp,DC=com</S>
    </MS>
  </Obj>
</Objs>

The content reads first

$filedata = gc $Env:HOMEDRIVE\users.xml

And then filtering out

$filedata = foreach ($obj in $filexml.Objs.Obj){
        $obj.MS.S | ?{ $_.N -eq "distinguishedname"} | 
        %{if( $_."#text" -match "*name_1" -or $_."#text" -match "*name_4*") 
    {$obj}}}

In my example <Obj RefId="2"> and <Obj RefId="4"> are OK and should be filtered, and <Obj RefId="0"> and <Obj RefId="1"> should be completely removed from the XML.

I would really appreciate any advice!

2
  • That's clixml - any reason you can't use Import-Clixml, and then use Where-Object? Commented Jul 20, 2012 at 15:29
  • @ViConst - in your filter, you say you want to keep name_1 and name_4, but in your explanation, you mention you want to keep name_2 and name_4. Please verify. Commented Jul 20, 2012 at 16:04

2 Answers 2

3

Well, for a start you are not assigning the variable $filexml as anything so you probably need

$filexml = [xml] fileData

If you're not using powershell ISE to debug your code you are missing out, setting a breakpoint on your foreach would have shown you that the $FileXml variable was null

And your xml is invalid, it should be

    <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>Selected.Microsoft.ActiveDirectory.Management.ADGroup</T>
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>

      <S N="Samaccountname">user.name</S>
      <S N="distinguishedname">CN=Domain Users,CN=Users,DC=company,DC=com</S>
    </MS>
  </Obj>
  <Obj RefId="1">
    <TNRef RefId="0" />
    <MS>
      <S N="Samaccountname">user.name1</S>
      <S N="distinguishedname">CN=app_name_1,OU=publ,OU=app,DC=comp,DC=com</S>
    </MS>
  </Obj>
  <Obj RefId="2">
    <TNRef RefId="0" />
    <MS>
      <S N="Samaccountname">user.name1</S>
      <S N="distinguishedname">CN=app_name_2,OU=publ,OU=app,DC=comp,DC=com</S>
    </MS>
  </Obj>
  <Obj RefId="3">
    <TNRef RefId="0" />
    <MS>
      <S N="Samaccountname">user.name2</S>
      <S N="distinguishedname">CN=CN=app_name_3,OU=publ,OU=app,DC=comp,DC=com</S>
    </MS>
  </Obj>
  <Obj RefId="4">
    <TNRef RefId="0" />
    <MS>
      <S N="Samaccountname">user.name2</S>
      <S N="distinguishedname">CN=app_name_4,OU=publ,OU=app,DC=comp,DC=com</S>
    </MS>
  </Obj>
</Objs>
Sign up to request clarification or add additional context in comments.

1 Comment

Right! I just missed the $filexml = [xml] fileData. Thanks!
0

Assuming the following xml, and the goal is to remove all Obj RefId that do not equal 2 and 4:

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
  <T>Selected.Microsoft.ActiveDirectory.Management.ADGroup</T>
  <T>System.Management.Automation.PSCustomObject</T>
  <T>System.Object</T>
</TN>
<MS>
  <S N="Samaccountname">user.name</S>
  <S N="distinguishedname">CN=Domain Users,CN=Users,DC=company,DC=com</S>
</MS>
</Obj>
<Obj RefId="1">
<TNRef RefId="0" />
<MS>
  <S N="Samaccountname">user.name1</S>
  <S N="distinguishedname">CN=app_name_1,OU=publ,OU=app,DC=comp,DC=com</S>
</MS>
</Obj>
<Obj RefId="2">
<TNRef RefId="0" />
<MS>
  <S N="Samaccountname">user.name1</S>
  <S N="distinguishedname">CN=app_name_2,OU=publ,OU=app,DC=comp,DC=com</S>
</MS>
</Obj>
<Obj RefId="3">
<TNRef RefId="0" />
<MS>
  <S N="Samaccountname">user.name2</S>
  <S N="distinguishedname">CN=CN=app_name_3,OU=publ,OU=app,DC=comp,DC=com</S>
</MS>
</Obj>
<Obj RefId="4">
<TNRef RefId="0" />
<MS>
  <S N="Samaccountname">user.name2</S>
  <S N="distinguishedname">CN=app_name_4,OU=publ,OU=app,DC=comp,DC=com</S>
</MS>
</Obj>
</Objs>

Define filters:

$filters = [regex]".*(name_2|name_4).*"

Load xml:

$xml = [xml](Get-Content "$home\Documents\test.xml")

Remove unwanted elements:

$xml.Objs.Obj | ?{ ($_.MS.S | ?{$_.N -eq "distinguishedname"}).'#text' -notmatch $filters} | %{$xml.Objs.RemoveChild($_)}

Save xml:

$xml.Save("$home\Documents\test2.xml")

1 Comment

I need to save also the first node <TN RefId="0"> <T>Selected.Microsoft.ActiveDirectory.Management.ADGroup</T> <T>System.Management.Automation.PSCustomObject</T> <T>System.Object</T> </TN> How to filter out it as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.