How to get the inventory of the existing SharePoint workflow using PowerShell

Hari Rama Krishnan
 
Technology Specialist
May 18, 2018
 
Rate this article
 
Views
4028

While planning for the Migration, we need to know the number of workflows associated with the Lists for planning the remediation and validation. This inventory is not available on any of the existing migration tools handy. For that, before even starting the migration, we need to know the counts and the lists associated for a better planning. The below script will give a clear output of the lists and the associated workflows.

 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 
 try {
 
 #Create a variable based on the current date and time
 
 $StartTime = (Get-Date -UFormat "%Y-%m-%d_%I-%M-%S %p").tostring()
 
 $0 = $MyInvocation.MyCommand.Definition
 
 $dp0 = [System.IO.Path]::GetDirectoryName($0) 
 
 $CSVoutput = $("$dp0ResultWorkflowsInformation_" +$StartTime +".csv")
 
 $logFile=$("$dp0LogsWorkflowInformationLog.txt")
 
 Write "Script started running at " $StartTime >> $logFile
 
 ##Creating and Returning a DataTable## 
 
 function createDT() 
 
 { 
 
 ###Creating a new DataTable### 
 
 $tempTable = New-Object System.Data.DataTable 
 
 ##Creating Columns for DataTable## 
 
 $col1 = New-Object System.Data.DataColumn("URL") 
 
 $col2 = New-Object System.Data.DataColumn("List Name") 
 
 $col3 = New-Object System.Data.DataColumn("Workflow Instances") 
 
 $col4 = New-Object System.Data.DataColumn("Workflow")
 
 $col5 = New-Object System.Data.DataColumn("Workflow Created")
 
 $col6 = New-Object System.Data.DataColumn("Site Colln URL")
 
 $col7 = New-Object System.Data.DataColumn("Sub-Site URL")
 
 ###Adding Columns for DataTable### 
 
 $tempTable.columns.Add($col1) 
 
 $tempTable.columns.Add($col2) 
 
 $tempTable.columns.Add($col3) 
 
 $tempTable.columns.Add($col4)
 
 $tempTable.columns.Add($col5)
 
 $tempTable.columns.Add($col6)
 
 $tempTable.columns.Add($col7)
 
 return ,$tempTable 
 
 }
 
 [System.Data.DataTable]$dTable = createDT
 
 #Initialize Workflow Count variable
 
 $workflowcount = 0
 
 $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
 
 $websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
 
 foreach ($websvc in $websvcs) 
 
 {
 
 try
 
 {
 
 foreach ($webApplication in $websvc.WebApplications) 
 
 {
 
 #Skip Admin Web Applicaiton
 
 if($webApplication.DisplayName -match "SharePoint Central Administration")
 
 {
 
 continue;
 
 }
 
 try
 
 {
 
 Write "`r`n Inside the loop for web application" $webApplication.Url >> $logFile
 
 foreach($site in $webApplication.Sites)
 
 {
 
 try
 
 {
 
 Write "`r`n Inside the loop for site" $site.Url >> $logFile
 
 foreach($web in $site.AllWebs)
 
 {
 
 Write "`r`n Inside the loop for web" $web.Url >> $logFile
 
 foreach($list in $web.Lists)
 
 {
 
 Write "`r`n Inside the loop for the list" $list.Title >> $logFile
 
 foreach($wf in $list.WorkflowAssociations)
 
 {
 
 Write "`r`n Inside the loop for the workflow" $wf.Name >> $logFile
 
 $workflowcount += 1
 
 $row = $dTable.NewRow() 
 
 $row["URL"] = $web.Url 
 
 $row["List Name"] = $list.Title 
 
 $row["Workflow Instances"] = $wf.RunningInstances 
 
 $row["Workflow"] = $wf.Name
 
 $row["Workflow Created"] = $wf.Created
 
 $row["Site Colln URL"] = $site.Url
 
 $row["Sub-Site URL"] = $web.Url
 
 $dTable.rows.Add($row)
 
 }
 
 }
 
 $web.Dispose()
 
 }
 
 }
 
 catch [Exception]{
 
 Write $_.Exception|format-list -force >>$logFile
 
 Write-Host -f red $_.Exception|format-list -force
 
 }
 
 finally{
 
 if($web){
 
 $web.Dispose()
 
 }
 
 }
 
 $site.Dispose()
 
 }
 
 }
 
 catch [Exception]{
 
 Write $_.Exception|format-list -force >>$logFile
 
 Write-Host -f red $_.Exception|format-list -force
 
 }
 
 finally{
 
 if($site){
 
 $site.Dispose();
 
 }
 
 }
 
 }
 
 }
 
 catch [Exception]{
 
 Write $_.Exception|format-list -force >>$logFile
 
 Write-Host -f red $_.Exception|format-list -force
 
 }
 
 }
 
 if($dTable -ne $null) 
 
 {
 
 $dTable | Export-CSV -path $CSVoutput -notype
 
 #Write-Host "Done" -ForegroundColor Green
 
 } 
 
 else 
 
 { 
 
 Write-Host "There are no workflows in SharePoint Farm" -ForegroundColor red
 
 }
 
 }
 
 catch [Exception]{
 
 Write $_.Exception|format-list -force >>$logFile
 
 Write-Host -f red $_.Exception|format-list -force
 
 }
 
 $EndTime = (Get-Date -UFormat "%Y-%m-%d_%I-%M-%S %p").tostring()
 
 Write "Script stopped at" $EndTime >> $logFile
 

The output of the script will be as below.

clip_image002

Hope the handy script helps to get the inventory and save few hours of effort.

Happy Coding,

Hariramakrishnan Vasuthevan

Author Info

Hari Rama Krishnan
 
Technology Specialist
 
Rate this article
 
Hariramakrishnan Vasuthevan is a SharePoint Consultant and he has very good experience in the field of Microsoft Technologies, specifically SharePoint 2010, 2013, 2016 and O365, ASP.NET, Angular JS. He involved ...read more
 

Leave a comment