Skip to content

Commit 7295d2c

Browse files
authored
Merge pull request #3474 from gautamdsheth/feature/1346
Feature #1346 - Improved Remove-PnPFlow cmdlet to throw error and added verbose logging
2 parents 6d9a34f + c35a603 commit 7295d2c

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
8282
- Improved `Add-PnPFile` cmdlet. It will now automatically checkout the file if `-CheckinType` parameter is specified. [#3403](https://github.com/pnp/powershell/pull/3403)
8383
- 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)
8484
- 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)
85+
- 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)
8586
- 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)
8687
- `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)
8788
- Improved `Get-PnPSiteCollectionAdmin ` cmdlet to allow retrieval of additional properties when `-Includes` parameter is specified. [#3521](https://github.com/pnp/powershell/pull/3521)

documentation/Remove-PnPFlow.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Removes the specified flow.
2020

2121
```powershell
2222
Remove-PnPFlow -Environment <PowerAutomateEnvironmentPipeBind> -Identity <PowerAutomateFlowPipeBind> [-AsAdmin]
23-
[-Force] [-Connection <PnPConnection>]
23+
[-Force] [-ThrowExceptionIfPowerAutomateNotFound] [-Connection <PnPConnection>]
2424
```
2525

2626
## DESCRIPTION
@@ -36,6 +36,14 @@ Remove-PnPFlow -Environment $environment -Identity fba63225-baf9-4d76-86a1-1b42c
3636

3737
This removes the specified flow.
3838

39+
### Example 1
40+
```powershell
41+
$environment = Get-PnPFlowEnvironment
42+
Remove-PnPFlow -Environment $environment -Identity fba63225-baf9-4d76-86a1-1b42c917a182 -ThrowExceptionIfPowerAutomateNotFound
43+
```
44+
45+
This removes the specified flow and throws an exception if the specified flow is not present.
46+
3947
## PARAMETERS
4048

4149
### -AsAdmin
@@ -99,6 +107,20 @@ Accept pipeline input: False
99107
Accept wildcard characters: False
100108
```
101109
110+
### -ThrowExceptionIfPowerAutomateNotFound
111+
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
112+
113+
```yaml
114+
Type: SwitchParameter
115+
Parameter Sets: (All)
116+
117+
Required: False
118+
Position: Named
119+
Default value: None
120+
Accept pipeline input: False
121+
Accept wildcard characters: False
122+
```
123+
102124
### -Force
103125
If specified, no confirmation question will be asked.
104126

src/Commands/PowerPlatform/PowerAutomate/RemoveFlow.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using PnP.PowerShell.Commands.Base;
22
using PnP.PowerShell.Commands.Base.PipeBinds;
33
using PnP.PowerShell.Commands.Utilities.REST;
4+
using System;
45
using System.Management.Automation;
56

67
namespace PnP.PowerShell.Commands.PowerPlatform.PowerAutomate
@@ -17,6 +18,9 @@ public class RemoveFlow : PnPAzureManagementApiCmdlet
1718
[Parameter(Mandatory = false)]
1819
public SwitchParameter AsAdmin;
1920

21+
[Parameter(Mandatory = false)]
22+
public SwitchParameter ThrowExceptionIfPowerAutomateNotFound;
23+
2024
[Parameter(Mandatory = false)]
2125
public SwitchParameter Force;
2226

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

2832
if (Force || ShouldContinue($"Remove flow with name '{flowName}'?", "Remove flow"))
2933
{
30-
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();
34+
WriteVerbose($"Attempting to delete Flow with name {flowName}");
35+
if (ThrowExceptionIfPowerAutomateNotFound)
36+
{
37+
try
38+
{
39+
// Had to add this because DELETE doesn't throw error if invalid Flow Id or Name is provided
40+
WriteVerbose($"Retrieving Flow with name {flowName} in environment ${environmentName}");
41+
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();
42+
if (result != null)
43+
{
44+
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();
45+
WriteVerbose($"Flow with name {flowName} deleted");
46+
}
47+
}
48+
catch
49+
{
50+
throw new Exception($"Cannot find flow with Identity '{flowName}'");
51+
}
52+
}
53+
else
54+
{
55+
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();
56+
WriteVerbose($"Flow with name {flowName} deleted");
57+
}
3158
}
3259
}
3360
}

0 commit comments

Comments
 (0)