0

I have a xml file like that:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
    <serviceUrlFiles>
        <file>Licensed/Mobile/android/Phone/res/xml/preferences.xml</file>
        <file>Licensed/Mobile/iOS/FIService/Resources/Settings.bundle/Root.plist</file>
        <file>Licensed/Mobile/javascript/src/shared/config/settings.json</file>
            <file>Framework/Mobile/iOS/FIToolkit/FIToolkit/NativeSOAPService/MCProcessSOAPRequest.m</file>
    </serviceUrlFiles>
    <builds>
        <build type="internal.qa">
            <serviceUrl regexp="[a-zA-Z0-9_\-\.]*://[a-zA-Z0-9_\.-]*/RichChannel[0-9a-zA-Z]*/Service.svc">https://10.41.69.77/RichchannelWS/service.svc</serviceUrl>
        </build>
        <build type="client.qa">
            <serviceUrl regexp="[a-zA-Z0-9_\-\.]*://[a-zA-Z0-9_\.-]*/RichChannel[0-9a-zA-Z]*/Service.svc">https://10.41.69.77/RichchannelWS/service.svc</serviceUrl>
        </build>
        <build type="stage">
            <serviceUrl regexp="[a-zA-Z0-9_\-\.]*://[a-zA-Z0-9_\.-]*/RichChannel[0-9a-zA-Z]*/Service.svc">https://10.41.69.77/RichchannelWS/service.svc</serviceUrl>
        </build>
        <build type="release">
            <serviceUrl regexp="[a-zA-Z0-9_\-\.]*://[a-zA-Z0-9_\.-]*/RichChannel[0-9a-zA-Z]*/Service.svc">https://10.41.69.77/RichchannelWS/service.svc</serviceUrl>
        </build>
    </builds>
</settings>

What I want to do is to use powershell to get the content of the serviceUrl where build type="stage".

I wrote a piece of powershell code to do it:

$apktype = $args[0]
$xml = [xml](Get-Content .\Licensed\Mobile\JenkinsBuildScripts\prebuildsetting_url_ver.xml)
$newUrl = $xml.settings.builds.build | ? {$_.type -eq $apktype} | select serviceUrl

Then, I run the command in powershell and pass the parameter:

test.ps1 stage

It is just a simple code. However, when I tried to echo $newUrl to see its value, it does not return the value of serviceUrl under the build tag with type="stage".

Anyone has some idea? I read lots of sample on the internet but can't see any different.

Thanks in advance.

2 Answers 2

2

I believe this is what you are looking for:

$newUrl = ($xml.settings.builds.build | ? {$_.type -eq $apktype} |select -expand serviceUrl).innerText

Even after you get the <serviceUrl> node, what you really want is the innerText out of that node.

Remember also that the variable $newUrl will not be available outside of the test.ps1 script. You can either scope the variable, or you can run the script using the "dot-source" operator:

. test.ps1 stage

Notice the . at the front.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, it works. Thanks a lot. I also found out another way to do it: $build = $xml.settings.builds.build | ? {$_.type -eq $apktype} $serviceUrl = $build.serviceUrl.'#text'
1

I would suggest using Select-Xml instead:

function Get-ServiceUrl {
param (
    [string]$BuildType = 'stage',
    [string]$Path = '.\Licensed\Mobile\JenkinsBuildScripts\prebuildsetting_url_ver.xml'
)

    $Nodes = Select-Xml -Path $Path -XPath "//build[@type = '$BuildType']/serviceUrl"
    foreach ($Node in $Nodes) {
        $Node.Node.InnerText
    }

}

Get-ServiceUrl
Get-ServiceUrl -BuildType release

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.