sreda, 11. november 2015

Powershell reading XML

Xml document

Content of XML file (part of the XML file) is used in examples below:
<!-- file.xml -->
<config>
    <settings>
        <depot>
            <depotDirectory>\\servername\PerfTestReports</depotDirectory>
            <kibanaDirectory>\\servername\PerformanceTestsResults</kibanaDirectory>
             <authentication login="no">
                <login>servername\username</login>
                <password>password</password>
            </authentication>
        </depot>
        <localdepotDirectory>C:\PowerShell\TempDepot</localdepotDirectory>
        <jmeterhost>client001</jmeterhost>
        <excelpathdll>e:\aps\EPPlus 4.0.4\EPPlus.dll</excelpathdll>
    </settings>
   
    <clean>
        <server login="true">performanceapp1</server>
        <server login="true">performanceapp2</server>
        <server login="true">performanceapp3</server>
        <server login="false">client001</server>
        <server login="false">client002</server>
        <server login="false">client003</server>
        <server login="true">performancebo1</server>
        <server login="true">performancebo2</server>
        <server login="true">performancedb1</server>
        <server login="true">performancedb2</server>
        <counters>
            <performancedb1>DB_Performance</performancedb1>
            <performancedb2>DB_Performance</performancedb2>
            <performanceapp1>cmlt General Counter</performanceapp1>
            <performanceapp2>cmlt General Counter</performanceapp2>
            <performanceapp3>cmlt General Counter</performanceapp3>
            <performancebo1>cmlt General Counter</performancebo1>
            <performancebo2>cmlt General Counter</performancebo2>
            <client001>cmlt General Counter</client001>
            <client002>cmlt General Counter</client002>
            <client003>cmlt General Counter</client00>
        </counters>
    </clean>

   <compile>
            <server id="
performanceapp1" run="local">
                <counter threshold="DotNet.xml">cmlt General Counter</counter>     

        </server>
        <server id="
performanceapp2" run="local">
            <counter threshold="DotNet.xml">cmlt General Counter</counter>        </server>

     </compile>   
</config>


Read XML

For creating script is used Powershell ISE, because it uses Intelisense, which is not good as in visual studio, but very helpful to find searched method .

XML file is saved in current folder. Load complete XML file into one variable:

$configpath = "."
$configfile = [xml] (Get-Content $configpath\config.xml)

Element

Read value of one element from variable $configfile with the Xpath "config/settings/depot/depotDirectory". To get text of the element is used method ".InnerText":

$depotDirectory  = $configfile.SelectSingleNode("config/settings/depot/depotDirectory").InnerText

Attribute

To read attribute of specific element is used method "GetAtribute". First element is read with method 'SelectSingleNode' and saved in variable '$authenticationnode'. Vairable '$authenticationnode' is used methon 'GetAttribute' to get value of the attribute:

$authenticationnode  = $configfile.SelectSingleNode("/config/settings/depot/authentication")
$global:login = $authenticationnode.GetAttribute("login")


Nodes 

Sometimes we need to read a repetitive elements in XML. In out case we are going to read remote computes on which we will clean log, make IIS reset, to get version of application under test. To read we use next command:


$arrayNodes = @($configfile.config.clean.server.InnerText)

We get array of child elements of parent element server. Iteration is done with 'foreach' element in node, as it is shown below:
 
foreach($xmlnode in $xmlnodes)
{
     $login = $xmlnode.GetAttribute("login").ToLower()
     $xmlnode.InnerText
}


Or you can use:

Select-Xml -Xml $xdoc -Xpath "//compile/server[@id=`'$hostForCompile`']/counter" `
| foreach { $thresHolds  += $_.node.threshold ; $counterNames += $_.node.InnerXML}


  Summary

 Using XML document in powershell is very easy. Once you reach how to read XML file it make sense to drive complete powershell automation with XML. In my project XML is used for:
- powershell Test runner script is driven by XML
- to connect to remote computers
- defines whch perfomance countesd must be started on remote computes
- copy file from one location to another
- to create PAL reports from performance counters
- to create a word document
- read file and seved it in to variable
.......

With powershell nothing is imposibile.

Ni komentarjev:

Objavite komentar