Skip to content

Commit

Permalink
Merge pull request #32 from stefanstranger/PowerAutomateLocal
Browse files Browse the repository at this point in the history
Fix for actions that don't have a runafter property
  • Loading branch information
stefanstranger authored Mar 29, 2024
2 parents 40dc946 + d5710a9 commit b73bb25
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 31 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ And finally some great PowerShell community members for their feedback and sugge

## Change Log

v1.1.3 - 2024-03-29
* Bug fixes:
* [Fix for actions that don't have a runafter property](https://github.com/stefanstranger/logicappdocs/issues/31)

v1.1.2 - 2023-08-18
* Bug fixes:
* [Fix for actions with multiple runafter properties](https://github.com/stefanstranger/logicappdocs/issues/23)
Expand Down
33 changes: 24 additions & 9 deletions src/Helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,34 @@ Function Get-Action {
foreach ($key in $Actions.PSObject.Properties.Name) {
$action = $Actions.$key
$actionName = $key -replace '[ |(|)|@]', '_'
Write-Verbose ('Action {0}' -f $actionName)
Write-Verbose ('Object {0}' -f $($action | ConvertTo-Json -Depth 10 ))

# new runafter code
$runAfter = if (![string]::IsNullOrWhitespace($action.runafter)) {
$action.runAfter.PSObject.Properties.Name -replace '[ |(|)|@]', '_'
}
elseif (([string]::IsNullOrWhitespace($action.runafter)) -and $Parent) {
# if Runafter is empty but has parent use parent.
$Parent -replace '(-False|-True)', ''
if ($action | Get-Member -MemberType Noteproperty -Name 'runafter') {
$runAfter = if (![string]::IsNullOrWhitespace($action.runafter)) {
$action.runAfter.PSObject.Properties.Name -replace '[ |(|)|@]', '_'
}
elseif (([string]::IsNullOrWhitespace($action.runafter)) -and $Parent) {
# if Runafter is empty but has parent use parent.
$Parent -replace '(-False|-True)', ''
}
else {
# if Runafter is empty and has no parent use null.
$null
}
}
else {
# if Runafter is empty and has no parent use null.
$null
}
Write-Warning ('Action {0} has no runafter property' -f $actionName)
#Set runafter to parent if parent is not null
if ($Parent) {
$runAfter = $Parent
}
else {
$runAfter = $null
}
}


$inputs = if ($action | Get-Member -MemberType Noteproperty -Name 'inputs') {
$($action.inputs)
Expand Down
2 changes: 1 addition & 1 deletion src/New-LogicAppDoc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ $WarningPreference = 'SilentlyContinue'
Author: Stefan Stranger
Github: https://github.com/stefanstranger/logicappdocs
Version: 1.1.2
Version: 1.1.3
"@.foreach({
Write-Host $_ -ForegroundColor Magenta
Expand Down
57 changes: 36 additions & 21 deletions src/New-PowerAutomateDoc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Param(
ParameterSetName = 'PowerAutomate')]
[string]$PowerAutomateName,

[Parameter(Mandatory = $false)]
[string]$FilePath,

[Parameter(Mandatory = $false)]
[string]$OutputPath = (Get-Location).Path
)
Expand All @@ -27,7 +30,7 @@ $WarningPreference = 'SilentlyContinue'
Author: Stefan Stranger
Github: https://github.com/stefanstranger/logicappdocs
Version: 1.1.2
Version: 1.1.3
"@.foreach({
Write-Host $_ -ForegroundColor Magenta
Expand Down Expand Up @@ -106,28 +109,40 @@ Function Create-ExportPackage {

#region Main Script

#region login to Power Automate and get PowerAutomate Flow
Write-Host ('Login to Power Automate and get PowerAutomate Flow') -ForegroundColor Green
Get-Flow -EnvironmentName $EnvironmentName | Where-Object { $_.DisplayName -eq $PowerAutomateName } -OutVariable PowerAutomateFlow
#endregion
if (!($FilePath)) {
#region login to Power Automate and get PowerAutomate Flow
Write-Host ('Login to Power Automate and get PowerAutomate Flow') -ForegroundColor Green
Get-Flow -EnvironmentName $EnvironmentName | Where-Object { $_.DisplayName -eq $PowerAutomateName } -OutVariable PowerAutomateFlow
#endregion

#region Create PowerAutomate Flow Export Package
Write-Host ('Create PowerAutomate Flow Export Package') -ForegroundColor Green
Create-ExportPackage -Flow $PowerAutomateFlow -OutVariable packageDownload
#endregion
#region Create PowerAutomate Flow Export Package
Write-Host ('Create PowerAutomate Flow Export Package') -ForegroundColor Green
Create-ExportPackage -Flow $PowerAutomateFlow -OutVariable packageDownload
#endregion

#region download PowerAutomate Flow Export Package
Write-Host ('Download PowerAutomate Flow Export Package') -ForegroundColor Green
Start-BitsTransfer -Source $($packageDownload.packageLink.value) -Destination (Join-Path $($env:TEMP) ('{0}.zip' -f $($PowerAutomateFlow.DisplayName.Split([IO.Path]::GetInvalidFileNameChars()) -join '_')))
#endregion
#region download PowerAutomate Flow Export Package
Write-Host ('Download PowerAutomate Flow Export Package') -ForegroundColor Green
Start-BitsTransfer -Source $($packageDownload.packageLink.value) -Destination (Join-Path $($env:TEMP) ('{0}.zip' -f $($PowerAutomateFlow.DisplayName.Split([IO.Path]::GetInvalidFileNameChars()) -join '_')))
#endregion

$zipPath = (Join-Path $($env:TEMP) ('{0}.zip' -f $($PowerAutomateFlow.DisplayName.Split([IO.Path]::GetInvalidFileNameChars()) -join '_')))
$packageName = $($packagedownload.resources.psobject.Properties.name[0])
}
else {
$zipPath = $FilePath
}

#region Unzip PowerAutomate Flow Export Package
Write-Host ('Unzip PowerAutomate Flow Export Package') -ForegroundColor Green
Expand-Archive -LiteralPath (Join-Path $($env:TEMP) ('{0}.zip' -f $($PowerAutomateFlow.DisplayName.Split([IO.Path]::GetInvalidFileNameChars()) -join '_'))) -DestinationPath $($env:TEMP) -Force
Expand-Archive -LiteralPath $zipPath -DestinationPath $($env:TEMP) -Force
if ($FilePath) {
#Find the package name from the unzipped folder
$packageName = (Get-ChildItem -Path $($env:TEMP) -Recurse -Filter 'definition.json' -ErrorAction SilentlyContinue | Sort-Object -Property CreationTime -Descending | Select-Object -First 1).Directory.BaseName
}
#endregion

#region refactor PowerAutomate Flow definition.json to align with LogicApp expected format
$PowerAutomateFlowJson = Get-Content -Path (Join-Path $($env:TEMP) ('Microsoft.Flow\flows\{0}\definition.json' -f $($packagedownload.resources.psobject.Properties.name[0]))) -Raw | ConvertFrom-Json
$PowerAutomateFlowJson = Get-Content -Path (Join-Path $($env:TEMP) ('Microsoft.Flow\flows\{0}\definition.json' -f $($packageName))) -Raw | ConvertFrom-Json
$PowerAutomateFlowDefinition = $PowerAutomateFlowJson.properties.definition
#endregion

Expand Down Expand Up @@ -218,12 +233,12 @@ $InputObject = [pscustomobject]@{
$options = New-PSDocumentOption -Option @{ 'Markdown.UseEdgePipes' = 'Always'; 'Markdown.ColumnPadding' = 'Single' };
$null = [PSDocs.Configuration.PSDocumentOption]$Options
$invokePSDocumentSplat = @{
Path = $templatePath
Name = $templateName
InputObject = $InputObject
Culture = 'en-us'
Option = $options
OutputPath = $OutputPath
Path = $templatePath
Name = $templateName
InputObject = $InputObject
Culture = 'en-us'
Option = $options
OutputPath = $OutputPath
InstanceName = $($PowerAutomateName.Split([IO.Path]::GetInvalidFileNameChars()) -join '_')
}
$markDownFile = Invoke-PSDocument @invokePSDocumentSplat
Expand Down
14 changes: 14 additions & 0 deletions tests/Test-PowerAutomateDocLocal.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<#
Script to test the PowerShell script New-PowerAutomateDoc.ps1 using a json file with the Logic App Workflow configuration
#>

$params = @{
EnvironmentName = '839eace6-59ab-4243-97ec-a5b8fcc104e4'
PowerAutomateName = 'LocalPowerAutomate'
OutputPath = $($env:TEMP)
FilePath = 'c:\temp\DevSecOpsCapabilityFlow_20240328153930.zip'
Verbose = $true
Debug = $true
}

. ..\src\New-PowerAutomateDoc.ps1 @params

0 comments on commit b73bb25

Please sign in to comment.