当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 终止工作流

作者:精品下载站 日期:2024-12-14 15:00:25 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 终止工作流


要求:使用 PowerShell 停止 SharePoint Online 中的工作流。

[玩转系统] SharePoint Online:使用 PowerShell 终止工作流

SharePoint Online:PowerShell 取消工作流

SharePoint 工作流是一个强大的工具,允许组织自动化业务流程并提高生产力。但是,有时您可能需要终止导致问题或不再需要的工作流程。在这种情况下,可以使用 PowerShell 终止 SharePoint Online 中的工作流。在本文中,我们将概述使用 PowerShell 终止 SharePoint Online 中的工作流的步骤。

让我们使用 PowerShell 终止所有列表项中特定工作流的所有工作流实例。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"
 
#Set Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName ="Project Tasks"
$WorkflowName ="Send Email"
 
#Get Credentials to connect
$Cred= Get-Credential
 
Try{
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
    #Get the Web, List Objects
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $List = $Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
 
    #Initialize Workflow Manager and other related objects
    $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Ctx, $Web)
    $WorkflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService()
    $WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService() 
    $WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
    $Ctx.Load($WorkflowAssociations)
    $Ctx.ExecuteQuery()
 
    #Get the Target workflow to Terminate
    $WorkflowAssociation = $WorkflowAssociations | Where {$_.Name -eq $WorkflowName}
   
    #Prepare the query
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "@
    <View Scope='RecursiveAll'>
        <Query>
            <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
        </Query>
        <RowLimit Paged='TRUE'>2000</RowLimit>
    </View>"
  
    $Counter=1
    #Batch Process items: sharepoint online powershell bulk check in 
    Do {
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()  
        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition

        #Loop through each List Item
        ForEach($ListItem in $ListItems)
        {
            #Display a Progress bar
            Write-Progress -Activity "Processing List Items" -Status "Processing Item '$($ListItem.FieldValues.FileRef)' ($Counter of $($List.ItemCount))" -PercentComplete (($Counter / $List.ItemCount) * 100) 

            #Get Workflow Instances of the List Item            
            $WorkflowInstanceCollection = $WorkflowInstanceService.EnumerateInstancesForListItem($List.Id, $ListItem.Id) 
            $Ctx.Load($WorkflowInstanceCollection)
            $Ctx.ExecuteQuery()
         
            #Filter All Workflows Instances of the target workflow where Status is: Started or Canceled or Suspended
            $WorkflowInstancesToKill = $WorkflowInstanceCollection | Where { ($_.Status -eq "Started" -or $_.Status -eq "Canceled" -or $_.Status -eq "Suspended") `
                                                                     -and $_.WorkflowSubscriptionId -eq $WorkflowAssociation.Id}
         
            #Terminate Workflow Instances
            ForEach ($WorkflowInstance in $WorkflowInstancesToKill) 
            { 
                Try{
                    $WorkflowInstanceService.TerminateWorkflow($WorkflowInstance)
                    #To Cancel workflow, use: $WorkflowInstanceService.CancelWorkflow($WorkflowInstance)
                    $Ctx.ExecuteQuery()
                    Write-host -f Green "Worfklow Terminated on List Item:" $ListItem.Id
                } Catch {
                    Write-host "Error terminating workflow on " $ListItem.Id " Error Details: $_"
                }
            }
            $Counter++
        }
    }While($Query.ListItemCollectionPosition -ne $Null)
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯