PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

[FIXED] How to Extract string from XML Property

 August 14, 2022     object, output, powershell, properties, xml     No comments   

Issue

Using PowerShell, I am attempting to extract the contents of Value="" which will either be Production or Training, from a XML file. Here is a snippit of the XML file:

<ParameterGroup TestRequired="false" Name="CommonSystemConfig" xmlns="http://www...">
    <Parameter Name="DefaultEnvironmentId" Text="DefaultEnvironmentId" ParameterType="List">
        <Value UseFunction="false" Value="Training">
            <ValueList>
                <string>Production</string>
                <string>Training</string>
                <string>Staging</string>

I currently have the script as follows:

$XMLfile = "\\$pchostname\$xmlPath"
[XML]$paramBackup = Get-Content $XMLfile

$paramBackup.ParametersInfo.ParameterGroup.parameter | Where-Object {$_.Name -eq 'DefaultEnvironmentId'} | Select-object -ExpandProperty 'Value'

The script is giving me:

UseFunction Value      ValueList
----------- -----      ---------
false       Production ValueList

How would I just output the Production or Training so that I could have it populate an xls with the corresponding computer hostname?

Thank you


Solution

As you have it now, your object named Value has 3 properties, UseFunction,Value, and ValueList, and you need to select the child property named Value, which has a value of Training.

You could actually just duplicate what you already have, by adding another pipe and select to the end

| Select-Object -ExpandProperty 'Value'

So the full line would be

$paramBackup.ParametersInfo.ParameterGroup.parameter | Where-Object {$_.Name -eq 'DefaultEnvironmentId'} | Select-object -ExpandProperty 'Value' | Select-object -ExpandProperty 'Value'

Another way with additional dot-notation

($paramBackup.ParametersInfo.ParameterGroup.parameter | Where-Object {$_.Name -eq 'DefaultEnvironmentId'}).Value | Select-object -ExpandProperty 'Value'


Answered By - scottwtang
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing