Introduction

This articles outlines how to generate workflow reports (in-progress workflows and completed workflows) using a PowerShell script in SharePoint 2010.

Why PowerShell script

Consider a scenario where you configure a workflow for a list to alert you when a new item is added or there are any changes to an existing item. In this case each item would have a separate instance of workflow.

Consider a list with thousands of items, where it is tedious to check the workflow status on each item manually, here PowerShell is useful.

Functionality

The PowerShell script explained in this article loops through all the sites under a site collection and all the lists under each web and all the items under each list and generates 2 separate reports, one to hold the in-progress workflow details and the other to hold the completed workflow details.

The following piece of code generates the workflow reports:

  1. Function WorkflowReports()
  2. {
  3. $output = $scriptbase + "\" + "InProgressWorkflows.csv"
  4. "SiteCollection" + "," + "WebURL" + "," + "List" + "," + "ItemName" + "," + "ItemGUID" + "," + "WorkflowInstanceID" | Out-File -Encoding Default -FilePath $Output;
  5. $output1 = $scriptbase + "\" + "OtherWorkflows.csv"
  6. "SiteCollection" + "," + "WebURL" + "," + "List" + "," + "ItemName" + "," + "ItemGUID" + "," + "WorkflowInstanceID" | Out-File -Encoding Default -FilePath $Output1;
  7. Write-host "Getting workflow report" -fore magenta
  8. $SiteCollectionURL = read-host "Enter site collection URL "
  9. $siteCollection = get-spsite $SiteCollectionURL -ea silentlycontinue
  10. if($SiteCollection -ne $null)
  11. {
  12. foreach($web in $SiteCollection.allwebs)
  13. {
  14. $lists = $web.lists
  15. foreach($list in $lists)
  16. {
  17. foreach($item in $list.items)
  18. {
  19. foreach($workflow in $item.workflows)
  20. {
  21. if($workflow.iscompleted -eq $false)
  22. {
  23. $SiteCollectionURL + "," + $web.url + "," + $list.Title + "," + $workflow.ItemName + "," + $workflow.ItemGUID + "," + $workflow.InstanceId | Out-File -Encoding Default -Append -FilePath $Output;
  24. }
  25. else
  26. {
  27. $SiteCollectionURL + "," + $web.url + "," + $list.Title + "," + $workflow.ItemName + "," + $workflow.ItemGUID + "," + $workflow.InstanceId | Out-File -Encoding Default -Append -FilePath $Output1;
  28. }
  29. }
  30. }
  31. }
  32. }
  33. Write-host "Reports generated and available at the script location " -fore green
  34. }
  35. else
  36. {
  37. write-host "Invalid site collection.... please check the URL...." -fore red
  38. }
  39. }
Complete Code
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
  2. $LogFile = ".\WorkflowDetailsPatch-$LogTime.rtf"
  3. # Add SharePoint PowerShell Snapin
  4. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
  5. Add-PSSnapin Microsoft.SharePoint.Powershell
  6. }
  7. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
  8. Set-Location $scriptBase
  9. write-host "TESTING FOR LOG FOLDER EXISTENCE" -fore yellow
  10. $TestLogFolder = test-path -path $scriptbase\Logs
  11. if($TestLogFolder)
  12. {
  13. write-host "The log folder already exist in the script location" -fore yellow
  14. $clearlogfolder = read-host "Do you want to clear the log folder (y/n)"
  15. if($clearlogfolder -eq 'y')
  16. {
  17. write-host "The user choosen to clear the log folder" -fore yellow
  18. write-host "Clearing the log folder" -fore yellow
  19. remove-item $scriptbase\Logs\* -recurse -confirm:$false
  20. write-host "Log folder cleared" -fore yellow
  21. }
  22. else
  23. {
  24. write-host "The user choosen not to clear the log files" -fore yellow
  25. }
  26. }
  27. else
  28. {
  29. write-host "Log folder does not exist" -fore yellow
  30. write-host "Creating a log folder" -fore yellow
  31. New-Item $Scriptbase\Logs -type directory
  32. write-host "Log folder created" -fore yellow
  33. }
  34. #moving any .rtf files in the scriptbase location
  35. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
  36. if($FindRTFFile)
  37. {
  38. write-host "Some old log files are found in the script location" -fore yellow
  39. write-host "Moving old log files into the Logs folder" -fore yellow
  40. foreach($file in $FindRTFFile)
  41. {
  42. move-item -path $file -destination $scriptbase\logs
  43. }
  44. write-host "Old log files moved successfully" -fore yellow
  45. }
  46. start-transcript $logfile
  47. Function WorkflowReports()
  48. {
  49. $output = $scriptbase + "\" + "InProgressWorkflows.csv"
  50. "SiteCollection" + "," + "WebURL" + "," + "List" + "," + "ItemName" + "," + "ItemGUID" + "," + "WorkflowInstanceID" | Out-File -Encoding Default -FilePath $Output;
  51. $output1 = $scriptbase + "\" + "OtherWorkflows.csv"
  52. "SiteCollection" + "," + "WebURL" + "," + "List" + "," + "ItemName" + "," + "ItemGUID" + "," + "WorkflowInstanceID" | Out-File -Encoding Default -FilePath $Output1;
  53. Write-host "Getting workflow report" -fore magenta
  54. $SiteCollectionURL = read-host "Enter site collection URL "
  55. $siteCollection = get-spsite $SiteCollectionURL -ea silentlycontinue
  56. if($SiteCollection -ne $null)
  57. {
  58. foreach($web in $SiteCollection.allwebs)
  59. {
  60. $lists = $web.lists
  61. foreach($list in $lists)
  62. {
  63. foreach($item in $list.items)
  64. {
  65. foreach($workflow in $item.workflows)
  66. {
  67. if($workflow.iscompleted -eq $false)
  68. {
  69. $SiteCollectionURL + "," + $web.url + "," + $list.Title + "," + $workflow.ItemName + "," + $workflow.ItemGUID + "," + $workflow.InstanceId | Out-File -Encoding Default -Append -FilePath $Output;
  70. }
  71. else
  72. {
  73. $SiteCollectionURL + "," + $web.url + "," + $list.Title + "," + $workflow.ItemName + "," + $workflow.ItemGUID + "," + $workflow.InstanceId | Out-File -Encoding Default -Append -FilePath $Output1;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. Write-host "Reports generated and available at the script location " -fore green
  80. }
  81. else
  82. {
  83. write-host "Invalid site collection.... please check the URL...." -fore red
  84. }
  85. }
  86. WorkflowReports
  87. write-host ""
  88. write-host "SCRIPT COMPLETED" -fore green
  89. stop-transcript
Execution procedure
  • Step 1: Copy the script to the SharePoint server
  • Step 2: Launch the SharePoint management shell
  • Step 3: Navigate to the script path
  • Step 4: Execute the script

Enter the site collection URL to generate the workflow reports under that specific site collection. The output file is placed under the same location where the PowerShell script is placed.

Conclusion

The script can be modified to run against the SharePoint Farm or a specific web application or specific web/site. Thus this article outlines how to generate workflow reports using a PowerShell script.