Skip to content

Commit d3a64a9

Browse files
[New-Commandlet Get-PnPDeletedFlow] to list all the deleted Power Automate flows (#4396)
Co-authored-by: Gautam Sheth <gautamdsheth@outlook.com>
1 parent 555728e commit d3a64a9

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
schema: 2.0.0
4+
applicable: SharePoint Online
5+
online version: https://pnp.github.io/powershell/cmdlets/Get-PnPDeletedFlow.html
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
title: Get-PnPDeletedFlow
8+
---
9+
10+
# Get-PnPDeletedFlow
11+
12+
## SYNOPSIS
13+
14+
**Required Permissions**
15+
16+
* Azure: management.azure.com
17+
18+
**Information**
19+
20+
* To use this command, you must be a Global or Power Platform administrator.
21+
22+
**Note**
23+
24+
* A Power Automate flow is soft-deleted when:
25+
* It's a non-solution flow.
26+
* It's been deleted less than 21 days ago.
27+
28+
Returns all soft-deleted Power Automate flows within an environment
29+
30+
## SYNTAX
31+
32+
### All (Default)
33+
```powershell
34+
Get-PnPDeletedFlow [-Environment <PowerAutomateEnvironmentPipeBind>] [-Connection <PnPConnection>] [-Verbose]
35+
```
36+
37+
38+
## DESCRIPTION
39+
This cmdlet returns Deleted Power Automate Flows meeting the specified criteria.
40+
41+
## EXAMPLES
42+
43+
### Example 1
44+
```powershell
45+
Get-PnPDeletedFlow
46+
```
47+
Returns all the deleted flows in the default Power Platform environment belonging to any user
48+
49+
### Example 2
50+
```powershell
51+
Get-PnPPowerPlatformEnvironment -Identity "MyOrganization (default)" | Get-PnPDeletedFlow
52+
```
53+
Returns all the deleted flows for a given Power Platform environment belonging to the any user
54+
55+
56+
## PARAMETERS
57+
58+
### -Environment
59+
The name of the Power Platform environment or an Environment object to retrieve the available flows for.
60+
61+
```yaml
62+
Type: PowerAutomateEnvironmentPipeBind
63+
Parameter Sets: (All)
64+
Aliases:
65+
66+
Required: False
67+
Position: Named
68+
Default value: The default environment
69+
Accept pipeline input: False
70+
Accept wildcard characters: False
71+
```
72+
73+
74+
### -Connection
75+
Optional connection to be used by the cmdlet.
76+
Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.
77+
78+
```yaml
79+
Type: PnPConnection
80+
Parameter Sets: (All)
81+
Aliases:
82+
83+
Required: False
84+
Position: Named
85+
Default value: None
86+
Accept pipeline input: False
87+
Accept wildcard characters: False
88+
```
89+
90+
91+
### -Verbose
92+
When provided, additional debug statements will be shown while executing the cmdlet.
93+
94+
```yaml
95+
Type: SwitchParameter
96+
Parameter Sets: (All)
97+
98+
Required: False
99+
Position: Named
100+
Default value: None
101+
Accept pipeline input: False
102+
Accept wildcard characters: False
103+
```
104+
105+
## RELATED LINKS
106+
107+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Text.Json.Serialization;
3+
4+
namespace PnP.PowerShell.Commands.Model.PowerPlatform.PowerAutomate
5+
{
6+
/// <summary>
7+
/// Contains information on one Microsoft Power Automate Flow
8+
/// </summary>
9+
public class DeletedFlow
10+
{
11+
/// <summary>
12+
/// Name of the Flow as its Flow GUID
13+
/// </summary>
14+
public string Name { get; set;}
15+
16+
/// <summary>
17+
/// The friendly name of the Flow as can be seen through flow.microsoft.com
18+
/// </summary>
19+
public string DisplayName { get; set; }
20+
21+
/// <summary>
22+
/// Date and time at which this Flow has last been modified
23+
/// </summary>
24+
public DateTime? LastModifiedTime { get; set; }
25+
26+
public DeletedFlow(string name, string displayName, DateTime? lastModifiedTime)
27+
{
28+
Name = name;
29+
DisplayName = displayName;
30+
LastModifiedTime = lastModifiedTime;
31+
}
32+
}
33+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using PnP.PowerShell.Commands.Base;
2+
using PnP.PowerShell.Commands.Base.PipeBinds;
3+
using PnP.PowerShell.Commands.Utilities.REST;
4+
using System;
5+
using System.Linq;
6+
using System.Management.Automation;
7+
using PnP.PowerShell.Commands.Enums;
8+
using PnP.PowerShell.Commands.Utilities;
9+
using PnP.PowerShell.Commands.Model.PowerPlatform.PowerAutomate;
10+
11+
namespace PnP.PowerShell.Commands.PowerPlatform.PowerAutomate
12+
{
13+
[Cmdlet(VerbsCommon.Get, "PnPDeletedFlow", DefaultParameterSetName = ParameterSet_ALL)]
14+
public class GetDeletedFlow : PnPAzureManagementApiCmdlet
15+
{
16+
private const string ParameterSet_ALL = "All";
17+
18+
[Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ParameterSet_ALL)]
19+
public PowerPlatformEnvironmentPipeBind Environment;
20+
21+
protected override void ExecuteCmdlet()
22+
{
23+
string environmentName = null;
24+
string baseUrl = PowerPlatformUtility.GetPowerAutomateEndpoint(Connection.AzureEnvironment);
25+
if (ParameterSpecified(nameof(Environment)))
26+
{
27+
environmentName = Environment.GetName();
28+
29+
WriteVerbose($"Using environment as provided '{environmentName}'");
30+
}
31+
else
32+
{
33+
var environments = GraphHelper.GetResultCollection<Model.PowerPlatform.Environment.Environment>(this, Connection, baseUrl + "/providers/Microsoft.ProcessSimple/environments?api-version=2016-11-01", AccessToken);
34+
environmentName = environments.FirstOrDefault(e => e.Properties.IsDefault.HasValue && e.Properties.IsDefault == true)?.Name;
35+
36+
if(string.IsNullOrEmpty(environmentName))
37+
{
38+
throw new Exception($"No default environment found, please pass in a specific environment name using the {nameof(Environment)} parameter");
39+
}
40+
41+
WriteVerbose($"Using default environment as retrieved '{environmentName}'");
42+
}
43+
44+
45+
WriteVerbose($"Retrieving all Power Automate Flows within environment '{environmentName}'");
46+
47+
var flowUrl = $"{baseUrl}/providers/Microsoft.ProcessSimple/scopes/admin/environments/{environmentName}/v2/flows?api-version=2016-11-01&include=softDeletedFlows";
48+
var results = GraphHelper.GetResultCollection<Model.PowerPlatform.PowerAutomate.Flow>(this, Connection, flowUrl, AccessToken);
49+
50+
var deletedFlowList = results
51+
.Where(flow => flow.Properties.StateRaw == "Deleted")
52+
.Select(flow => new Model.PowerPlatform.PowerAutomate.DeletedFlow(
53+
flow.Name,
54+
flow.Properties.DisplayName,
55+
flow.Properties.LastModifiedTime
56+
))
57+
.ToList();
58+
WriteObject(deletedFlowList, true);
59+
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)