Home Contact

The Frog Pond of Technology

Ripples of Knowledge for SharePoint and Other .Net Technologies

News

My blog has moved to WordPress at BrianTJackett.com. Any pages will automatically redirect to the new address in 7 seconds. Please excuse an issues during this migration.

 Subscribe to this blog


About Me

Name:
Brian T. Jackett
Location:
Columbus, OH
Company:
Microsoft

Find me on...

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

Syndication:

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


Monday, July 12, 2010 10:31 PM

Feedback

# re: PowerShell Script To Find Instances Of Running SharePoint Workflow

Thank you...
its a very useful blog.. 9/22/2011 12:07 AM | Neha Jaiswal

# re: PowerShell Script To Find Instances Of Running SharePoint Workflow

Hi,

there is a software to show all instances and associations at the farm level, HarePoint Workflow Monitor:

http://www.harepoint.com/Products/HarePointWorkflowMonitor/Default.aspx

WBR, Alexander 2/24/2012 8:52 AM | Alexander Gorlach

# re: PowerShell Script To Find Instances Of Running SharePoint Workflow

Need to check workflow associations with the list content types as well. 4/18/2012 3:35 PM | Gitendra Malla

# re: PowerShell Script To Find Instances Of Running SharePoint Workflow

Gitendra,

Good point. When I wrote this script the customer I was working with did not have any workflows on content types. This script could be modified to include content types fairly easily though if you wish. I may try that if time permits in the future.
4/19/2012 1:17 PM | Brian T. Jackett

# re: PowerShell Script To Find Instances Of Running SharePoint Workflow

I 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. 1/7/2013 3:22 PM | Chilly

# re: PowerShell Script To Find Instances Of Running SharePoint Workflow

Chilly,

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.
1/10/2013 10:20 PM | Brian T. Jackett

# re: PowerShell Script To Find Instances Of Running SharePoint Workflow

Have you tried this in a SharePoint Online environment? I understand the logic but getting the connection is the tricky part. 4/8/2014 2:42 PM | pwc

# re: PowerShell Script To Find Instances Of Running SharePoint Workflow

It's possible use this powershell code to sharepoint online. Can you help me to do this? 9/26/2014 3:40 PM | Diego Hillesheim

# re: PowerShell Script To Find Instances Of Running SharePoint Workflow

Is 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. 3/23/2016 9:39 AM | Chilly

Post A Comment
Title:
Name:
Email:
Comment:
Verification: