diff --git a/CHANGELOG.md b/CHANGELOG.md index b52236d4e..4e9dc8730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Improved `Add-PnPFile` cmdlet. It will now automatically checkout the file if `-CheckinType` parameter is specified. [#3403](https://github.com/pnp/powershell/pull/3403) - Improved the error message thrown when using `-ValidateConnection` with `Connect-PnPOnline` and it failing due to i.e. an expired ClientSecret so the reason of the failed connect becomes more clear. [#3440](https://github.com/pnp/powershell/pull/3440) - If a cmdlet gets renamed and an alias gets added for it for backwards compatibility, a cmdlet page for the alias will automatically be created so it can still be found in the documentation [#3455](https://github.com/pnp/powershell/pull/3455) +- Improved `Remove-PnPFlow` cmdlet to throw error if the Flow doesn't exist and also added verbose logging. [#3474](https://github.com/pnp/powershell/pull/3474) - Changed `Get-PnPContentType` to now also support `-Includes` to allow retrieval of additional properties of the content type [#3518](https://github.com/pnp/powershell/pull/3518) - `Get-PnPTeamsTeam` cmdlet throws error message if the team isn't found when `-Identity` parameter is specified. [#3502](https://github.com/pnp/powershell/pull/3502) - Improved `Get-PnPSiteCollectionAdmin ` cmdlet to allow retrieval of additional properties when `-Includes` parameter is specified. [#3521](https://github.com/pnp/powershell/pull/3521) diff --git a/documentation/Remove-PnPFlow.md b/documentation/Remove-PnPFlow.md index 7ca54e343..c0c3278eb 100644 --- a/documentation/Remove-PnPFlow.md +++ b/documentation/Remove-PnPFlow.md @@ -20,7 +20,7 @@ Removes the specified flow. ```powershell Remove-PnPFlow -Environment -Identity [-AsAdmin] - [-Force] [-Connection ] + [-Force] [-ThrowExceptionIfPowerAutomateNotFound] [-Connection ] ``` ## DESCRIPTION @@ -36,6 +36,14 @@ Remove-PnPFlow -Environment $environment -Identity fba63225-baf9-4d76-86a1-1b42c This removes the specified flow. +### Example 1 +```powershell +$environment = Get-PnPFlowEnvironment +Remove-PnPFlow -Environment $environment -Identity fba63225-baf9-4d76-86a1-1b42c917a182 -ThrowExceptionIfPowerAutomateNotFound +``` + +This removes the specified flow and throws an exception if the specified flow is not present. + ## PARAMETERS ### -AsAdmin @@ -99,6 +107,20 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -ThrowExceptionIfPowerAutomateNotFound +Switch parameter if an exception should be thrown if the requested flow does not exist (true) or if omitted, nothing will be returned in case the flow does not exist + +```yaml +Type: SwitchParameter +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Force If specified, no confirmation question will be asked. diff --git a/src/Commands/PowerPlatform/PowerAutomate/RemoveFlow.cs b/src/Commands/PowerPlatform/PowerAutomate/RemoveFlow.cs index fec036da4..2165089a2 100644 --- a/src/Commands/PowerPlatform/PowerAutomate/RemoveFlow.cs +++ b/src/Commands/PowerPlatform/PowerAutomate/RemoveFlow.cs @@ -1,6 +1,7 @@ using PnP.PowerShell.Commands.Base; using PnP.PowerShell.Commands.Base.PipeBinds; using PnP.PowerShell.Commands.Utilities.REST; +using System; using System.Management.Automation; namespace PnP.PowerShell.Commands.PowerPlatform.PowerAutomate @@ -17,6 +18,9 @@ public class RemoveFlow : PnPAzureManagementApiCmdlet [Parameter(Mandatory = false)] public SwitchParameter AsAdmin; + [Parameter(Mandatory = false)] + public SwitchParameter ThrowExceptionIfPowerAutomateNotFound; + [Parameter(Mandatory = false)] public SwitchParameter Force; @@ -27,7 +31,30 @@ protected override void ExecuteCmdlet() if (Force || ShouldContinue($"Remove flow with name '{flowName}'?", "Remove flow")) { - var result = RestHelper.DeleteAsync>(Connection.HttpClient, $"https://management.azure.com/providers/Microsoft.ProcessSimple{(AsAdmin ? "/scopes/admin" : "")}/environments/{environmentName}/flows/{flowName}?api-version=2016-11-01", AccessToken).GetAwaiter().GetResult(); + WriteVerbose($"Attempting to delete Flow with name {flowName}"); + if (ThrowExceptionIfPowerAutomateNotFound) + { + try + { + // Had to add this because DELETE doesn't throw error if invalid Flow Id or Name is provided + WriteVerbose($"Retrieving Flow with name {flowName} in environment ${environmentName}"); + var result = GraphHelper.GetAsync(Connection, $"https://management.azure.com/providers/Microsoft.ProcessSimple{(AsAdmin ? "/scopes/admin" : "")}/environments/{environmentName}/flows/{flowName}?api-version=2016-11-01", AccessToken).GetAwaiter().GetResult(); + if (result != null) + { + RestHelper.DeleteAsync(Connection.HttpClient, $"https://management.azure.com/providers/Microsoft.ProcessSimple{(AsAdmin ? "/scopes/admin" : "")}/environments/{environmentName}/flows/{flowName}?api-version=2016-11-01", AccessToken).GetAwaiter().GetResult(); + WriteVerbose($"Flow with name {flowName} deleted"); + } + } + catch + { + throw new Exception($"Cannot find flow with Identity '{flowName}'"); + } + } + else + { + RestHelper.DeleteAsync(Connection.HttpClient, $"https://management.azure.com/providers/Microsoft.ProcessSimple{(AsAdmin ? "/scopes/admin" : "")}/environments/{environmentName}/flows/{flowName}?api-version=2016-11-01", AccessToken).GetAwaiter().GetResult(); + WriteVerbose($"Flow with name {flowName} deleted"); + } } } }