Thursday, May 10, 2012

PowerShell Script: Identify All Workflows in Your SharePoint Farm


The following script will return all Workflows currently associated within your SharePoint 2007 or SharePoint 2010 farm using PowerShell and output it to a CSV file.  It returns the URL of the site, Title of the list, Title of the workflow, and the number of currently running instances.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null 
$outputFile = Read-Host "Filename and location (i.e. C:\output.txt)"

$farm = [Microsoft.SharePoint.Administration.SPFarm]::local
$websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]} 
$webapps = @() 

$outputHeader = "Url;List;Workflow;Running Instances" > $outputFile

foreach ($websvc in $websvcs) { 

    foreach ($webapp in $websvc.WebApplications) { 
  foreach ($site in $webapp.Sites) {
   foreach ($web in $site.AllWebs) {
    foreach ($List in $web.Lists) {
     foreach ($workflow in $List.WorkflowAssociations) {
      $output = $web.Url + ";" + $List.Title + ";" + $workflow.Name + ";" + $workflow.RunningInstances
      Write-Output $output >> $outputFile
    }}
    } 
   } 
  }
 }
$Web.Dispose();
$site.Dispose();

It can also be downloaded from my Google Drive here: CheckWorkflowsInFarm.zip 

Output Example:

Url;List;Workflow;Running Instances
http://intranet;Report a Problem or New Request;Notification Workflow for Bug List (Previous Version:4/13/2011 3:01:15 PM);0
http://intranet;Report a Problem or New Request;Notification Workflow for Bug List;0
http://intranet;Report a Problem or New Request;Notification Workflow for Bug List (Previous Version:4/13/2011 3:04:13 PM);0
http://intranet/contracts;PDF Doc Library;Send to Sales;0
http://intranet/contracts;PDF Doc Library;Send to Sales (Previous Version:6/4/2010 5:33:55 PM);0
http://intranet/contracts;Doc Library;DOC Approval (Previous Version:6/29/2010 3:55:22 PM);1
http://intranet/contracts;Doc Library;DOC Approval;0
http://intranet/contracts;Doc Library;DOC Approval (Previous Version:6/29/2010 3:49:51 PM);0
http://intranet/contracts;Doc Library;DOC Approval (Previous Version:6/29/2010 3:37:48 PM);0
http://intranet/contracts;Doc Library;DOC Approval (Previous Version:6/29/2010 11:18:26 AM);0

9 comments:

  1. Great script, but i needed avoid "previus version" and launch it only for one site collection because of memory so I used this:

    #Load SharePoint 2007 Assemblies
    [System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

    $siteurl="http://your.site.com"
    $site=new-object Microsoft.SharePoint.SPSite($siteurl)

    #Initialize Workflow Count variable
    $workflowcount = 0

    #Foreach loop to loop through all webs, and lists with workflow associations, and exclude workflows that have previous versions and write findings to .csv file.

    function Get-Workflows()
    {
    foreach($web in $site.AllWebs)
    {
    foreach($list in $web.Lists)
    {
    foreach($wf in $list.WorkflowAssociations)
    {
    if ($wf.Name -notlike "*Previous Version*")
    {
    $hash = @{"[URL]"=$web.Url;"[List Name]"=$list.Title;"[Workflow]"=$wf.Name}
    New-Object PSObject -Property $hash | Sort-Object

    }
    }
    }
    }
    }

    foreach($web in $site.AllWebs)
    {
    foreach($list in $web.Lists)
    {
    foreach($wf in $list.WorkflowAssociations)
    {
    if ($wf.Name -notlike "*Previous Version*")
    {
    $workflowcount += 1
    }
    }
    }
    }

    Get-Workflows | Export-csv E:\workflows.csv
    "Workflow Count " + $workflowcount >> E:\workflows.csv

    $site.Dispose()

    ReplyDelete
  2. Hi Jeff:

    David Tobey from Nintex here. I just wanted to leave a note to say how much I like this script you've built. Easy to follow and extremely versatile, considering you can drop a CSV file right into Excel for sorting. Nice work!

    ReplyDelete
  3. Within SharePoint, there are a variety of kinds of workflows - out of the box, sharepoint designer, visual studio, nintex/k2/agilepoint, and perhaps other types.

    Does this script display information about all these types?

    ReplyDelete
  4. I ran the above script and got a file out. However, I don't know how to tell what types of workflows these are.

    Also, when I tried to run this script a second time, I got errors saying that an out of memory exception was raised.

    Is there something that I need to do after executing this program so that memory is released?

    Thank you.

    ReplyDelete
  5. Jeff, I am hoping to get some info from you about the above script.
    I ran it in my environment and it came back with several hundred lines. I have some questions about the output. I don't understand everything present, and not everything that I expected to see is there.

    For instance, we have 6 InfoPath applications which have workflow associated to them in our farm. One of the 6 shows up in the output from the above script, but the others do not. All have have some degree of activity in the past 7 or 8 months, so I was expecting to see them all there. Is there something different I need to do in the script to pick them all up?

    Also, there are quite a number of entries that show, as part of the workflow name words to the effect "(Previous Version:mm/dd/yyy hh:mm:ss AM or PM)". It appears that at least some of these are SharePoint Designer workflows. Is there a way to determine what those workflows do if I don't have the source files?

    ReplyDelete
  6. GREAT Script - thanks so much

    ReplyDelete
  7. I am curious about the info available within PowerShell. Is there a way to determine any email addresses associated to the workflow via properties or methods? I have been reading various technet pages but so far I could not find what I would have to add to your script to get that information.

    ReplyDelete
  8. Great script! Thank you so much for this it was tremendously helpful!

    ReplyDelete
  9. Thank you for the great script! It's the only one I found to work!

    ReplyDelete