0

I am trying to get the most recent run time of a list of workflows. I have an export of data that gives me the Site URL and the Subscription ID of the workflow.

My thought process is to some how connect to the Workflow History list for each site URL, then filter the data by the Subscription ID. I would then sort by either the Created Date or ID, then return the first entry. This would give me the most recent run. I've been able to do this with Power Automate but the Flow that I started has been running for 5 days now and only outputted 1,000 out of the 3,000 workflows I'm interested in.

I was thinking about using PowerShell for this issue because it can query SharePoint faster and handles the data faster. I've looked at the PNP command to get list items but it doesn't work well with the Workflow history list.

Does anyone know how to get items from a SharePoint list and filter them like I describe above?

0

SharePoint Online: Get All running Workflows using PowerShell

$url = "https://xxx.sharepoint.com/sites/projects"
$userName = "username@xxx.onmicrosoft.com"
$password = Read-Host "Please enter the password for $($userName)" -AsSecureString
 
#Create credential variable and enable 
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$context.Credentials = $SPOcredentials
 
#ObjectArray
$WorkflowDetails=@()
 
Function Get-WorkflowAssociations($WorkflowDetails, $SPOCredentials, $Web){
    #Get Web information and subsites
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($web)
    $context.Credentials = $SPOCredentials
    $web = $context.Web
    $context.Load($web)
    $context.Load($web.Webs)
     
    try{
        $context.executeQuery()
        #check any subweb if present
        if ($web.Webs.Count -ne 0){
            foreach ($subweb in $web.Webs){
                Get-WorkflowAssociations -WorkflowDetails $WorkflowDetails -SPOCredentials $SPOCredentials -Web $subweb.url
            }
        }
 
        write-host $web.url
        $context.Load($web.Lists)
        $context.ExecuteQuery() 
 
        foreach($list in $web.Lists) {     
            $context.Load($list.WorkflowAssociations)   
            $context.ExecuteQuery() 
 
            foreach($wfAssociation in $list.WorkflowAssociations | Where {$_.Status -eq "Started"}) {
                if($wfAssociation.name -notlike "*Previous Version*"){
                    $row=new-object PSObject
                    add-Member -inputObject $row -memberType NoteProperty -name "SiteURL" -Value $web.Url
                    add-Member -inputObject $row -memberType NoteProperty -name "ListTitle" -Value $list.Title
                    add-Member -inputObject $row -memberType NoteProperty -name "WorkflowName" -Value $wfAssociation.Name
                    $WorkflowDetails+=$row
                }
            }
        }
        return $WorkflowDetails
    }
    catch{
        write-host "Error: $($_.Exception.Message)" -foregroundcolor green
    }
}
 
$WorkflowDetails = Get-WorkflowAssociations -WorkflowDetails $WorkflowDetails -SPOCredentials $SPOCredentials -Web $url

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.