PowerShell Script To Find Instances Of Running SharePoint Workflow

This is a quick blog post for a fun PowerShell script I got to write today.  My client asked me to make a listing of all instances of a particular workflow currently running on one of our SharePoint farms.  Since we have dozens of SharePoint lists spread out across numerous sub-sites on this farm I decided to tackle the request with PowerShell.

Here is a quick overview of what the scrip accomplishes.  First I get a reference to the site collection in question.  Next I get the workflow template from the name of the workflow I’m checking for.  Next I search all webs within the site collection and all lists within each web.  I filter the lists for any workflow associations with a BaseId matching my workflow template Id while also having at least 1 running instance.  Once I know there are running instances on this list I can then loop through all items in the list checking for workflows that are in the “Running” state.  I then output the SPWeb name, SPList name, and SPListItem name into a delimited output.

On a side note you may notice that I use $($variable.property) in my “write-output…” command.  I do this so that the property values are evaluated first before being passed to the the output stream.  If you attempt $variable.property you’ll most likely end up with a default value for $variable (typically ToString()) followed by “.property” which is not the intended result.

Download Script

Click here for a copy of this script off my SkyDrive.

*Note: As I’m currently very busy with SharePoint Saturday Columbus tasks this script is just in draft form so no recursive traversal of site hierarchy, input parameters, comments, etc.

$workflowNameToCheck = "My Sample Workflow"
$url = "http://SharePointDemo"

$spSite = new-object Microsoft.SharePoint.SPSite($url)
$spWeb = $spSite.OpenWeb()

$workflowBase = $spweb.WorkflowTemplates | where {$_.Name -eq $workflowNameToCheck}

$spWeb.Dispose()

foreach($spWeb in $spSite.AllWebs)
{
    for($i = 0; $i -lt $spWeb.Lists.Count; $i++)
    {
        $spList = $spweb.Lists[$i]
        $assoc = $spList.WorkflowAssociations | where {$_.BaseId -eq $workflowBase.Id.ToString() `
                            -and $_.RunningInstances -gt 0}

    if($assoc -ne $null)
        {
        foreach($item in $spList.Items)
            {
                if(($item.Workflows | where {$_.InternalState -eq "Running"}) -ne $null)
                {
                    write-output "$($spWeb.Name) | $($spList.Title) | $($item.Name)"
                }
            }
        }
    }
    $spWeb.Dispose()
}
$spSite.Dispose()

Conclusion

This script (very much in draft form) checks for running instances of a given workflow within a site collection.  After writing this script today I felt that this is probably a common search for some people so I hope you can glean some useful information from it.  If you find it useful or have any questions feel free to let me know.  Enjoy!

-Frog Out

11 thoughts on “PowerShell Script To Find Instances Of Running SharePoint Workflow

  1. Originally posted on: https://briantjackett.com/archive/2010/07/12/powershell-script-to-find-instances-of-running-sharepoint-workflow.aspx#623536I love this script. Thank you for writing. How would I modify it to list all running workflow instances. I am running into a situation where a user is getting phantom email messages from a task list. The only information I can get is the item link from the email. The item has been deleted. And in the workflow history list I do see an entry for this item. Only I don’t have a name for the workflow just the information that is in the workflow history list:Workflow History Parent Instance, Workflow Association ID, Workflow Template ID, List ID.My goal is to stop whatever workflow is triggering this email and stop it, to stop the annoying emails being sent out every week. Thanks for any help you can give.

    Like

  2. Originally posted on: https://briantjackett.com/archive/2010/07/12/powershell-script-to-find-instances-of-running-sharepoint-workflow.aspx#623685Chilly,If you have the workflow template id you could use that to look up the name of the workflow. On line 7 in the above script you could change it to use the id instead of name.$workflowBase = $spweb.WorkflowTemplates | where {$_.ID -eq “your workflow ID goes here”}Let me know if that works or not.

    Like

  3. Originally posted on: https://briantjackett.com/archive/2010/07/12/powershell-script-to-find-instances-of-running-sharepoint-workflow.aspx#648915Is there a way to use the $assoc and get the id’s of items that have running instances. I am running into a situation, where the $assoc.Running instances are displaying 365 running instances. But none of the items in the list have that association running. SO I think those items are in the recycle bin. But the recyle bin is so large filtering is not working for me. I was hoping there was a way to grab the GUID so I can just delete those specific items. Any help is appreciated.

    Like

    • Anon,

      Thanks for letting me know about broken link. I’ve fixed link. Note that this script only works for SharePoint 2010 / 2013 workflows and not Workflow Manager workflows. I have a script from coworker for that should get the latter. Let me know if interested.

      Brian T. Jackett

      Like

Leave a Reply

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s