Search code examples
powershelltfstfsbuildstart-job

Powershell Quoted variables parameters in a job


$buildDef = "Service.xxxx"
$buildDefFull="MyProject/$buildDef"

Start-Job -Name 'Service1' -ScriptBlock { tfsbuild start /collection:"http://yyyy:8080/tfs/DefaultCollection"  /builddefinition:"$buildDefFull" }

i get this error:

Option builddefinition requires a value.
    + CategoryInfo          : NotSpecified: (Option builddefinition requires a value.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
    + PSComputerName        : localhost

i can't seem to get tfsbuild to accept the parameter in a start job... it actually runs fine if i just do the tfsbuild part with no job.

Any ideas how i'm supposed to pass that value? tx


Solution

  • The $buildDefFull variable is outside the scope of the scriptblock.

    You have 2 options:

    PowerShell 3+

    Use the Using scope modifier:

    $buildDef = "Service.xxxx"
    $buildDefFull="MyProject/$buildDef"
    
    Start-Job -Name 'Service1' -ScriptBlock { tfsbuild start /collection:"http://yyyy:8080/tfs/DefaultCollection"  /builddefinition:"$Using:buildDefFull" }
    

    Any Version

    Define and pass parameters to the scriptblock:

    $buildDef = "Service.xxxx"
    $buildDefFull="MyProject/$buildDef"
    
    Start-Job -Name 'Service1' -ScriptBlock { param($bdf) tfsbuild start /collection:"http://yyyy:8080/tfs/DefaultCollection"  /builddefinition:"$bdf" } -ArgumentList $buildDefFull