skip to Main Content

I need your help, I’m trying to create a powershell script that will create a task directly in Azure Devops (Azure Boards -> Work Items).

The problem is that after much research, I haven’t found the corresponding module to do this, nor the method to connect to the module, if one exists.

I had the idea of generating a token and using it to connect.

For the moment, I’ve tried to use the generate token to connect, but it’s blocked because I don’t know the module for connecting directly to Azure DevOps.

2

Answers


  1. You can use the Azure DevOps API, and if it’s running as a Powershell script in the build you should use the predefined variables. Example:

    $organizationUrl = $(System.CollectionUri)  
    $projectName = $(System.TeamProject )  
    $pat = $(System.AccessToken)
    $apiVersion = "6.0"
    $type = "User Story"
    # User Story JSON Payload
        $witFieldsValues = @"
        [
          {
            "op": "add",
            "path": "/fields/System.Title",
            "value": "Test"
          }
        ]
        "@
            
    # Azure DevOps REST API URL for creating a work item (user story)
    $uri="$organizationUrl/$projectName/_apis/wit/workitems/"+"$"+"$Type"+"?bypassRules=true&api-version=$apiVersion"
        
    $creationResponse = Invoke-WebRequest -Uri $uri -Method POST -Body $witFieldsValues -ContentType "application/json-patch+json" -Headers @{
                Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) }  
    
    Login or Signup to reply.
  2. You can reference the PowerShell script sample below to call Azure DevOps REST API to create a Task type work item.

    # Convert PAT to a Base64 string.
    $pat = "{Personal Access Token}"
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
    
    # Set request headers.
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
    $headers.Add("Content-Type", "application/json-patch+json")
    
    $uri = "https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/`$Task?api-version=7.0"
    
    # Set request body and convert it to JSON type.
    $body = @(
        @{
            op = 'add'
            path = '/fields/System.Title'
            value = '{Title}'  # Set the title
        }
        @{
            op = 'add'
            path = '/fields/System.AreaPath'
            value = '{Area Path}'  # Set the Area Path, e.g.: MyProj\MyArea\SubArea
        }
        @{
            op = 'add'
            path = '/fields/System.IterationPath'
            value = '{Iteration Path}'  # Set the Iteration Path, e.g.: MyProj\MyIteration\SubIteration
        }
    ) | ConvertTo-Json -Depth 10
    
    # Execute Invoke-RestMethod cmdlet to call the API and convert its response to JSON for better readable.
    Invoke-RestMethod -Method POST -Uri $uri -Headers $headers -Body $body | ConvertTo-Json -Depth 10
    

    The value of variable "$body" (request body) also can directly set to be a JSON string like as below.

    $body = '[
        {
            "op": "add",
            "path": "/fields/System.Title",
            "value": "{Title}"
        },
        {
            "op": "add",
            "path": "/fields/System.AreaPath",
            "value": "{Area Path}"
        },
        {
            "op": "add",
            "path": "/fields/System.IterationPath",
            "value": "{Iteration Path}"
        }
    ]'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search