2

I'm using TFS 2015 Update 2 to create a release. One of my release steps is a "PowerShell on Target Machines" task that I'm using to apply a DSC configuration.

I'd like to use the Script Arguments field to pass in parameters from TFS to the DSC script.

My script looks like this:

Param(
  [string]$data
)

configuration ApplyConfig
{
    Script Backup {
        SetScript = {
            #do some stuff with $data
        }

        TestScript = {
            Write-Output "Print param"
            Write-Output $data

            return $true
        }

        GetScript = {
            return @{"Test" = "test data"}
        }
    }
}

ApplyConfig

The Script Arguments field contains this:

-Destination "$(ApplicationPath)"

However, at this point, $data seems to always be null. How can I get the argument defined in the Script Arguments field into my Script Resource?

TravisEz13
  • 2,263
  • 1
  • 20
  • 28
Devin Goble
  • 2,639
  • 4
  • 30
  • 44

2 Answers2

4

When you reference $data in the TestScript you need the 'using' scope:

TestScript = {
    Write-Output "Print param"
    Write-Output $using:data

   return $true
}

The TestScript executes on a different PowerShell context; 'using' allows you to copy the value of $data across those contexts.

1

My recommendation for flexibility is to declare a configuration hash table in your DSC script and pass parameters in to configure it. My Continuous Delivery with TFS / VSTS – Server Configuration and Application Deployment with Release Management blog post has a complete walkthrough of how to use DSC and Release Management in TFS 2015 Update 2.

Getting the parameters in then becomes a case of declaring your parameters as follows:

param(
  [Parameter(Position=1)]
  [string]$myFirstParameter,
  [Parameter(Position=2)]
  [string]$mySecondParameter
)

and then passing in the value in either directly:

Script Arguments field contains 'myFirstValue' 'mySecondValue'

or better as variables:

Script Arguments field contains $(myFirstValue) $(mySecondValue)

Graham Smith
  • 517
  • 2
  • 10
  • That's a great article! However, I couldn't get that configuration technique to work until I used the $using prefix from the other answer. Any thoughts? – Devin Goble May 11 '16 at 14:49
  • For example: Write-Verbose $using:Node.NodeName -Verbose – Devin Goble May 11 '16 at 14:58
  • You don't use the parameters directly in the DSC configuration - rather you use them via the configuration hash table. My post shows how to do this. – Graham Smith May 12 '16 at 08:58
  • Indeed, and that's how I'm attempting to do it, but I can't seem to even reference the contents of the hash table without the $using prefix. – Devin Goble May 12 '16 at 13:47
  • What error message are you getting when you don't use $using? – Graham Smith May 14 '16 at 05:35
  • The variable ends up being a null value. – Devin Goble May 16 '16 at 19:40
  • What OS version are you using and what version of DSC? My blog post is based on WS 2012 R2 with WMF 5.0 installed. Are you sure you are using the exact same syntax as I use in my post? Might be worth copying the web server example and then carefully stripping everything back to just create a folder or similar. – Graham Smith May 17 '16 at 05:38
  • I believe we have WMF 5.0, but we're on Server 2008 R2. As it is, it works fine now with the $using prefix. I'm not inclined to spend too much trying to exactly match the examples. Thanks! – Devin Goble May 17 '16 at 15:23