PowerShell Script To Find Instances Of Running SharePoint Workflow

$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()

This article is part of the GWB Archives. Original Author: Brian T. Jackett

New on Geeks with Blogs