How to Cancel a Workflow that has not completed Successfully

Continuing on this List WF that have not been completed Successfully

Well here is the script to Cancel Workflows that have not been completed.

CLS

$web = Get-SPWeb "http://www.YourSite.com/"
$web.AllowUnsafeUpdates = $true;    

#Your List Name
$list = $web.Lists["YourListName"]
$count = 0

#Loop through all Items in List then loop through all Workflows on each List Items.         
foreach ($listItem in $list.Items) 
{
    foreach ($workflow in $listItem.Workflows) 
    {
        #Disregard Completed Workflows 
        if($Workflow.InternalState -ne "Completed" -and  $Workflow.InternalState -notlike "Cancelled*")
        {
            # Cancel Workflows        
            [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow)   
            Write-Host "Workflow cancelled for : " $Workflow.AssociationId " - " $Workflow.InternalState  " - "  $listitem.ID $listitem.Title $listitem.Title  
        }
    }
}
$web.AllowUnsafeUpdates = $false
$web.Dispose()

Enjoy !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: