Skip to content

Instantly share code, notes, and snippets.

@hpmmatuska
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpmmatuska/75eb13fba6915b95c499 to your computer and use it in GitHub Desktop.
Save hpmmatuska/75eb13fba6915b95c499 to your computer and use it in GitHub Desktop.
The function will query the SharePoint web applications or the Site Collection for all the workflows and the result codes. It's useful to have overview about all running workflows due to build-in limits.
Function mms-SPRunningWorkflows {
<#
.Synopsis
List of running workflows for the site
.Description
Due to running worfklows you can face to various .Net compilation errors. The most know are:
- Could not write to output file ... The Directory is invalid
- The workflow can not run and will be saved only
- etc.
As the workflows are laoded in the memory you can see the increased memory consumption again the baseline. It is due to fact
of the limits by SharePoint itself, for example:
- workflow associations per list: 100
- Published Workflow definition per web site: 1000
- Total associated Workflows for web site: 1799
- etc.
.INPUTS
-URL <>
parameter converted to get spsite variable. If empty, all sites will be queried.
.OUTPUTS
The default output is a new object, contans basic info about of all running workflows on the site (all except "completed", "Null", "Cancelled").
The possible workflow statuses are:
None = 0,
Locked = 1,
Running = 2,
Completed = 4,
Cancelled = 8,
Expiring = 16,
Expired = 32,
Faulting = 64,
Terminated = 128,
Suspended = 256,
Orphaned = 512,
HasNewEvents = 1024,
NotStarted = 2048,
All = 4098
.Example
List of all executed workflows
PS C:\>mms-SPRunningWorkflows |ft -a
Web List Item State Created Modified
--- ---- ---- ----- ------- --------
http://sptest/SC Offers 1514002311 Completed 17.12.2014 8:47:42 17.12.2014 8:47:42
http://sptest/SC Offers 1514002312 Completed 17.12.2014 8:48:14 17.12.2014 8:48:14
http://sptest/SC Offers 1514002313 Completed 17.12.2014 8:49:00 17.12.2014 8:49:00
http://sptest/SC Offers 1514002313 Completed 17.12.2014 8:49:00 17.12.2014 8:49:00
http://sptest/SC Offers 1514002314 Completed 17.12.2014 8:49:25 17.12.2014 8:49:25
http://sptest/SC Offers 1514002479 Completed 17.12.2014 13:18:12 17.12.2014 13:18:12
.Example
Count executed workflows on the site collection, based on the status
PS C:\>mms-SPRunningWorkflows -URL http://sptest/sc |group state |sort -Descending |ft Count, Name -AutoSize
Count Name
----- ----
586 Completed
17 Running
4 Cancelled, Faulting, Terminated
.Example
Count runing workflows on te sharepoint, group by URL
PS C:\>mms-SPRunningWorkflows | ?{$_.state -eq "Running"} | group web | sort -Descending |ft Count, Name -AutoSize
Count Name
----- ----
868 http://sp/sc
17 http://sptest/sc
.Notes
Last Updated: February 9, 2015
Version : 1.0
.Link
https://technet.microsoft.com/en-us/library/cc262787.aspx#Workflow
#>
Param(
[Parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName, ParameterSetName="URL")]
[String[]]$URL = $null
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
if (!$URL) {$webs = get-spsite -limit all |Get-SPWeb -limit All}
else {$webs = foreach ($link in $URL){get-spsite $link | get-spweb}}
foreach($Web in $webs){
foreach ($splist in $web.Lists) {
$assoc = $spList.WorkflowAssociations | where {$_.RunningInstances -gt 0}
if($assoc -ne $null) {
foreach($item in $spList.Items){
$item.workflows | %{
[PSCustomObject] @{
Web = $Web.Url
List = $spList.Title
Item = $item.Title
State = $_.InternalState
Created = $_.created
Modified = $_.modified
}
<# # If you would lke to cancel the workflows, uncomment this block. Be sure you are running with elevated permission (farm account)
$web.AllowUnsafeUpdates = true
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($item.workflows)
$web.AllowUnsafeUpdates = false
#>
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment