XPathQuery on PowerShell XML output

Last post 03-20-2018, 7:27 AM by Sederik. 2 replies.
Sort Posts: Previous Next
  • XPathQuery on PowerShell XML output
    Posted: 03-15-2018, 5:55 PM

    Greetings!

    I'm new to Workflows and need some help on the correct syntax of XPathQuery. I have an ExecuteScript object that runs a PowerShell script which provides an XML output that looks like this:

    <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
      <Obj RefId="0">
        <TN RefId="0">
          <T>System.Management.ManagementObject#root\cimv2\Win32_Service</T>
          <T>System.Management.ManagementObject#root\cimv2\Win32_BaseService</T>
          <T>System.Management.ManagementObject#root\cimv2\CIM_Service</T>
          <T>System.Management.ManagementObject#root\cimv2\CIM_LogicalElement</T>
          <T>System.Management.ManagementObject#root\cimv2\CIM_ManagedSystemElement</T>
          <T>System.Management.ManagementObject#Win32_Service</T>
          <T>System.Management.ManagementObject#Win32_BaseService</T>
          <T>System.Management.ManagementObject#CIM_Service</T>
          <T>System.Management.ManagementObject#CIM_LogicalElement</T>
          <T>System.Management.ManagementObject#CIM_ManagedSystemElement</T>
          <T>System.Management.ManagementObject</T>
          <T>System.Management.ManagementBaseObject</T>
          <T>System.ComponentModel.Component</T>
          <T>System.MarshalByRefObject</T>
          <T>System.Object</T>
        </TN>
        <ToString>\\DC2\root\cimv2:Win32_Service.Name="WinRM"</ToString>
        <Props>
          <S N="Name">WinRM</S>
          <S N="State">Running</S>
        </Props>
        <MS>
          <S N="PSComputerName">DC2</S>
        </MS>
      </Obj>
    </Objs>

    Ideally I would like to enumerate the <Obj> elements and perform some operations for each of them depending on the <S N="State"> value. However I can't figure out how to select the "Running" string even for one <Obj> with a single XPathQuery. Can anybody help, please?

  • Re: XPathQuery on PowerShell XML output
    Posted: 03-16-2018, 10:43 AM

    Hey, the namespace tag here appears to be causing some issues with retrieving the value.  What you can do is add a Script activity which takes the xml from powershell script and removes this attribute:

    return xpath:{/workflow/ExecuteScript_1/commandOutput}.replaceAll("xmlns.*?(\"|\').*?(\"|\')", "");

    Then use the output of the Script as the input into your XPathQuery activity and use any xpath statement you want to get to this element like

    //S[@N='State']

    If you want to iterate through xml, you can use the ForEachXML which you would also want to use the output of the Script activity for your xpath statement.

    Regards

  • Re: XPathQuery on PowerShell XML output
    Posted: 03-20-2018, 7:27 AM

    Hi cwsunderland,

    Thanks for help! I'll try this on XML generated with Export-Xml. But I actually understood that I can do an easier thing then mess with XML as files and used ConvertTo-Xml -as Stream to get the XML written directly to standard output. This way I get a much simplier output to process, like

    <?xml version="1.0" encoding="utf-8"?>
    <Objects>
    <Object Type="System.Management.Automation.PSCustomObject">
      <Property Name="Name" Type="System.String">NTDS</Property>
      <Property Name="State" Type="System.String">Running</Property>
    </Object>
    </Objects>

    which XPathQuery has no problem to deal with.

    Thanks for help anyway. I'll keep your example for future reference whenever I need to remove unwanted parts of XML.

The content of the forums, threads and posts reflects the thoughts and opinions of each author, and does not represent the thoughts, opinions, plans or strategies of Commvault Systems, Inc. ("Commvault") and Commvault undertakes no obligation to update, correct or modify any statements made in this forum. Any and all third party links, statements, comments, or feedback posted to, or otherwise provided by this forum, thread or post are not affiliated with, nor endorsed by, Commvault.
Commvault, Commvault and logo, the “CV” logo, Commvault Systems, Solving Forward, SIM, Singular Information Management, Simpana, Commvault Galaxy, Unified Data Management, QiNetix, Quick Recovery, QR, CommNet, GridStor, Vault Tracker, InnerVault, QuickSnap, QSnap, Recovery Director, CommServe, CommCell, SnapProtect, ROMS, and CommValue, are trademarks or registered trademarks of Commvault Systems, Inc. All other third party brands, products, service names, trademarks, or registered service marks are the property of and used to identify the products or services of their respective owners. All specifications are subject to change without notice.
Close
Copyright © 2019 Commvault | All Rights Reserved. | Legal | Privacy Policy