Azure DevOps(ADO) is a Microsoft product that provides version control, reporting, requirements management, project management, automated builds, lab management, testing and release management capabilities. It covers the entire application life-cycle, and enables DevOps capabilities.
Today I am going to share a script block to create ADO Work Item, specially Task using Powershell. My initial goal was to create Azure DevOps Tasks based on certain feeds from our ITSM system. While ITSM system is our main system to manage and document all activities, Development and Engineering teams use Azure DevOps to manage and track all their activities using Work Items in Azure DevOps(ADO). So, we need to create an automation to create a record in ADO corresponding to the ITSM Ticket.
There are several ways to setup this automation. You can use Connectors, Azure Logic App etc.
Here, I am going to create an ADO Task from an Incident Ticket created in the ITSM system. Check my previous blog on how to create ServiceNow Incident using Powershell. So, I am here not going to explain how to get Incident details from ITSM system.
For now, let’s consider following fields to be documented or added in the ADO task we will create.
Incident Number | Incident identifier, example INC123456 |
Requester | Requester id or name who entered/requested the incident |
Short Description | Short description of the issue/incident |
Description | Detailed description of the issue/incident |
Category | Category of the incident,example, Infrastructure |
Sub-Category | Sub-Category of the incident for the given category,example, Windows Server |
Impact | Impact or priority of the issue/incident. It could be any number between 1 and 4. Where 1 being highest priority; 4 being lowest |
You may have different field requirements.Feel free to update based on your requirements.
Here is the mapping between Incident field data and what we will populate in the ADO Task.
Title | Incident Number & Short Description |
Comment | Incident Number, Requester, Category, Sub-Category, Description |
Priority | Impact |
Tag | Category,Sub-Category |
Before we start, as prerequisite, we need a Personal Access Token(PAT) with correct set of permissions to authenticate and authorize on the ADO API. You may use SSH Key or an alternate method to generate credentials.
To generate a PAT, click on the user icon on the right-side top corner of the ADO website as shown in the picture and click on the ellipse and then click on “User settings” :
Now, within User settings, select Personal Access Tokens from the list as shown in the screenshot :
In the next screen, click on New Token to generate a new Token. Follow the on-screen instructions to complete the PAT creation process. Make sure to copy the PAT in safe place as you will not be able to retrieve it once the window is closed. For permission, make sure, you select all required permissions you need to perform the task.
Finally, we are now ready to start writing the script. Let’s define the ADO Task REST API URL and also generate the Authorization Header using the PAT generated in previous step. The ADO API URL uses your ADO Organization name and the Project name as part to construct the URL path.
$pacToken = "abcdesomesamplejunkentriesdontuseititwillnotwork"
$organizationName = "MyTestOrg"
$projectName = "MyTestProject"
$adoHeader = @{Authorization=("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$pacToken))))}
$adoTaskUri = "https://dev.azure.com/$organizationName/$projectName/_apis/wit/workitems/`$Task?api-version=5.1"
Now, let’s create the main script block to create ADO Task. I am assuming you already collected all required ITSM Incident information to use while updating Task details.
#Data retrieved from Incident
$number = "INC12345" #Enter your incident number here
$requester = "Arindam Hazra" #Enter Requester name, I will share a separate script on how to retrieve these information from ServiceNow using Powershell
$cat = "Infrastructure"
$subcat = "Windows"
$shortDescription = "Some issue reported on the Windows Server - abc"
$description = "Some issue reported on the Windows Server - abc. Server went down at time - 12:05 AM due to Hardware Failure. Please check the issue"
#Define ADO variables
$title = "$number : $shortDescription"
$comments = "Incident Number : $number <br />"
$comments += "Opened By : $requester <br />"
$comments += "Category : $cat <br />"
$comments += "Sub-Category : $subcat <br />"
$comments += "Description : <br />"
$comments += $description
$comments = $comments.replace('"',"'")
$impact = 2
#Generate JSON body to make the REST API call
$body="[
{
`"op`": `"add`",
`"path`": `"/fields/System.Title`",
`"value`": `"$title`"
},
{
`"op`": `"add`",
`"path`": `"/fields/System.Description`",
`"value`": `"$comments`"
},
{
`"op`": `"add`",
`"path`": `"/fields/Microsoft.VSTS.Common.Priority`",
`"value`": `"$impact`"
},
{
`"op`": `"add`",
`"path`": `"/fields/System.Tags`",
`"value`": `"$cat;$subcat`"
}
]"
#REST API call
Invoke-RestMethod -Uri $adoTaskUri -ContentType "application/json-patch+json" -Body $body -headers $adoHeader -Method POST
Once the script runs successfully, you will receive response like below :
Now, check in Azure DevOps. You should see a Task has been created in the Project name you provided :
Microsoft provides a list of documentations on different REST API actions you can perform on Azure DevOps:
https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items?view=azure-devops-rest-5.1
Make sure you have selected the correct version of the API.
I hope this blog will be helpful to you!
[…] my previous post I talked about creating Azure DevOps(ADO) Task from ITSM Incident. I am going to talk about another […]
[…] your script. You also need a Personal Access Token(PA Token) ready with necessary access. Refer my previous post if you need any help on creating Token. If you have multiple organizations, update this script to […]