Using Release Management vNext templates when you don’t want to use DSC scripts – A better script

A couple of months ago I wrote a post on using PowerShell scripts to deploy web sites in Release Management vNext templates as opposed to DSC. In that post I provided a script to help with the translation of Release Management configuration variables to entries in the [MSDELPOY].setparameters.xml file for web sites.

The code I provided in that post required you to hard code the variables to translate. This quickly become a problem for maintenance. However, there is a simple solution.

If we use a naming convention for our RM configuration variables that map to web.config entries (I chose __NAME__ to be consistent to the old RM Agent based deployment standards) we can let PowerShell do the work.

So the revised script is

 1$VerbosePreference ='Continue' # equiv to -verbose 
 2
 3function Update-ParametersFile  
 4{  
 5    param  
 6    (  
 7        $paramFilePath,  
 8        $paramsToReplace  
 9    )
10
11 
12
13    write-verbose "Updating parameters file '$paramFilePath'" -verbose  
14    $content = get-content $paramFilePath  
15    $paramsToReplace.GetEnumerator() | % {  
16        Write-Verbose "Replacing value for key '$($\_.Name)'" -Verbose  
17        $content = $content.Replace($\_.Name, $\_.Value)  
18    }  
19    set-content -Path $paramFilePath -Value $content
20
21 
22
23}
24
25 
26
27\# the script folder  
28$folder = Split-Path -parent $MyInvocation.MyCommand.Definition  
29write-verbose "Deploying Website '$package' using script in '$folder'" 
30
31 
32
33\# work out the variables to replace using a naming convention
34
35\# we make sure that the value is stored in an array even if it is single item  
36$parameters = @(Get-Variable -include "\_\_\*\_\_" )  
37write-verbose "Discovered replacement parameters that match the convention '\_\_\*\_\_': $($parameters | Out-string)"   
38Update-ParametersFile -paramFilePath "$ApplicationPath$packagePath$package.SetParameters.xml" -paramsToReplace $parameters
39
40 
41
42write-verbose "Calling '$ApplicationPath$packagePath$package.deploy.cmd'"   
43& "$ApplicationPath$packagePath$package.deploy.cmd" /Y  /m:"$PublishUrl" -allowUntrusted /u:"$PublishUser" /p:"$PublishPassword" /a:Basic | Write-Verbose 

Note: This script allow the deployment to a remote IIS server, so useful for Azure Web Sites. If you are running it locally on an IIS server just trim everything after the /Y on the last line

So now I provide

  • $PackagePath – path to our deployment on the deployment VM(relative to the $ApplicationPath local working folder)

  • $Package – name of the MSdeploy package

  • The publish settings you can get from the Azure Portal

  • $PublishUser – The login name

  • $PublishPassword – The login password

  • $PublishUrl  – The URL  e.g. https://[your.site.azure.com]:433/msdeploy.axd

  • $__PARAM1__ –  a value to swap in the web.config

  • $__PARAM2__ –  another value to swap in the web.config

In RM it will look like this.

image

So now you can use a single script for all your web deployments.