Skip to content

Commit 18733cd

Browse files
authored
Merge pull request #2710 from gautamdsheth/feature/filter-sp
Feature: Added Filter parameter to service principal cmdlet
2 parents 3cef98a + 42d801b commit 18733cd

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2323
- Added `-KeyColumn` to `Add-PnPDataRowsToSiteTemplate` which allows for overwriting existing list items in a site template [#2616](https://github.com/pnp/powershell/pull/2616)
2424
- Added `IsTeamsConnected`, `IsTeamsChannelConnected` and `TeamChannelType` to be returned when `Get-PnPTenantSite` cmdlet is executed. [#2656](https://github.com/pnp/powershell/pull/2656)
2525
- Added `-EnvironmentVariable` parameter to `Connect-PnPOnline` to connect using Azure environment variables. [#2681](https://github.com/pnp/powershell/pull/2681)
26+
- Added `-Filter` parameter to `Get-PnPAzureADServicePrincipal` cmdlet to retrieve specific application registrations based on filter conditions. It supports simple and advanced queries. [#2710](https://github.com/pnp/powershell/pull/2710)
2627
- Added `-Filter` parameter to `Get-PnPMicrosoft365Group` cmdlet to retrieve specific M365 groups based on filter conditions. It supports simple and advanced queries.
2728

2829
### Changed

documentation/Get-PnPAzureADServicePrincipal.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Gets service principal/application registrations in Azure Active Directory.
2222
### All
2323

2424
```powershell
25-
Get-PnPAzureADServicePrincipal [-Connection <PnPConnection>]
25+
Get-PnPAzureADServicePrincipal [-Filter <string>] [-Connection <PnPConnection>]
2626
```
2727

2828
### By App Id
@@ -83,6 +83,13 @@ Get-PnPAzureADServicePrincipal -AppName "My application"
8383

8484
Retrieves the application registration with the name "My application" from Azure Active Directory
8585

86+
### EXAMPLE 5
87+
```powershell
88+
Get-PnPAzureADServicePrincipal -Filter "startswith(description, 'contoso')"
89+
```
90+
91+
Retrieves the application registration with the description starting with "contoso" from Azure Active Directory. This example demonstrates using Advanced Query capabilities (see: https://learn.microsoft.com/graph/aad-advanced-queries?tabs=http#group-properties).
92+
8693
## PARAMETERS
8794

8895
### -AppId
@@ -141,6 +148,20 @@ Accept pipeline input: False
141148
Accept wildcard characters: False
142149
```
143150
151+
### -Filter
152+
Specify the query to pass to Graph API in $filter.
153+
154+
```yaml
155+
Type: String
156+
Parameter Sets: Filter
157+
158+
Required: False
159+
Position: Named
160+
Default value: None
161+
Accept pipeline input: False
162+
Accept wildcard characters: False
163+
```
164+
144165
## RELATED LINKS
145166
146167
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

src/Commands/Apps/GetAzureADServicePrincipal.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ public class GetAzureADServicePrincipal : PnPGraphCmdlet
3030
public string AppName;
3131

3232
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYBUILTINTYPE)]
33-
public ServicePrincipalBuiltInType BuiltInType;
33+
public ServicePrincipalBuiltInType BuiltInType;
34+
35+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ALL)]
36+
public string Filter;
3437

3538
protected override void ExecuteCmdlet()
3639
{
3740
AzureADServicePrincipal servicePrincipal = null;
38-
switch(ParameterSetName)
41+
switch (ParameterSetName)
3942
{
4043
case ParameterSet_BYAPPID:
4144
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByAppId(Connection, AccessToken, AppId);
@@ -50,12 +53,12 @@ protected override void ExecuteCmdlet()
5053
servicePrincipal = ServicePrincipalUtility.GetServicePrincipalByBuiltInType(Connection, AccessToken, BuiltInType);
5154
break;
5255
case ParameterSet_ALL:
53-
var servicePrincipals = ServicePrincipalUtility.GetServicePrincipals(Connection, AccessToken);
56+
var servicePrincipals = ServicePrincipalUtility.GetServicePrincipals(Connection, AccessToken, Filter);
5457
WriteObject(servicePrincipals, true);
5558
return;
5659
}
5760

58-
if(servicePrincipal == null)
61+
if (servicePrincipal == null)
5962
{
6063
throw new PSArgumentException("Service principal not found");
6164
}

src/Commands/Utilities/ServicePrincipalUtility.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@ internal static class ServicePrincipalUtility
1616
/// <summary>
1717
/// Returns all service principals
1818
/// </summary>
19-
public static List<AzureADServicePrincipal> GetServicePrincipals(PnPConnection connection, string accesstoken)
19+
public static List<AzureADServicePrincipal> GetServicePrincipals(PnPConnection connection, string accesstoken, string filter = null)
2020
{
21-
var result = Utilities.REST.GraphHelper.GetResultCollectionAsync<AzureADServicePrincipal>(connection, $"https://{connection.GraphEndPoint}/v1.0/servicePrincipals", accesstoken).GetAwaiter().GetResult();
21+
string requestUrl = $"https://{connection.GraphEndPoint}/v1.0/servicePrincipals";
22+
Dictionary<string, string> additionalHeaders = null;
23+
if (!string.IsNullOrEmpty(filter))
24+
{
25+
requestUrl = $"{requestUrl}?$filter=({filter})";
26+
additionalHeaders = new Dictionary<string, string>
27+
{
28+
{ "ConsistencyLevel", "eventual" }
29+
};
30+
}
31+
var result = REST.GraphHelper.GetResultCollectionAsync<AzureADServicePrincipal>(connection, requestUrl, accesstoken, additionalHeaders: additionalHeaders).GetAwaiter().GetResult();
2232
return result.ToList();
2333
}
2434

@@ -28,7 +38,7 @@ public static List<AzureADServicePrincipal> GetServicePrincipals(PnPConnection c
2838
public static AzureADServicePrincipal GetServicePrincipalByBuiltInType(PnPConnection connection, string accesstoken, ServicePrincipalBuiltInType builtInType)
2939
{
3040
AzureADServicePrincipal result = null;
31-
switch(builtInType)
41+
switch (builtInType)
3242
{
3343
case ServicePrincipalBuiltInType.MicrosoftGraph:
3444
result = ServicePrincipalUtility.GetServicePrincipalByAppId(connection, accesstoken, new Guid("00000003-0000-0000-c000-000000000000"));
@@ -53,13 +63,13 @@ public static AzureADServicePrincipal GetServicePrincipalByObjectId(PnPConnectio
5363
try
5464
{
5565
result = Utilities.REST.GraphHelper.GetResultCollectionAsync<AzureADServicePrincipal>(connection, $"https://{connection.GraphEndPoint}/v1.0/servicePrincipals?$filter=id eq '{objectId}'", accesstoken).GetAwaiter().GetResult();
56-
66+
5767
var servicePrincipal = result.FirstOrDefault();
5868
servicePrincipal.AppRoles.ForEach(ar => ar.ServicePrincipal = servicePrincipal);
5969
return servicePrincipal;
6070
}
6171
catch (Exception) { }
62-
return null;
72+
return null;
6373
}
6474

6575
/// <summary>
@@ -71,7 +81,7 @@ public static AzureADServicePrincipal GetServicePrincipalByAppId(PnPConnection c
7181
try
7282
{
7383
result = Utilities.REST.GraphHelper.GetResultCollectionAsync<AzureADServicePrincipal>(connection, $"https://{connection.GraphEndPoint}/v1.0/servicePrincipals?$filter=appid eq '{appId}'", accesstoken).GetAwaiter().GetResult();
74-
84+
7585
var servicePrincipal = result.FirstOrDefault();
7686
servicePrincipal.AppRoles.ForEach(ar => ar.ServicePrincipal = servicePrincipal);
7787
return servicePrincipal;
@@ -89,7 +99,7 @@ public static AzureADServicePrincipal GetServicePrincipalByAppName(PnPConnection
8999
try
90100
{
91101
result = Utilities.REST.GraphHelper.GetResultCollectionAsync<AzureADServicePrincipal>(connection, $"https://{connection.GraphEndPoint}/v1.0/servicePrincipals?$filter=displayName eq '{appName}'", accesstoken).GetAwaiter().GetResult();
92-
102+
93103
var servicePrincipal = result.FirstOrDefault();
94104
servicePrincipal.AppRoles.ForEach(ar => ar.ServicePrincipal = servicePrincipal);
95105
return servicePrincipal;
@@ -205,7 +215,7 @@ public static void RemoveServicePrincipalRoleAssignment(PnPConnection connection
205215
{
206216
Utilities.REST.GraphHelper.DeleteAsync(connection, $"https://{connection.GraphEndPoint}/v1.0/servicePrincipals/{principalToRemoveRoleFrom.Id}/appRoleAssignments/{assignment.Id}", accesstoken).GetAwaiter().GetResult();
207217
}
208-
}
218+
}
209219

210220
/// <summary>
211221
/// Removes the provided role from the role assignments of the provided service principal
@@ -243,7 +253,7 @@ public static void RemoveServicePrincipalRoleAssignment(PnPConnection connection
243253
Utilities.REST.GraphHelper.DeleteAsync(connection, $"https://{connection.GraphEndPoint}/v1.0/servicePrincipals/{principalToRemoveRoleFrom.Id}/appRoleAssignments/{assignment.Id}", accesstoken).GetAwaiter().GetResult();
244254
}
245255
}
246-
}
256+
}
247257

248258
/// <summary>
249259
/// Removes a role assignment from the provided service principal
@@ -255,6 +265,6 @@ public static void RemoveServicePrincipalRoleAssignment(PnPConnection connection
255265
public static void RemoveServicePrincipalRoleAssignment(PnPConnection connection, string accesstoken, AzureADServicePrincipalAppRoleAssignment appRoleAssignmenToRemove)
256266
{
257267
Utilities.REST.GraphHelper.DeleteAsync(connection, $"https://{connection.GraphEndPoint}/v1.0/servicePrincipals/{appRoleAssignmenToRemove.PrincipalId}/appRoleAssignments/{appRoleAssignmenToRemove.Id}", accesstoken).GetAwaiter().GetResult();
258-
}
268+
}
259269
}
260270
}

0 commit comments

Comments
 (0)