SharePoint 2013 – Start/Cancel/Restart workflows from PowerShell

My Problem

Using the SharePoint 2010 workflow engine, I have a list with many list items where my workflows have failed and many workflows are showing an error status. This is a workflow in development so this could happen.  I now fixed my workflow and I need to restart the workflows.

Your Solution

I decided to get PowerShell to do the hard work.

So first I get my list

$web = Get-SPWeb $siteUrl
$list = $web.Lists["MyList"]

Get the workflow manager object:

$wfm = $web.Site.WorkFlowManager

Getting the association and data for the worklow

$association = $list.WorkFlowAssociations |Where { $_.Name -eq "My workflow name"}
$data = $association.AssociationData

now we’re ready to go through all the list items:

foreach ($item in $list.Items)
{
   $wfsexisting = $wfm.GetItemWorkflows($item)
   if ( $wfsexisting -ne $null)
   {
      foreach ($wfexisting in $wfsexisting)
      {
         if (!$wfexisting.IsCompleted -and $wfexisting.StatusText -ne "Canceled" -and $wfexisting.StatusText -ne "Failed on Start" )
         {
            Write-Host "Canceled " $wfexisting.ItemName
            [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wfexisting)
         }
     }
   }
   Write-Host "Start workflow " $item["Title"]
   $wf = $wfm.Startworkflow($item,$association, $data,$true)
   Start-Sleep -Seconds 10
}

When I did this I found that my nintex workflows all started, however there was a problem. The following line in the above code starts the workflow in A-Sync mode.

$wf = $wfm.Startworkflow($item,$association, $data, $true)</blockquote>

No problem it means that the workflows start faster. No not really!

The workflows go first into a Starting state and while the workflows are in a starting state multiple instances are created. Looking and my log output (in the nintex workflow) I can see multiple times the same output generated.

Until the workflow has a context switch (Wait, Approval Task, State change, … and some  other activities) the multiple copies of the same instance will run. This can give many problems.

Solution: Start the workflow with a Wait Activity and the wait activity is run twice, however the activity after the wait is not. Problem solved.

Advertisements

18 thoughts on “SharePoint 2013 – Start/Cancel/Restart workflows from PowerShell

  1. You should specifically state that this is to run ONLY SharePoint 2010 workflows, if the workflows are created as a 2013 workflow (workflow manager) then they will not show up in Workflow Association

    The process for running a 2013 workflow is quite different

    Like

    1. Hi Spanky,
      Can we restart cancelled workflows in SharePoint 2013. Do you have any power shell script for SharePoint 2013.

      Like

  2. Just to clarify, when you say “Solution=start the workflow with a Wait activity”, are you referring to the next line “Start-Sleep -Seconds 10”? Or is there some other command we need to pass to start with a “wait activity”? I think i have the same issue where the multiple copies are colliding and half (or more) of them end up in “Error Occurred” status.

    Like

  3. I’m trying to pin point a workflow on each list item, and if that particular workflow has a StatusText of “Starting” I need to cancel it, and restart it. But I’m having a hard time grabbing the StatusText for a particular workflow. Do you have any suggestions?

    Like

  4. Would the following work:

    if (!$wfexisting.IsCompleted -and $wfexisting.StatusText -eq “Starting” )

    If it doesn’t then try adding the following to display all the status values:

    if (!$wfexisting.IsCompleted )
    {
    Write-Host $wfexisting.StatusText
    }

    Like

    1. This does pull back all workflow StatusText, but not for a specific workflow. It pulls back the requested StatusText for each workflow in the item. ex. i have an item for “Lisa Lee’s Travel Vouchers” but it’s listed 3 times for 3 different workflows. Does that make sense?

      Like

  5. Ok, in the Write-Host in my previous comment can you include
    Write-Host $wfexisting.AssociationId
    If the same AssociationId is returned for each of your workflows on a single item then my guess is that your workflow is starting multiple times.

    https://social.technet.microsoft.com/Forums/sharepoint/en-US/85541784-237a-4d98-8431-6e7258a05081/workflow-running-multiple-times?forum=sharepointgenerallegacy

    How are you starting your workflows? OnChange or OnCreate or both?

    Liked by 1 person

    1. First, thank you for your replies.
      The one I’m targeting is started OnCreate. However, there are others in the list that are triggered OnChange. Sometime in the last week the site collection (not the farm just one site collection) stopped running workflows that were triggered automatically by OnCreate or OnChange. I can manually run them, but if auto triggered they are stuck with “Starting” as the statusText.
      So as a bandaid until I find the root issue, I’m working on finding if a particular workflow is stuck “Starting,” cancel it and rerun it. But I’m having a very hard time targeting a workflow in an item only if it’s StatusText is “Starting”, or anything for that matter 🙂

      Using your script I can see all workflows, but for some reason I can’t pin down the just the one.

      Like

  6. I’ve also seen this in other workflow solution. In general the easiest workaround is to add a wait for X minutes at the beginning of the workflow. SharePoint should then kill off the unwanted workflows without any of them having run.

    Liked by 1 person

Please leave a comment or feedback

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.