none
Powershell - stop all running workflows

    Question

  • Hi,

    I need to write a PowerShell script that will make sure that all Workflows running anywhere in a Site Collection are stopped.

    Does anyone have a script that does this or can point me in the right direction?

    Thanks!

    Thursday, June 16, 2011 7:56 AM

Answers

  • Just an idea to get you started...

    If you can read C# skills you convert the C# code here into a PowerShell script: http://social.msdn.microsoft.com/Forums/en-US/sharepointworkflow/thread/e5fc79f8-ca3a-421a-ab8a-78fcb3365b46

    (Most anything you can do in SharePoint with C# you can also do with PowerShell.)

     

     


    Mike Smith TechTrainingNotes.blogspot.com
    Sunday, June 19, 2011 1:24 AM
  • I haven't tested my code, but this should also helps (don't forget that in SharePoint 2010 we have both site workflows and lits workflows)

                using (SPSite oSite = new SPSite("<your url>"))
                {
                    foreach (SPWeb oWeb in oSite.AllWebs)
                    {
                       
                        oWeb.AllowUnsafeUpdates = true;


                        // stop list workflows
                        foreach (SPList list in oWeb.Lists)
                        {
                            foreach (SPListItem oItem in list.Items)
                            {
                                foreach (SPWorkflow workflow in oItem.Workflows)
                                {
                                    SPWorkflowManager.CancelWorkflow(workflow);
                                }
                            }
                        }


                        // stop site workflows
                        foreach (SPWorkflow workflow in oWeb.Workflows)
                        {
                            SPWorkflowManager.CancelWorkflow(workflow);
                        }

                        oWeb.AllowUnsafeUpdates = false;
                        oWeb.Dispose();
                    }
                }
            }
        }

     


    Serge Luca; MVP blog: http://sergeluca.spaces.live.com Devoteam Belgium. http://twitter.com/sergeluca
    Sunday, June 19, 2011 10:41 AM

All replies

  • Just an idea to get you started...

    If you can read C# skills you convert the C# code here into a PowerShell script: http://social.msdn.microsoft.com/Forums/en-US/sharepointworkflow/thread/e5fc79f8-ca3a-421a-ab8a-78fcb3365b46

    (Most anything you can do in SharePoint with C# you can also do with PowerShell.)

     

     


    Mike Smith TechTrainingNotes.blogspot.com
    Sunday, June 19, 2011 1:24 AM
  • I haven't tested my code, but this should also helps (don't forget that in SharePoint 2010 we have both site workflows and lits workflows)

                using (SPSite oSite = new SPSite("<your url>"))
                {
                    foreach (SPWeb oWeb in oSite.AllWebs)
                    {
                       
                        oWeb.AllowUnsafeUpdates = true;


                        // stop list workflows
                        foreach (SPList list in oWeb.Lists)
                        {
                            foreach (SPListItem oItem in list.Items)
                            {
                                foreach (SPWorkflow workflow in oItem.Workflows)
                                {
                                    SPWorkflowManager.CancelWorkflow(workflow);
                                }
                            }
                        }


                        // stop site workflows
                        foreach (SPWorkflow workflow in oWeb.Workflows)
                        {
                            SPWorkflowManager.CancelWorkflow(workflow);
                        }

                        oWeb.AllowUnsafeUpdates = false;
                        oWeb.Dispose();
                    }
                }
            }
        }

     


    Serge Luca; MVP blog: http://sergeluca.spaces.live.com Devoteam Belgium. http://twitter.com/sergeluca
    Sunday, June 19, 2011 10:41 AM
  • Thanks a lot!

    I didn't see this until now (haven't gotten an reminder e-mail). However, I figured it out and did it exactly the way you proposed!

    Thanks again!

    Tuesday, June 21, 2011 11:23 AM