none
Read an XML file with Powershell ? RRS feed

  • Question

  • Hi,

    Can someone please help. Is there a way in which powershell can check my 'example' xml file for an instance of 'BizTalk1' for Server1? Can PowerShell check <Group name="BiztalkGroup1" for <Name>BizTalk1 for Server1

    Hope my question makes sense. Thanks in advance

    <Config>
    <Defaults>
        <dgroup>BiztalkGroup1</dgroup>
        <dgroup>Services</dgroup>
    </Defaults>
    <Server name="Server1">
      <Services>
    	<Group name="Services" order="1">
    		<Items>
    			<Name>Service 1</Name>
    			<Name>Service 2</Name>
    		</Items>
    	</Group>
      </Services>
    	<HostInstances>
    	<Group name="BiztalkGroup1"<order="2">
    	  <Items>
    	    <Name>Biztalk1</Name>
                 <Name>Biztalk2</Name>
                 <Name>Biztalk3</Name>
                 <Name>Biztalk4</Name>
               </Items>
       </Group>
    </HostInstances>
    </Server>
    <Server name="Server2">
      <Services>
    	<Group name="Services" order="1">
    		<Items>
    			<Name>Service 1</Name>
    			<Name>Service 2</Name>
    		</Items>
    	</Group>
      </Services>
    	<HostInstances>
    	<Group name="BiztalkGroup1"<order="2">
    	  <Items>
    	    <Name>Biztalk1</Name>
                 <Name>Biztalk2</Name>
                 <Name>Biztalk3</Name>
                 <Name>Biztalk4</Name>
               </Items>
       </Group>
    </HostInstances>
    </Server>
    				

    Friday, October 31, 2014 2:49 PM

Answers

  • Example:

    PS >$node=select-xml -Xml $xml -XPath '//Server[@name="Server1"]/*/Group[@name="BiztalkGroup1"]/*/Name[text()="Biztalk1"]'
    PS > $node.Node

    #text
    -----
    Biztalk1


    ¯\_(ツ)_/¯

    • Marked as answer by JimAll Monday, November 3, 2014 2:06 AM
    Friday, October 31, 2014 4:26 PM

All replies

  • What do you mean by "Check ... for check ..."?  Do you mean "change" a value or do you mean "test for" a value?

    To read XML:

    help Select-Xml -Full


    ¯\_(ツ)_/¯

    Friday, October 31, 2014 4:10 PM
  • Example:

    PS >$node=select-xml -Xml $xml -XPath '//Server[@name="Server1"]/*/Group[@name="BiztalkGroup1"]/*/Name[text()="Biztalk1"]'
    PS > $node.Node

    #text
    -----
    Biztalk1


    ¯\_(ツ)_/¯

    • Marked as answer by JimAll Monday, November 3, 2014 2:06 AM
    Friday, October 31, 2014 4:26 PM
  • Hi jrv, I would like to test to see if a server has that value.

    My end goal is to have a checkbox on a form I have created to be ticked if the value exists for Server 1 and antoher Checkbox doing the same for Server2. There are 30 different values in the XML I need to check for.

    e.g: Does BizTalkGroup1 contain 'BizTalk1' for Server1, if true checkbox is ticked if false checkbox is NOT ticked.

    Thanks for your help.

    Monday, November 3, 2014 1:46 AM
  • That is what the code I posted does.


    ¯\_(ツ)_/¯

    Monday, November 3, 2014 1:54 AM
  • Hi, Yep, just used it and did an If statement against $node to check or uncheck the checkbox.

    Thanks again :)

    Monday, November 3, 2014 2:06 AM