Skip to main content

Find all parent flows that invoke the specified child flow

Author: MichaƂ Kornet

This script

  $m365Status = m365 status --output text
if ($m365Status -eq "Logged Out") {
m365 login
}

$environment = 'Default-dc109ffd-4298-487e-xxxx-6b9b1a2cd3e2'
$childFlowId = 'e5a842f3-f5ac-ed0c-xxxx-c8df2af79fb3'

$childFlow = m365 flow get --name $childFlowId --environmentName $environment --output json | ConvertFrom-Json
$workflowEntityId = $childFlow.properties.workflowEntityId

if (!$childFlow) {
Write-Host "Child flow not found try different environment or id"
exit
}

if ($childFlow.properties.definitionSummary.triggers[0].type -ne 'Request' -or $childFlow.properties.definitionSummary.triggers[0].kind -ne "Button") {
Write-Host "Child flow has incorrect trigger"
exit
}

$flows = m365 flow list --environmentName $environment --output json | ConvertFrom-Json

foreach ($flow in $flows) {
$displayName = $flow.properties.displayName
$id = $flow.name
$hasWorkflowAction = $false

foreach ($action in $flow.properties.definitionSummary.actions) {
if ($action.type -eq 'Workflow') {
$hasWorkflowAction = $true
break
}
}

if ($hasWorkflowAction) {
$flowDetails = m365 flow get --name $id --environmentName $environment --output json | ConvertFrom-Json

$flowDetails.properties.definition.actions | Get-Member -MemberType Properties | ForEach-Object {
$property = $_.Name
$detailedAction = $flowDetails.properties.definition.actions.$property

if ($detailedAction.type -eq 'Workflow' -and $detailedAction.inputs.host.workflowReferenceName -eq $workflowEntityId) {
$previousActions = $detailedAction.runAfter;
$previousActionNames = ""

$previousActions | Get-Member -MemberType Properties | ForEach-Object {
$previousActionNames += $_.Name + ", "
}

Write-Host "$displayName -> $id -> before: $previousActionNames"
}
}
}
}

CTRL + M