Sunday, June 22, 2014

Cancel Workflow Instances using powershell script

Hi All,

Recently, I worked on the requirement to terminate the running workflow instances associtated to the specific list using powershell script.

I used WorkflowManager and then Cancel method with passiing the workflow instance class,

Here  I terminated the workflows for which the workflow status is running.

here is the script.

 
#Site URL
$siteurl=Read-Host "Enter Site URL"
$web = Get-SPWeb $siteurl
$web.AllowUnsafeUpdates = $true;    

#List Name
$listname=Read-Host "Enter list Name"
$list = $web.Lists["$listname"];

if($list -ne $null)
{
# Iterate through all Items in List and all Workflows on Items.         
foreach ($item in $list.Items) {
foreach ($wf in $item.Workflows) {

#Cancel Workflows      

if($wf.InternalState - eq 'Running')
{  
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);      
}
}
}
$web.Dispose();
}
else
{
 write-host "List does not exist"
}

No comments: