使用foreach powershell xml替换teg

发布于 2025-01-21 05:33:54 字数 5769 浏览 0 评论 0原文

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

for a comment:
enter image description here

How to fix the script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硬不硬你别怂 2025-01-28 05:33:54

您试图附加的XML片段并不完善。假设实际表格就像下面的形式一样,您可以获得预期输出的一种方法是:

#EDIT
$xml=[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>
"@

$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>
      </Props>
    </Obj>
  </LST>
'@

#EDIT 2
$node = $xml.SelectSingleNode('//*[local-name()="LST"]') 
$dest = $node.ParentNode
$dest.RemoveChild($node)

$xmlFragment=$xml.CreateDocumentFragment()
$xmlFragment.InnerXML=$LST
$dest.AppendChild($xmlFragment)

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:

#EDIT
$xml=[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>
"@

$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>
      </Props>
    </Obj>
  </LST>
'@

#EDIT 2
$node = $xml.SelectSingleNode('//*[local-name()="LST"]') 
$dest = $node.ParentNode
$dest.RemoveChild($node)

$xmlFragment=$xml.CreateDocumentFragment()
$xmlFragment.InnerXML=$LST
$dest.AppendChild($xmlFragment)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文