locked
Cancel all workflows in entire site collection and subsites RRS feed

  • General discussion

  • Hi All,

    I have below script to use and achieve the title but i am getting error message:

    +++++++++++++++++++++++++++++++++++++++++++++

    Add-pssnapin "Microsoft.SharePoint.PowerShell"
    $site = Get-SPSite "Site_Collection_URL";

    $site.AllWebs | foreach {
      $web = $_;
      $web.AllowUnsafeUpdates = $true;

      # stop list workflows
      $web.Lists | foreach {
        $list = $_;
        $list.Items | foreach {
          $item = $_;
          $item.Workflows | foreach {
             $wf = $_;
             [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
          }
        }
      }

      # stop site workflows
      $web.Workflows | foreach {
        $wf = $_;
        [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
      }    

      $web.AllowUnsafeUpdates = $false;
      $web.Dispose();
    }

    $site.Dispose();

    +++++++++++++++++++++++++++++++++++++

    Error message is:

    An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute..
    At E:\Scripts\Cancel_workflows2.ps1:9 char:3
    +   $web.Lists | foreach {
    +   ~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (Microsoft.Share...on+SPEnumerator:SPEnumerator) [], RuntimeException
        + FullyQualifiedErrorId : BadEnumeration


    MCP & MCTS [WSS 3.0/MOSS/SharePoint 2010] Visit: http://yagyashree.wordpress.com/

    • Changed type Bill_Stewart Wednesday, December 7, 2016 11:02 PM
    • Moved by Bill_Stewart Wednesday, December 7, 2016 11:02 PM This is not "fix/debug/rewrite/add feature(s) to this script I found on the Internet" forum
    Monday, October 31, 2016 12:27 PM

All replies

  • Don't forget to ask your question.

    Before you ask, though, read the following:

    This forum is for scripting questions rather than script requests

    Note that "will someone please debug/fix/rewrite this script for me" is not one of the purposes of this forum.


    -- Bill Stewart [Bill_Stewart]

    Monday, October 31, 2016 2:23 PM
  • You cannot enumerate a dynamic collection and remove elements of the collection.  You must remove them one at a time.  Get the IDs of the workflows into an array then retrieve each workflow by ID and cancel.


    \_(ツ)_/

    Monday, October 31, 2016 6:51 PM