Terminate only suspended sharepoint 2013 workflows using powershell for sharepoint online

New Contributor
 
 

 

 

if ( (Get-PSSnapin -Name "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PsSnapin Microsoft.SharePoint.Powershell
}

$siteURL = "<Site URL>";
$listName = "<ListName>";

$spWeb = Get-SPWeb $siteURL;
$spList = $spWeb.Lists[$listName];
$spListItems = $spList.Items;

#Get the Workflow Manager object and then the instance of the Manager
$wfMgr = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($spWeb);
$wfInstanceSvc = $wfMgr.GetWorkflowInstanceService();

foreach($spListItem in $spListItems)
{
    #Get a list of workflow instances running for the item in the list
    $wfInstances = $wfInstanceSvc.EnumerateInstancesForListItem($spList.ID, $spListItem.ID);
    
    foreach($wfInstance in $wfInstances)
    {
        #Check to see if the instance is suspended.  If so, terminate it.
        if($wfInstance.Status -eq "Suspended")
        {
            write-host("Terminating instances for list item: {0}" -f $spListItem.ID);   
            $wfInstanceSvc.TerminateWorkflow($wfInstance);
        }
    }
}

 

 

Hi ,

we have a work flow for our document library which was built in SP 2013. Now it been migrated to SharePoint online. In our library, we have few workflows which are started by system accounts and they re showing as suspended. I am looking for a PS script which can help us terminate only those suspended workflows. I found the script as below, but looks like its doing the termination for list, what changes i need to make it work for document library?
Thanks

0 Replies