Sunday, June 22, 2014

Workflow Instance Report across site collection using Powershell script

Hi All,

I have written the script to retrieve the workflow instances that associated across a specific site collection.

The script will iterate through each item in the list/library for all sub sites in the site collection and provides the workflows associated with the status, status url, item id, workflow name, workflow instance id.

 
param ([boolean] $writeToFile = $true)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Add-Type @'
 public class workflowItem
 {
   public string weburl;
   public string ListURL;
   public string Item;
   public string workflowname;
   public string status;
   public string InstanceId;
   public string url;
 }
'@

$itemsCollection = New-Object 'System.Collections.Generic.List[System.Object]';
$exportfile = $currentdatetime = get-date -format "yyyy-MM-d.hh-mm-ss";
$exportVersionLocation=$exportfolder+"\workflowstats"+$exportfile+".csv"

#Counter variables
 $webcount = 0
 $listcount = 0
 $associationcount = 0

$sitename =  Read-Host "Enter site collection url"
$site=Get-SPSite -Identity $sitename

$webs=$site.Allwebs

if($webs.count -ge 1)
{

foreach($web in $site.Allwebs)
{


 write-host $web 
 #Grab all lists in the current web
 $lists = $web.Lists
 foreach($list in $lists)
 {
 $associations = @()
    #Get all workflows that are associated with the current list
             foreach($listassociation in $list.WorkflowAssociations)
 {
 $associations += $($listassociation.name)
 }
 $listcount +=1

 if($associations.count -ge 1 -and $list.items.count -ge 1)
 {

 foreach($item in $list.items)
  {

foreach($wf in $item.workflows)
    {

     $wfItem = New-Object workflowItem
     $wfItem.weburl=$web.url
     $wfItem.ListURL=$list.Title
     $wfitem.Item=$item.Title
     $ass1=$wf.parentassociation
     $wfItem.workflowname=$ass1.Name
     $wfItem.status=$wf.InternalState
     $wfitem.InstanceId=$wf.InstanceID.Tostring()
     $wfitem.url=$wf.StatusUrl
     $itemsCollection.Add($wfItem);
     
    }
 
   }

 
 }

 }
}
 $webcount +=1

 $web.Dispose()
 }

$itemsCollection | export-csv "$exportVersionLocation" -noType
 
The csv file will be generated in the root directory where the script placed and executed,

No comments: