none
Terminate Suspended SharePoint 2013 Workflows with PowerShell

    Question

  • Greetings,

    I have a large list (form library) with hundreds of 2013 workflows in a Suspended state.  I would like to write a PowerShell script to terminate these workflows and restart for each item.  Can someone point me to an example?

    Thank you!

    Bob

    Monday, March 16, 2015 10:19 PM

Answers

All replies

  • I came up with this sample script. However, I did not test it. But it should give you a head start:

    $web = Get-SPWeb http://yoursite 
    $wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
    $sub = $wfm.GetWorkflowSubscriptionService()
    $list = $web.Lists["YourList"]
    $WF = $sub.EnumerateSubscriptionsByList($list.ID) | Where-Object {$_.Name -eq "YourWorkFlowName"}
    $wfis=$wfm.GetWorkflowInstanceService()
    $items = $list.Items
    foreach($item in $items)
    {
        $itemWorkflowCollection = $item.Workflows
        foreach($itemWorkflow in $itemWorkflowCollection)
        {
            if ($itemWorkflow.InternalState -eq 'Suspended')
            {
                $object = New-Object 'system.collections.generic.dictionary[string,object]'
                $object.Add("WorkflowStart", "StartWorkflow");
                $wfis.StartWorkflowOnListItem($WF, $item.ID, $object)
            }
        }
    }

    See this for more info:

    http://social.technet.microsoft.com/wiki/contents/articles/23850.sharepoint-2013-workflow-management-starting-a-workflow-using-powershell.aspx


    Blog | SharePoint Learnings CodePlex Tools | Export Version History To Excel | Autocomplete Lookup Field

    Tuesday, March 17, 2015 5:09 AM
  • I came up with this sample script. However, I did not test it. But it should give you a head start:

    $web = Get-SPWeb http://yoursite 
    $wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
    $sub = $wfm.GetWorkflowSubscriptionService()
    $list = $web.Lists["YourList"]
    $WF = $sub.EnumerateSubscriptionsByList($list.ID) | Where-Object {$_.Name -eq "YourWorkFlowName"}
    $wfis=$wfm.GetWorkflowInstanceService()
    $items = $list.Items
    foreach($item in $items)
    {
        $itemWorkflowCollection = $item.Workflows
        foreach($itemWorkflow in $itemWorkflowCollection)
        {
            if ($itemWorkflow.InternalState -eq 'Suspended')
            {
                $object = New-Object 'system.collections.generic.dictionary[string,object]'
                $object.Add("WorkflowStart", "StartWorkflow");
                $wfis.StartWorkflowOnListItem($WF, $item.ID, $object)
            }
        }
    }

    See this for more info:

    http://social.technet.microsoft.com/wiki/contents/articles/23850.sharepoint-2013-workflow-management-starting-a-workflow-using-powershell.aspx


    Blog | SharePoint Learnings CodePlex Tools | Export Version History To Excel | Autocomplete Lookup Field

    Hi Nadeem,

    I appreciate it and had something similar myself.  However, this only works with SharePoint 2010 workflows.  The workflows I'm trying to terminate (end) are 2013 workflows.

    Thank you again, I do appreciate it...

    Bob

    Wednesday, March 25, 2015 6:39 PM
  • Hi,

    You can try to use CSOM/RESET API in PowerShell to do it.

    Check if it is useful for you:

    http://sharepoint.stackexchange.com/questions/116691/powershell-to-get-workflow-status-in-a-list-items-of-the-sharepoint-online


    Kally

    Friday, March 27, 2015 3:15 AM
  • A little late but here is some script that worked for me to just resume the workflows

    #https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.workflowservices.workflowinstanceservice_methods.aspx
    
    $sourceWebURL = 'yoursite'
    $sourceListName = 'yourlist'
    $spSourceWeb = Get-SPWeb $sourceWebURL
    $spSourceList = $spSourceWeb.Lists[$sourceListName]
    $items = $spSourceList.getItems()
    
    #-- Getting a Workflow manager object to work with.
    $wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($spSourceweb)
    #-- Getting the subscriptions
    #$sub = $wfm.GetWorkflowSubscriptionService()
    #-- Getting the specific workflow within the list of subscriptions on the specific list. (SP2010 associated workflows basically)
    #$WF = $sub.EnumerateSubscriptionsByList($spSourcelist.ID) | Where-Object {$_.Name -eq "$TargetWorkflow"}
    #-- Getting a Workflow instance in order to perform my commands.
    $wfis=$wfm.GetWorkflowInstanceService()
    
    Foreach($item in $items){
        $wfinstances =  $wfis.EnumerateInstancesForListItem($spSourcelist.ID, $item.ID);
        foreach($instance in $wfInstances)
        {
            if($instance.Status -eq "Suspended")
            {
                 Write-Host "Currently suspended"
                 $wfis.ResumeWorkflow($instance);             
                 Write-Host "Started"
    
            }
        }
    }
    

    • Proposed as answer by ComputerHabit Friday, June 17, 2016 10:49 AM
    Thursday, April 7, 2016 8:01 PM
  • Michael's seemed to work well and helped a lot. Thanks.
    Friday, June 17, 2016 10:49 AM