简体   繁体   中英

TFS 2017 - [ProjectName].setParameters.xml with powershell

I create a zip Package in my build then i deploy it to a diffrent machine i am pasing few Parameters in my parameters.xml

<?xml version="1.0" encoding="utf-8"?>
 <parameters>   
        <parameter name="WebSiteUserName" description="Please enter the username" defaultvalue="__WebSiteUserName__" tags="">
        <parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/connectionStrings/add[@key='WebSiteUserName']/@value">
        </parameterentry>
        </parameter>
        <parameter name="WebSiteUserPassword" description="Please enter the password" defaultvalue="__UserPassword__" tags="">
        <parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/connectionStrings/add[@key='WebSiteUserPassword']/@value">
        </parameterentry>
        </parameter>
        <parameter name="WebSiteDomain" description="Domiain" defaultvalue="__Domain__" tags="">
        <parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/appSettings/add[@key='WebSiteDomain']/@value">
    </parameterentry>
  </parameter>
</parameters>
    </parameters>

Then i run a powershell script

param(
    [string]$paramsFilePath
)

Write-Verbose -Verbose "Entering script Replace-SetParameters.ps1"
Write-Verbose -Verbose ("Path to SetParametersFile: {0}" -f $paramsFilePath)

# get the environment variables
$vars = Get-ChildItem -path env:*

# read in the setParameters file
$contents = Get-Content -Path $paramsFilePath

# perform a regex replacement
$newContents = "";

$contents  | % {
    $line = $_
    if ($_ -match "__(\w+[\.\w+]*)__") {
        $setting = Get-ChildItem -path env:* | ? { $_.Name -eq $Matches[1]  }
        while ($setting) {
            Write-Verbose -Verbose ("Replacing key {0} with value from environment" -f $setting.Name)
            $line = $_ -replace "__(\w+[\.\w+]*)__", $setting.Value
        }
    }
    $newContents += $line + [Environment]::NewLine
}

Write-Verbose -Verbose "Overwriting SetParameters file with new values"
Set-Content $paramsFilePath -Value $newContents
Write-Verbose " Assigning Parameters"



Write-Verbose -Verbose "Exiting script Replace-SetParameters.ps1"

It will go through the parameters file and replace the parameters token with the enviromental variable.

In my setParamaters.xml file The WebSiteUsername only gets changed

<?xml version="1.0" encoding="utf-8"?>
<parameters>
  <setParameter name="IIS Web Application Name" value="Default Web Site/SomeWebsite" />
  <setParameter name="WebSiteUserName" value="username" />
  <setParameter name="WebSiteUserPassword" value="__UserPassword__" />
  <setParameter name="Web.config Connection String" value="SomeValueForConnection" />
</parameters>

I dont know why this is happening. Any Thoughts?

I think you want to get the password from a TFS release definition environment.

You can´t access hidden fields with

$vars = Get-ChildItem -path env:secretVariable

There is a way but that´s only works in the context of a TFS VNext build or release extension.

In your case the only possibility of access is to set the variable as a script argument like this:

-paramsFilePath $(paramsFilePath) -password $(secretVariable)

In your script add the parameter like

param(
   [string]$paramsFilePath,
   [string]$password 
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM