Nintex

Nintex Connect
Metalogix
Metalogix
BlueStripe
BlueStripe

Training - Combined Knowledge

Training - TekDog Inc

PowerShell - Cancel all Running Workflows

  • RSS
  • Add To My MSN
  • Add To Windows Live
  • Add To My Yahoo
  • Add To Google

Statistics

  • Entries (119)
  • Comments (312)
Posted by Vadim Tabakman Thursday, May 10, 2012 2:11:00 PM Categories: Nintex Workflow PowerShell SharePoint

WARNING

DO NOT run this on a production system unless absolutely necessary.  It iterates through all sites, lists, items and workflows.  I have not tested this for performance and system impact.

There have been some requests on how to cancel all running workflows. I've looked online and have found a number of C# and PowerShell scripts, but each has resulted in issues of one sort or another.

So I've created the script below.  It's not pretty and probably can be optomized, but it works and I'm happy with it.

What it does, is iterate through site, through each list in the site, each item in the list and then each workflow and if it's not "Completed" and not "Cancelled", then it cancels the workflow.

I have found that if you cancel a workflow that is completed or already cancelled, you'll find a mesasge in the workflow history that the workflow has been cancelled, even though it wasn't running.  Also, if it's a Nintex Workflow, you'll then end up getting an email telling you the workflow was cancelled, when you shouldn't have even tried to cancel it.

So the below script works for list and site workflows.

Why not use the online scripts?

There are 2 issues I had with scripts I found online.

  1. Scripts were iterating through each web using a ForEach, and that would crash the script since it affects the foreach collection of webs.
  2. Scripts were cancelling workflows that weren't running.

Improvements that could be made

It'd be nice to turn this into a ps1 file that takes arguments.  So you can pass in the url to the parent site and possibly a listname, item index or workflowname to make this a little smarter.  But for now, this works and you can modify it as you see fit.

Below is the PowerShell Script

It's hardcoded to localhost but you can change it your own site, or a different url.

$site = Get-SPSite "http://localhost"
if($site)
{
  $spWebCollection = $site.AllWebs;
  $i = 0;
  $iWebCount = $spWebCollection.Count;

  do
  {
    Write-Host $i;

    $web = $spWebCollection[$i];
    $spListCollection = $web.Lists;
    $iListCount = $spListCollection.Count;

    $j = 0;
    do
    {
      $list = $spListCollection[$j];
      $spItemCollection = $list.Items;
      $iItemCount = $spItemCollection.Count;
      $k = 0;

      do
      {
        $item = $spItemCollection[$k];
        $spWorkflowCollection = $item.Workflows;
        if($spWorkflowCollection)
        {
          $iWorkflowCount = $spWorkflowCollection.Count;
          $l = 0;
 
          do
          {
            $workflow = $spWorkflowCollection[$l];
            if($workflow)
            {
              if($workflow.InternalState -ne 'Completed' -and $workflow.InternalState -ne 'Cancelled')
              {
                [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow);
              }
            }
            $l++;
          }
          while ($l -lt $iWorkflowCount)
        }
        $k++;
      }
      while ($k -lt $iItemCount)
      $j++;
    }
    while ($j -lt $iListCount)
   
    # Site Workflows
    $spWorkflowCollection = $web.Workflows;
    if($spWorkflowCollection)
    {
      $iWorkflowCount = $spWorkflowCollection.Count;
      $w = 0;

      do
      {
        $workflow = $spWorkflowCollection[$w];
        if($workflow)
        {
          if($workflow.InternalState -ne 'Completed' -and $workflow.InternalState -ne 'Cancelled')
          {
            [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow);
          }
        }
        $w++;
      }
      while ($w -lt $iWorkflowCount)
    }
    $web.Dispose();
    $i++;
  }
  while ($i -lt $iWebCount)

  $site.Dispose()
}

Copyright Vadim Tabakman

re: PowerShell - Cancel all Running Workflows

Tuesday, April 30, 2013 12:43:51 PM

Awesome.  Glad to be of service :)

re: PowerShell - Cancel all Running Workflows

Tuesday, April 30, 2013 12:41:32 PM Randy Hinders

You are correct (as always).. that worked :)

Thanks again,

Randy

re: PowerShell - Cancel all Running Workflows

Tuesday, April 30, 2013 12:03:33 PM

Hi Randy,

It's possible the workflow isn't actually running, but the NW database is out of sync with SharePoint in this case.

You should be able to resolve that by running :

nwadmin -o SyncTerminatedWorkflow -url "siteurl"

If you put a -preScan on the end of that command line, it'll tell how many items it has found out of sync. Then rerun it with out the -preScan and it should fix it.

cheers,

Vadim

re: PowerShell - Cancel all Running Workflows

Tuesday, April 30, 2013 12:00:24 PM Randy Hinders

Vadim,

As always beautiful post!

But I have noticed a bug with SharePoint.... If a workflow has been deleted (under workflow settings not Nintex) and an item still has that deleted workflow running, then this script will not cancel the workflow.  It remains running in an orphaned state.

What do you think about this snafu?

Randy

re: PowerShell - Cancel all Running Workflows

Friday, April 19, 2013 2:30:36 PM

Hahah Thanks Josh.  I really appreciate that!!!

Let me know if there's something you think would interesting to post about.

re: PowerShell - Cancel all Running Workflows

Friday, April 19, 2013 2:29:04 PM

How come everytime I'm Googling something, I find the best answer on your blog? Because you have all the answers, of course! :P

Thanks as usual my friend, this is just what we are looking for!