6

I'm trying to write a script to check the status of workflows in a list. I have tried a couple different ways but I never get anything to return.

foreach($item in $list.Items){
    $item.Workflows   
}

$list.WorkFlowAssociations

$web.WorkFlowAssociations

$site.WorkflowManager.GetItemWorkflows($item)

Each of these statements return nothing. The list and site I am checking have active workflows created in designer that I can see and run in SharePoint just fine, but cannot seem to find them with powershell.

  • Do you use 2010 or 2013 workflow engine? – Roman Dec 3 '15 at 18:17
  • They are 2013 workflows. – MickB Dec 3 '15 at 19:32
9

For the SharePoint 2010 workflows (or 2010 workflows in a 2013 environment) you can run this.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$wf = @()

$web = get-spweb -Identity https://spwebUrl
$list = $web.Lists["ListName"]

foreach ($wf in $list.WorkflowAssociations)
{
    $wfAssociations += $($wf)
}

if ($wfAssociations.count -ge 1)
{
    foreach ($item in $list.Items)
    {
        foreach($workflow in $item.workflows)
        {
            $wfName = $wf.Name
            $wfStatus = $workflow.InternalState
            $wfListItem = $workflow.ItemName

            write-host "Workflow Title: $wfName Status: $wfStatus ListItem: $wfListItem"
        }
    }
}

For a 2013 workflow, you have to do it little difference because you have to access the workflows from the workflow manager.

I did some tests but i can't manage to pick up the title, only the GUID.

You might be able to figure it out, or the ID is maybe enough for you. I need to get back to work. :)

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$web = get-spweb -Identity https://spwebUrl
$list = $web.Lists["ListName"]

$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
$sub = $wfm.GetWorkflowSubscriptionService()
$wf = $sub.EnumerateSubscriptionsByList($list.ID)
$wfis=$wfm.GetWorkflowInstanceService()

foreach ($item in $list.Items)
{
    $workflowInstances=$wfis.EnumerateInstancesForListItem($list.ID,$item.ID)

    foreach($wf in $workflowInstances) 
    {
        #$wfName = $wf.Name **<---- Won't work here.**
        $wfID= $wf.ID
        $wfStatus = $wf.Status
        $wfListItem = $item.Name

        write-host "Workflow Title: $wfID Status: $wfStatus ListItem: $wfListItem"
    }

}

Update

Corrected the double use of the variable $wf and added MickB suggestions to get the name property.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$web = get-spweb -Identity https://spwebUrl
$list = $web.Lists["ListName"]

$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
$sub = $wfm.GetWorkflowSubscriptionService()
$wfs = $sub.EnumerateSubscriptionsByList($list.ID)
$wfis=$wfm.GetWorkflowInstanceService()

foreach ($item in $list.Items)
{
    $workflowInstances=$wfis.EnumerateInstancesForListItem($list.ID,$item.ID)

    foreach($wf in $workflowInstances) 
    {
        $wfName = $wfs | ?{$_.Id -eq $wf.WorkflowSubscriptionId} | select -ExpandProperty Name
        $wfID= $wf.ID
        $wfStatus = $wf.Status
        $wfListItem = $item.Name

        write-host "Workflow Title: $wfName Status: $wfStatus ListItem: $wfListItem"
    }

}
  • 2
    Great answer. Thank you. Also, for the name I changed the initial $wf variable to $wfs to differentiate it from the foreach loop variable. I got the name but using $wfName = $wfs | ?{$_.Id -eq $wf.WorkflowSubscriptionId} | select -ExpandProperty Name – MickB Dec 7 '15 at 19:20
  • Super! Thanks for finding that and with adding a way to get the name out! :) – Christoffer Dec 8 '15 at 7:49
  • Is it possible to get the last run time of Workflow instance. – Pavan Oct 17 '18 at 5:19
  • @Pavan 2010 or 2013? – Christoffer Oct 17 '18 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.