locked
Unable to include parameter in REST API calling when trigger queue vNext build RRS feed

  • Question

  • Hi,

    I've powershell script below to trigger queue vNext build. It is working fine without parameter; however, when I include parameter, (CPROVersion is custom predefine build variable), into body, script is throwing exception

    param
    (
        [string]$TeamCollectionURI,
        [string]$TeamProjectName,
        [string]$BuildID,
        [string]$CPROBuildVersion
    )
    cls
    
    #####################################################################
    # Initial the default script exit code
    $exitCode = 1
    
    ####################################################################
    # Invoke REST API to trigger vNext build
    ####################################################################
    
    $body = @{
    definition = @{ id = $BuildID },
    parameters = { CPROVersion = $CPROBuildVersion }
    }
    
    $Uri = $TeamCollectionURI + "/" + $TeamProjectName + "/_apis/build/builds?api-version=2.0"
    $buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body (ConvertTo-Json $body)
    
    ####################################################################
    # Indicate the resulting exit code to the calling process
    if ($exitCode -eq 0)
    {
        "'nERROR: Operation failed with error code $exitCode."
    }
    "'nDone"
    exit $exitCode

    Here is an exception. Please advise.

    At D:\Test\Trigger-vNextBuild.ps1:19 char:33

    + definition = @{ id = $BuildID },

    +                                 ~

    Missing expression after ','.

    At D:\Test\Trigger-vNextBuild.ps1:20 char:1

    + parameters = { CPROVersion = $CPROBuildVersion }

    + ~~~~~~~~~~

    Unexpected token 'parameters' in expression or statement.

    At D:\Test\Trigger-vNextBuild.ps1:19 char:33

    + definition = @{ id = $BuildID },

    +                                 ~

    The hash literal was incomplete.

    At D:\Test\Trigger-vNextBuild.ps1:21 char:1

    + }

    + ~

    Unexpected token '}' in expression or statement.

        + CategoryInfo          : ParserError: (:) [], ParseException

        + FullyQualifiedErrorId : MissingExpressionAfterToken

    Best Regards,

    Andy Pham


    Best Regards, Andy Pham



    • Edited by AndyPham Thursday, April 6, 2017 11:17 PM
    Thursday, April 6, 2017 11:09 PM

Answers

  • Hi AndyPham,

    You could try to use this format for body:

    $body = @"
    {
      "definition": {
        "id": "$BuildID"
      },
      "parameters": "{\"CPROVersion \":\"$($CPROBuildVersion)\"}"
    }
    "@

    And remove ConvertTo-Json for $body

    $buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $body

    Best Regards


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AndyPham Friday, April 7, 2017 5:01 PM
    Friday, April 7, 2017 9:21 AM

All replies

  • Hi AndyPham,

    You could try to use this format for body:

    $body = @"
    {
      "definition": {
        "id": "$BuildID"
      },
      "parameters": "{\"CPROVersion \":\"$($CPROBuildVersion)\"}"
    }
    "@

    And remove ConvertTo-Json for $body

    $buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $body

    Best Regards


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AndyPham Friday, April 7, 2017 5:01 PM
    Friday, April 7, 2017 9:21 AM
  • Hi AndyPham,

    You could try to use this format for body:

    $body = @"
    {
      "definition": {
        "id": "$BuildID"
      },
      "parameters": "{\"CPROVersion \":\"$($CPROBuildVersion)\"}"
    }
    "@

    And remove ConvertTo-Json for $body

    $buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body $body

    Best Regards


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Yes it is working. Thanks Yan

    Best Regards, Andy Pham

    Friday, April 7, 2017 5:01 PM