Skip to content

Instantly share code, notes, and snippets.

@pkskelly
Last active March 1, 2021 19:39
Show Gist options
  • Save pkskelly/9543e1774e5e72842454fbab67d6106e to your computer and use it in GitHub Desktop.
Save pkskelly/9543e1774e5e72842454fbab67d6106e to your computer and use it in GitHub Desktop.
PowerShell Core script that retrieves and exports Flows and Owner information to CSV file
#!/usr/local/bin/pwsh -File
$DIR = Split-Path $script:MyInvocation.MyCommand.Path
$TMP_DIR = "./tmp"
$FLOWSCSV_SUFFIX = "-flows.csv"
$ALLFLOWSCSV = "allflows.csv"
function CleanDistFolder {
# Remove the dist folder as needed
if (Test-Path -Path "$TMP_DIR" -PathType Container) {
Remove-Item -Path "$TMP_DIR" -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue
}
}
$CURRENT_USER = $(m365 status).Split(':')[1]
Write-Host "Logged in as $CURRENT_USER"
try {
if (-not (Test-Path -Path "$TMP_DIR" -PathType Container)) {
Write-Host " Creating $TMP_DIR folder..."
New-Item -ItemType Directory -Path "$TMP_DIR"
}
#Step 1 - Get the all environments
Write-Host "Querying for environments..."
$flowEnvironments = m365 flow environment list --output json | ConvertFrom-Json
foreach ($environment in $flowEnvironments) {
$currentEnvironmentName = $($environment.name)
Write-Host "Querying Flows from envrionment $currentEnvironmentName)..."
# Step 2 - Get all of the flows using the cli and write flows json to tmp files
# Use a JMESPath query to filter the size of the file. See https://github.com/pnp/cli-microsoft365/issues/1266
m365 flow list --environment $currentEnvironmentName `
--query '[].{environment: properties.environment.name, name: name, displayName: properties.displayName,owner: properties.creator.userId, state: properties.state, created: properties.createdTime, lastModified: properties.lastModifiedTime, trigger: properties.definitionSummary.triggers[0].swaggerOperationId, triggerType: properties.definitionSummary.triggers[0].type }' --asAdmin --output json |
Out-File "$TMP_DIR/$currentEnvironmentName$FLOWSCSV_SUFFIX" -Encoding ASCII
}
# Consolidate all flow environment output files.
$flows = Get-ChildItem "$TMP_DIR/*$FLOWSCSV_SUFFIX" | ForEach-Object {(Get-Content -Raw $_.FullName | ConvertFrom-Json)}
#Step 3 - Get a unique list of the flow owners from the tmp file
Write-Host "Flows found, searching for owner values..."
$uniqueOwners = $flows.owner | Sort-Object | Get-Unique
Write-Host "There are $($uniqueOwners.Count) unique Flows owners."
Write-Host "Building owner information mappings..."
#Step 4 - map properties.creator.userId's to {name, email} mapping hashtable
Write-Host "Querying graph for userids..."
$userMap = @{}
$uniqueOwners | ForEach-Object {
Write-Host "Querying graph for userid $_..."
m365 aad user get --id $_ --output json | ConvertFrom-Json
} | ForEach-Object {
$userMap.Add($_.id, @{
upn = $_.userPrincipalName
displayName = $_.displayName
mail = $_.mail
}
)
}
# And add the Owner information to each flow entry to get owner name and email
Write-Host "Mapping owner information..."
$flows | ForEach-Object {
$_ | Add-Member -MemberType NoteProperty -Name "upn" -Value $userMap[$_.owner].upn
$_ | Add-Member -MemberType NoteProperty -Name "ownerName" -Value $userMap[$_.owner].displayName
$_ | Add-Member -MemberType NoteProperty -Name "ownerMail" -Value $userMap[$_.owner].mail
}
#Step 5 - Create a CSV file with header row, flow information and owner email
$flows | Export-Csv -Path "$ALLFLOWSCSV" -NoTypeInformation
}
finally {
CleanDistFolder
}
# if we are on macOS try opening the file with Excel
if ($IsMacOS) {
$answer = Read-Host -Prompt "Open CSV file in Excel? (y/n)"
switch ($answer)
{
y {
open -a "/Applications/Microsoft Excel.app" "$DIR/$ALLFLOWSCSV"
}
Default {
Write-Host "Open $DIR/$ALLFLOWSCSV to review report."
}
}
}
@pkskelly
Copy link
Author

pkskelly commented Mar 1, 2021

Updated to get all flows from all environments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment