Welcome to Day 88 of the “100 Days of DevOps with PowerShell”! For background on our goals in this series, see Announcing the “100 Days of Devops with PowerShell” Series here at SCC.
On Day 28 – Let’s Integrate SCSM and TFS, I showed how to integrate SCSM and TFS, by creating a Task TFS work item when a certain type of Incident was created in TFS. But what if, rather than triggering this process via a certain work item type, I wanted to be able to create a work item in TFS in an ad-hoc fashion? To do that, I need to create a custom task in SCSM that takes the selected SCSM Incident and creates a task in TFS. I’ll be able to make use of the script from Day 28.
First, let’s take the script and make a couple of modifications:
param($Id, $Title, $Description)
if ((Get-PSSnapIn -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null)
[string] $tfsCollectionUrl = "http://r2-09-tfs:8080/tfs/DefaultCollection";
$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl);
#Get Work Item Store object
$ws = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
#Get Team Project
$proj = $ws.Projects["TestTeamProject"]
#Get the Work Item Type to create
$wit = $proj.WorkItemTypes["Task"]
#Create a new work item of that type
$workitem = $wit.NewWorkItem()
#Set work item properties
$workItem.Title = "$Title"
$workItem.Description = "$Id : $Description"
$workitem.AreaPath = "TestTeamProject"
$workitem.IterationPath = "TestTeamProject"
#Save work item
Write-Host "TFS Task Work Item created: " $workItem.Id
I’ve made two modifications to the script. The first is to add a Params statement at the top of the script. This allows me to pass in the SCSM work item ID, Title, and Description:
param($Id, $Title, $Description)
I also added Write-Host statement at the end of the script to write out the ID of the TFS Task that is created. This allows me to capture that information and put it into the Incident Action Log in SCSM. I’m going to take the above script, and save it into c:\ws\PowerShell as CreateTFSTask.ps1.
To get started creating a custom task, open the Service Manager console. Select the Library workspace, then right-click on the Tasks folder and select Create Task from the context menu.
This will open the Create a Task Wizard. On the Before You Begin tab of the wizard, click Next.
On the General tab, enter Create Task in TFS as the name of the task. Under Target class, click the ellipse to open the selection box, and then select Incident. Finally, select a management pack to store the custom task, then click Next.
The Categories tab is optional, and allows you to specify on which different menus and context menus the task will appear. I only want my task to appear when I’m working directly with a specific incident, so I am not going to select anything on this tab. Click Next.
The Command Line tab is where I create the actual task. What I want to do is execute my PowerShell script that I created earlier. In the Full path to command, I enter the path to the PowerShell EXE in windows. Then I need to specify the parameters for that. The first parameter is the script that I want to execute, in this case CreateTFSTask.ps1. My script is expecting three parameters: Id, Title, and Description, from the selected Incident. To access those values, I need to know the XPath expression to those values. Luckily, I can click the Insert Property button and select the values, and SCSM will create the XPath expression for me.
I changed the working directory to be the directory where my PowerShell script is located. Finally I checked both boxes at the bottom of the page. This will make the task both display the output while it is running, so I can see what is happening, as well as log in the action log that the task was executed, and the results. Click the Next button.
Here is a more detailed look at the Parameters field:
&'.\CreateTFSTask.ps1' '"$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Id$"' '"$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Title$"' '"$Context/Property[Type='CustomSystem_WorkItem_Library!System.WorkItem']/Description$"'
Review the information on the Summary tab, then click Create to create the task.
The task should create successfully. Click the Close button to close the wizard.
Now I need to test my new task. First thing to do is open up an incident. I now see a new task on the right-hand side, Create Task in TFS.
To execute the task, I just click it. The following window will open, showing me the output from the task. In this case, a new work item, #49, has been created for me in TFS.
If I refresh the incident, and go to the action log, I can see that the task execution has been logged for me. This is nice, as it provides me both a visible record that the task ran, as well as a historical record of the task results.
And if I go into Team Explorer and pull up the work item #49, I can see if is the work item created by our SCSM task.
Having work items created for you automatically in TFS is nice, but, depending on your process, your end users might need the ability to send specific items over to TFS, that don’t match your criteria. Using a custom task, as outlined above, is one way to provide that ability.