Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #1346 - Improved Remove-PnPFlow cmdlet to throw error and added verbose logging #3474

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion documentation/Remove-PnPFlow.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Removes the specified flow.

```powershell
Remove-PnPFlow -Environment <PowerAutomateEnvironmentPipeBind> -Identity <PowerAutomateFlowPipeBind> [-AsAdmin]
[-Force] [-Connection <PnPConnection>]
[-Force] [-ThrowExceptionIfPowerAutomateNotFound] [-Connection <PnPConnection>]
```

## DESCRIPTION
Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand Down
29 changes: 28 additions & 1 deletion src/Commands/PowerPlatform/PowerAutomate/RemoveFlow.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand All @@ -27,7 +31,30 @@ protected override void ExecuteCmdlet()

if (Force || ShouldContinue($"Remove flow with name '{flowName}'?", "Remove flow"))
{
var result = RestHelper.DeleteAsync<RestResultCollection<Model.PowerPlatform.PowerAutomate.Flow>>(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<Model.PowerPlatform.PowerAutomate.Flow>(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");
}
}
}
}
Expand Down
Loading