Tuesday, August 23, 2016

Calling AgilePoint web services with PowerShell

AgilePoint has 2 main APIs that are really handy - Workflow.asmx and Admin.asmx. You could also access it via REST.
Today I'm going to show an example of how you could batch reassign tasks to users by making use of the workflow web service.

The services could be found here.
  • Workflow API - http://[qualified machine name]:[port]/[AgilePoint virtual directory]/workflow.asmx?WSDL
  • Administration API - http://[qualified machine name]:[port]/[AgilePoint virtual directory]/admin.asmx?WSDL

With PowerShell here is a very straightforward example of a classic web service that accepts simple data types.
Using a CSV file that contains user account mapping from DOMAIN1\user to NEWDOMAIN\user, first declare the variables you need. Here is a sample CSV

oldlogin,newlogin
DOMAIN1\user1,NEWDOMAIN\user1new
DOMAIN1\user2,NEWDOMAIN\user2new

 #****************************************************  
 # Variables  
 #****************************************************  
 $username = "DOMAIN\admin"  
 $password = "Password"  
 $wsWorkflowUrl = "http://agilepointserver:8000/AgilePointServer/Workflow.asmx?WSDL"  
 $wsAdminUrl = "http://agilepointserver:8000/AgilePointServer/Admin.asmx?WSDL"  
 $csvFile = "Users.csv"  


Then, create the credentials object and connect to the webservice. PowerShell has a built-in WebServiceProxy class which is really handy.

 #****************************************************  
 # Credentials and proxy to connect to web service  
 #****************************************************  
 $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))  
 $workflowProxy = New-WebServiceProxy -Uri $wsWorkflowUrl -Credential $cred  
 $adminProxy = New-WebServiceProxy -Uri $wsAdminUrl -Credential $cred  

In order to consume the service, here is a function to search for tasks assigned to a user, list all tasks, and reassign it to the new (migrated) user. It makes use of GetWorkListByUserID and ReassignWorkItem services.

 Function Reassign-AgilePointTasks ($originalUser, $migratedUser)   
 {  
   $response = $workflowProxy.GetWorkListByUserID($originalUser, "Assigned;Overdue")  
   ForEach ($item in $response) {  
     # Reassigns each task to the migrated user account in AgilePoint.  
     Write-Host "Reassigning task $($item.Name) $($item.WorkItemID)" -foregroundcolor "Yellow"  
     $reassign = $workflowProxy.ReassignWorkItem($item.WorkItemID, $migratedUser)  
     $textLogFileOK = "Reassignment to $($reassign.UserID) complete. $($reassign.Error)"  
        Add-Content -path $LogFileOK -value $textLogFileOK  
     Write-Host "Reassignment to $($reassign.UserID) complete. $($reassign.Error)" -backgroundcolor "Green"  
   }    
 }  

Finally, put all the pieces together by looping through the csv file, and reassigning the tasks user by user:

 #****************************************************  
 # Start Bulk AgilePoint migration  
 #****************************************************  
 Import-Csv $csvFile | ForEach-Object{  
         $originalUser = $_.oldlogin  
         $migratedUser = $_.newlogin  
         Reassign-AgilePointTasks -originalUser $originalUser -migratedUser $migratedUser  
         Write-Host "Migrated User " + $originalUser + " to " + $migratedUser  
}   

And we're done!

No comments:

Related Posts Plugin for WordPress, Blogger...