PowerShell帮助. SharePoint Online中的暂停工作流.

问题描述:

大家好,

背景:

客户端正在查看脚本,如果任何工作流程被暂停,则必须通过电子邮件通知客户端有关已暂停的工作流程.对于暂停的工作流,SharePoint Online支持也没有任何选择.因此,我编写了一个脚本,针对该脚本进行了修改. 客户.    

解决方案:

首先,是所有工作流所在的SharePoint网站.例如 http://company.sharepoint.com/sites/MySiteCollection


第二,通过CSOM使用了所有列表.就我而言,有一个示例列表( Beta程序请求)已暂停工作流程.




第三次,如果工作流程实例被暂停,则创建 PowerShell中的数组对象.   

第四,在PowerShell控制台中,我可以获取所有已暂停的工作流程 在数组对象中 wfIntanceCollection




第五,我将已暂停的工作流程导出到CSV .    

问题: 在导出的 Suspended.csv ,我只得到如图所示的最后一个条目.

Background:

Client is looking script if any workflow is suspended the client must be notified via email about suspended workflows. SharePoint Online support also did not have any option finding for suspended workflow. Therefore, I wrote a script which I modified for the client.    

Solution:

Firstly, the SharePoint site where all the workflows have been housed. For e.g. http://company.sharepoint.com/sites/MySiteCollection


Secondly, used all the lists using CSOM. In my case, there is a sample list (Beta Program Requests) which has suspended workflows.




Thirdly, if the workflow instance is suspended, created an array object in PowerShell.   

Fourthly, in PowerShell console, I can get all the suspended workflows in the array object wfIntanceCollection




Fifthly, I exported the suspended workflow into CSV.     

Issue: In the exported Suspended.csv, I am getting only the last entry as shown.


尽管如此,我是 wfIntanceCollection 中PowerShell控制台中的所有工作流程.   (上面的第四步")

任何指针将不胜感激.

Although, I am all the workflows in PowerShell console in the wfIntanceCollection.   (the  Fourth Step above)

Any pointers will be greatly appreciated.

在我的PowerShell脚本中.

Here in my PowerShell script.

   #Import the required DLL

    #region  Import SharePoint Online Management Shell
    Import-Module Microsoft.Online.SharePoint.Powershell -ErrorAction SilentlyContinue

    Add-PSSnapIn Microsoft.SharePoint.PowerShell  -ErrorAction SilentlyContinue
    #endregion  


    Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'

    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"


    #region for Exporting to CSV


    $csvFilePath = "C:\Temp\Suspended.csv"

    #endregion 


    $CurrentDate = Get-Date
    $CurrentDate = $CurrentDate.ToString('dd-MM-yyyy')



    #Mysite URL
    $site = 'https://company.sharepoint.com/sites/MySiteCollection/'
  

    #Get the Client Context and Bind the Site Collection
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($site)

    $context | fl

    #region Code with no Password Prompt

    $admin = "alex.hue@company.com"  
    $password = "absc!@34"  
  

    #endregion End of Code with no Password Prompt




    #Authenticate
    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin,(ConvertTo-SecureString $Password -AsPlainText -Force))
    $context.Credentials = $credentials

    $credentials | fl


    #SUSPENDED WF

    $web = $context.Web
    $lists = $web.Lists
    $context.Load($lists);

    $context.ExecuteQuery();

    $workflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($context, $web);
    $workflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService();
    $workflowInstanceSevice = $workflowServicesManager.GetWorkflowInstanceService();


    Write-Host ""
    Write-Host "List Workflows" -ForegroundColor Green
    Write-Host ""


     foreach ($list in $lists)         
                                                                                                                                                                                                                                                                                                 {   
            $workflowSubscriptions = $workflowSubscriptionService.EnumerateSubscriptionsByList($list.Id);
            $context.Load($workflowSubscriptions);
            $context.Load($list);           
            $context.ExecuteQuery();                
            foreach($workflowSubscription in $workflowSubscriptions)
            {            
                Write-Host "**************************************************************************************"
                Write-Host "List -"$list.Title " Workflow - "$workflowSubscription.Name -ForegroundColor Green
                Write-Host "***************************************************************************************"
                Write-Host ""

                $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
                $camlQuery.ViewXml = "<View> <ViewFields><FieldRef Name='Title' /></ViewFields></View>";
                $listItems = $list.GetItems($camlQuery);
                $context.Load($listItems);
                $context.ExecuteQuery();

                foreach($listItem in $listItems)
                {
                    $workflowInstanceCollection = $workflowInstanceSevice.EnumerateInstancesForListItem($list.Id, $listItem.Id);
                    $context.Load($workflowInstanceCollection);
                    $context.ExecuteQuery();


                    

                    foreach ($workflowInstance in $workflowInstanceCollection)
                    {

                       Write-Host("List Name: " + $list.Title); 
                       Write-Host("List's Site URL: " + $list.ParentWebUrl)
                       

                       if($workflowInstance.Status -eq "Suspended")
                       {
                                Write-Host "List Item Title:"$listItem["Title"] 
                                Write-Host "Workflow Status:"$workflowInstance.Status 
                                Write-Host "Last Updated:"$workflowInstance.LastUpdated
                                Write-Host ""
                              
                                #Get All the Suspended Workflows as a collection,
                                $wfInstanceCollection = @()

                                $listItems | foreach {
                                

                                #Create an object to hold Workflow Instance Information
                                $exportItem = New-Object PSObject 

                                #Add other details of the list 
                                $exportItem | Add-Member -MemberType NoteProperty -Name "List" -Value $list.Title;
                                $exportItem | Add-Member -MemberType NoteProperty -Name "List's Web URL" -Value $list.ParentWebUrl;
                                $exportItem | Add-Member -MemberType NoteProperty -Name "List Item Title" -Value $listItem["Title"];
                                $exportItem | Add-Member -MemberType NoteProperty -Name "Workflow Status" -Value $workflowInstance.Status;
                                $exportItem | Add-Member -MemberType NoteProperty -Name "Last Updated" -Value $workflowInstance.LastUpdated
                                $exportItem | Add-Member -MemberType NoteProperty -Name "Error Info" -Value $workflowInstance.FaultInfo;                         
                                

                                }
                                 $wfInstanceCollection += $exportItem 
                                 Write-Host "wfInstanceCollection values:" $wfInstanceCollection


                                 #$wfInstanceCollection | fl 
                                 `

                                 $wfInstanceCollection |  Export-Csv -Path "C:\Temp\Suspended.csv" -NoTypeInformation 

                       }

                       
                         
                                
                    }

                    


                }
                
                
            }
  
            #$wfInstanceCollection |  Export-Csv -Path "C:\Temp\Suspended_$CurrentDate.csv" -NoTypeInformation 


            #$wfInstanceCollection | fl 

            $context.Dispose();

           
        }  


桑迪

因为每个处于已暂停"状态的工作流实例都将创建一个新的空白

because for each Workflow instance in Suspended status you create a new empty 


wfInstanceCollection = @()
wfInstanceCollection = @()

将其放在循环之外,以使用所有值递增

Put it outside the loop, to increment with all values

如果您需要更多详细信息,请告诉我

Let me know if you need more details