使用foreach powershell xml替换teg
我对SQL报告服务器报告有XML订阅。以下是XML部分:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Management.Automation.PSCustomObject</T>
<T>System.Object</T>
</TN>
<MS>
<S N="EventType">TimedSubscription</S>
<S N="SubscriptionID">0cd266b0-ecbe-41e0-b3a8-0118a6e1066c</S>
<DT N="ModifiedDate">2022-04-11T18:16:41.677</DT>
<B N="LastExecutedSpecified">false</B>
<S N="Report">Subscription Director</S>
<S N="Status">New Subscription</S>
<Obj N="Active" RefId="1">
<TN RefId="1">
<T>Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1Reports_ReportService2010_asmx.ActiveState</T>
<T>System.Object</T>
</TN>
<ToString>Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1Reports_ReportService2010_asmx.ActiveState</ToString>
<Props>
<B N="DeliveryExtensionRemoved">false</B>
<B N="DeliveryExtensionRemovedSpecified">false</B>
<B N="SharedDataSourceRemoved">false</B>
<B N="SharedDataSourceRemovedSpecified">false</B>
<B N="MissingParameterValue">false</B>
<B N="MissingParameterValueSpecified">false</B>
<B N="InvalidParameterValue">false</B>
<B N="InvalidParameterValueSpecified">false</B>
<B N="UnknownReportParameter">false</B>
<B N="UnknownReportParameterSpecified">false</B>
<B N="DisabledByUser">false</B>
<B N="DisabledByUserSpecified">false</B>
</Props>
</Obj>
<Nil N="VirtualPath" />
<S N="ModifiedBy">domain\admin</S>
<S N="Description">User</S>
<DT N="LastExecuted">0001-01-01T00:00:00</DT>
<Obj N="Values" RefId="2">
<TN RefId="2">
<T>Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1Reports_ReportService2010_asmx.ParameterValue[]</T>
<T>System.Array</T>
<T>System.Object</T>
</TN>
<LST />
</Obj>
</MS>
<!--..........-->
</Obj>
</Objs>
XML中有许多这样的字符串。我想用
<LST>
<Obj N="Values" RefId ="3">
<TN RefId ="3"> <T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</T>
<T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValueOfFieldReference</T>
<T>System.Object</T>
<TN/>
<ToString>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</ToString>
<Props>
<S N="Name">name</S>
<S N="Value">UserName</S>
<Nil N="Label">
</Obj>
</LST>
以下查询替换&lt; lt; lst/&gt;
:
$XML =Get-Content '\\share\test.xml'
Foreach ($LST in $xml)
{
$_ -replace @"
<LST />"@,
@"
<LST>
<Obj N="Values" RefId ="3">
<TN RefId ="3"> <T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</T>
<T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValueOfFieldReference</T>
<T>System.Object</T>
<TN/>
<ToString>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</ToString>
<Props>
<S N="Name">name</S>
<S N="Value">UserName</S>
<Nil N="Label">
</Obj>
</LST>
"@
}
Out-file '\\share\test.xml'
目标是在整个文件test.xml中获得这样的结果,
<Obj N="Values" RefId ="2">
<TN RefId ="2"> <T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue[]</T>
<T>System.Array</T>
<T>System.Object</T>
</TN>
<LST>
<Obj N="Values" RefId ="3">
<TN RefId ="3"> <T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</T>
<T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValueOfFieldReference</T>
<T>System.Object</T>
<TN/>
<ToString>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</ToString>
<Props>
<S N="Name">name</S>
<S N="Value">UserName</S>
<Nil N="Label">
</Obj>
</LST>
</Obj>
如何修复脚本?
I have an XML subscription to a SQL Reporting Server report. Below is the XML part:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Management.Automation.PSCustomObject</T>
<T>System.Object</T>
</TN>
<MS>
<S N="EventType">TimedSubscription</S>
<S N="SubscriptionID">0cd266b0-ecbe-41e0-b3a8-0118a6e1066c</S>
<DT N="ModifiedDate">2022-04-11T18:16:41.677</DT>
<B N="LastExecutedSpecified">false</B>
<S N="Report">Subscription Director</S>
<S N="Status">New Subscription</S>
<Obj N="Active" RefId="1">
<TN RefId="1">
<T>Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1Reports_ReportService2010_asmx.ActiveState</T>
<T>System.Object</T>
</TN>
<ToString>Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1Reports_ReportService2010_asmx.ActiveState</ToString>
<Props>
<B N="DeliveryExtensionRemoved">false</B>
<B N="DeliveryExtensionRemovedSpecified">false</B>
<B N="SharedDataSourceRemoved">false</B>
<B N="SharedDataSourceRemovedSpecified">false</B>
<B N="MissingParameterValue">false</B>
<B N="MissingParameterValueSpecified">false</B>
<B N="InvalidParameterValue">false</B>
<B N="InvalidParameterValueSpecified">false</B>
<B N="UnknownReportParameter">false</B>
<B N="UnknownReportParameterSpecified">false</B>
<B N="DisabledByUser">false</B>
<B N="DisabledByUserSpecified">false</B>
</Props>
</Obj>
<Nil N="VirtualPath" />
<S N="ModifiedBy">domain\admin</S>
<S N="Description">User</S>
<DT N="LastExecuted">0001-01-01T00:00:00</DT>
<Obj N="Values" RefId="2">
<TN RefId="2">
<T>Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1Reports_ReportService2010_asmx.ParameterValue[]</T>
<T>System.Array</T>
<T>System.Object</T>
</TN>
<LST />
</Obj>
</MS>
<!--..........-->
</Obj>
</Objs>
There are many such strings in XML. I want to replace <LST/>
with
<LST>
<Obj N="Values" RefId ="3">
<TN RefId ="3"> <T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</T>
<T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValueOfFieldReference</T>
<T>System.Object</T>
<TN/>
<ToString>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</ToString>
<Props>
<S N="Name">name</S>
<S N="Value">UserName</S>
<Nil N="Label">
</Obj>
</LST>
Used the following query:
$XML =Get-Content '\\share\test.xml'
Foreach ($LST in $xml)
{
$_ -replace @"
<LST />"@,
@"
<LST>
<Obj N="Values" RefId ="3">
<TN RefId ="3"> <T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</T>
<T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValueOfFieldReference</T>
<T>System.Object</T>
<TN/>
<ToString>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</ToString>
<Props>
<S N="Name">name</S>
<S N="Value">UserName</S>
<Nil N="Label">
</Obj>
</LST>
"@
}
Out-file '\\share\test.xml'
The goal is to get such a result in the entire file test.xml
<Obj N="Values" RefId ="2">
<TN RefId ="2"> <T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue[]</T>
<T>System.Array</T>
<T>System.Object</T>
</TN>
<LST>
<Obj N="Values" RefId ="3">
<TN RefId ="3"> <T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</T>
<T>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValueOfFieldReference</T>
<T>System.Object</T>
<TN/>
<ToString>MIcrosoft.Powershell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxyReports_ReportService2010_asmx.ParameterValue</ToString>
<Props>
<S N="Name">name</S>
<S N="Value">UserName</S>
<Nil N="Label">
</Obj>
</LST>
</Obj>
How to fix the script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您试图附加的XML片段并不完善。假设实际表格就像下面的形式一样,您可以获得预期输出的一种方法是:
The xml fragment you are trying to append is not well-formed. Assuming the actual form is like the one below, one way you can get your expected output is this: