0

After trying for several hours I am struggling with powershell to do this: Please see my previos question here: how to get with powershell the list items which have old versions of a workflow running

Basically I want to return the 5 items where this workflow is supposed to be running.

enter image description here

I tried the following script, with no success.

$web = get-spweb https://mysite
$list = $web.Lists["Bill Cycles"]
foreach($wf in $list.WorkflowAssociations)
{
   if($wf.Name -eq "Bill Cycle Preparation Approval (Previous Version:19/07/2013 14:52:21)")
   {
       write-host $wf.Name, $wf.Id
       foreach($listitem in $list.Items)
       {
          foreach($Workflow in $listitem.Workflows)
          {
            if($Workflow.AssociationId -eq $wf.ParentAssociationId)
            {
                write-host $listitem .Title
            }
          }
       }
   }
} 
1

I just tested your script in my VM and its working fine for a custom list.

Are your workflows associated with the list or with a content type?

For Workflows associated to a content type your script should be changed as below:

$web = get-spweb webUrl
$list = $web.Lists["listName"]
$cTypes = $list.ContentTypes
$cType = $cTypes["cTypeName"]
foreach($wf in $cType.WorkflowAssociations)
{
   if($wf.Name -eq "wfName")
   {
       write-host $wf.Name, $wf.Id
       foreach($listitem in $list.Items)
       {
          foreach($Workflow in $listitem.Workflows)
          {
            if($Workflow.AssociationId -eq $wf.ParentAssociationId)
            {
                write-host $listitem.Title
            }
          }
       }
   }
} 

But this didn't working for a document library.

For a document library I modified the script as below by using $wf.Id. This way all the items are returned that have ever ran the workflow but then you can filter by checking the internal state is it Running, Canceled, Completed or "Running, Faulting, Termininating".

$web = get-spweb webUrl
$list = $web.GetList($web.Url + "/Lists/listName");
$cTypes = $list.ContentTypes
$cType = $cTypes["cTypeName"];
foreach($wf in $cType.WorkflowAssociations)
{
   if($wf.Name -eq "workflowName")
   {
       write-host $wf.Name, $wf.Id, $wf.ParentAssociationId
       foreach($listitem in $list.Items)
       {
          foreach($Workflow in $listitem.Workflows)
          {
             #write-host $Workflow.AssociationId
             if($Workflow.AssociationId -eq $wf.Id)
             {
                 write-host $listitem.Title, $Workflow.InternalState
             }
          }
       }
   }
} 
2
  • my workflow is set on a document library, but the thing is the document library is full of document sets, dont know if that can make a difference.! Feb 7 '14 at 14:39
  • Not sure for document sets. Try the second script with $wf.Id and see what you get in your case. Feb 7 '14 at 14:48

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.