1

对于以下问题,我非常感谢您提供的任何帮助:

XML 数据存储在 .xml 文件中。

如果它们具有正确的“可分辨名称”(通过名称验证),我想过滤掉一些 XML 节点。

下面是 XML 结构:

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

内容先读

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

然后过滤掉

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

在我的示例中<Obj RefId="2">,并<Obj RefId="4">没有问题,应该被过滤,<Obj RefId="0">并且<Obj RefId="1">应该从 XML 中完全删除。

我真的很感激任何建议!

4

2 回答 2

2

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>
于 2012-07-20T15:25:28.070 回答
0

假设以下 xml,目标是删除所有不等于 2 和 4 的 Obj RefId:

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

定义过滤器:

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

加载 xml:

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

删除不需要的元素:

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

保存 xml:

$xml.Save("$home\Documents\test2.xml")
于 2012-07-20T15:52:54.547 回答