对于以下问题,我非常感谢您的帮助:
XML数据存储在.xml文件中。
我想过滤掉一些XML节点,如果它们具有正确的“distinguishedname”(通过名称验证它)。
以下是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">
中都可以,我们应该对其进行过滤,并且应该从XML中完全删除<Obj RefId="0">
和<Obj RefId="1">
。
我真的很感激任何建议!
答案 0 :(得分:2)
好吧,首先你没有将变量$ filexml指定为任何东西,所以你可能需要
$filexml = [xml] fileData
如果您没有使用PowerShell ISE来调试您错过的代码,那么在foreach上设置断点会向您显示$ FileXml变量为null
你的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>
<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>
答案 1 :(得分: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")