Skip to content

Commit c8e54dd

Browse files
authored
Merge pull request #2291 from cboonham/chris-SupportApplicationPermissions
Add GraphAPI application permissions support
2 parents 358d239 + e9d6662 commit c8e54dd

5 files changed

+112
-38
lines changed

M365/Remove-MailboxExtendedProperty.ps1

+34-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
#Requires -Modules @{ ModuleName="ExchangeOnlineManagement"; ModuleVersion="3.4.0" }
5-
#Requires -Modules @{ ModuleName="Microsoft.Graph.Users"; ModuleVersion="2.24.0" }
6-
#Requires -Modules @{ ModuleName="Microsoft.Graph.Mail"; ModuleVersion="2.24.0" }
4+
#Requires -Modules @{ ModuleName="ExchangeOnlineManagement"; ModuleVersion="3.7.0" }
5+
#Requires -Modules @{ ModuleName="Microsoft.Graph.Users"; ModuleVersion="2.25.0" }
6+
#Requires -Modules @{ ModuleName="Microsoft.Graph.Mail"; ModuleVersion="2.25.0" }
77

88
<#
99
.SYNOPSIS
@@ -17,9 +17,17 @@
1717
1818
.EXAMPLE
1919
$mailboxExtendedProperty = Get-MailboxExtendedProperty -Identity fred@contoso.com | Where-Object { $_.PropertyName -like '*Some Pattern*' }
20+
21+
Delegated permissions:
22+
2023
$messagesWithExtendedProperty = .\Search-MailboxExtendedProperty.ps1 -MailboxExtendedProperty $mailboxExtendedProperty
2124
.\Remove-MailboxExtendedProperty.ps1 -MessagesWithExtendedProperty $messagesWithExtendedProperty
2225
26+
Application permissions:
27+
28+
$messagesWithExtendedProperty = .\Search-MailboxExtendedProperty.ps1 -MailboxExtendedProperty $mailboxExtendedProperty -UserPrincipalName fred@contoso.com
29+
.\Remove-MailboxExtendedProperty.ps1 -MessagesWithExtendedProperty $messagesWithExtendedProperty -UserPrincipalName fred@contoso.com
30+
2331
#>
2432
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
2533
param(
@@ -31,22 +39,33 @@ param(
3139
throw "The parameter MailboxExtendedProperty doesn't appear to be the result from running 'Search-MailboxExtendedProperty'."
3240
}
3341
})]
34-
$MessagesWithExtendedProperty
42+
$MessagesWithExtendedProperty,
43+
[Parameter(Mandatory = $false, Position = 1)]
44+
$UserPrincipalName
3545
)
3646

3747
process {
38-
# Get the current Microsoft Graph context
39-
$context = Get-MgContext
40-
if ($null -eq $context) {
41-
Write-Host -ForegroundColor Red "No valid context. Please connect to Microsoft Graph first."
42-
return
43-
}
48+
if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('UserPrincipalName')) {
49+
# Get the user information for the supplied user principal name
50+
$user = Get-MgUser -UserId $UserPrincipalName -Select 'displayName, id, mail, userPrincipalName'
51+
if ($null -eq $user) {
52+
Write-Host -ForegroundColor Red "No valid user. Please check the name and retry."
53+
return
54+
}
55+
} else {
56+
# Get the current Microsoft Graph context
57+
$context = Get-MgContext
58+
if ($null -eq $context) {
59+
Write-Host -ForegroundColor Red "No valid context. Please connect to Microsoft Graph first."
60+
return
61+
}
4462

45-
# Get the user information for the context
46-
$user = Get-MgUser -UserId $context.Account -Select 'displayName, id, mail, userPrincipalName'
47-
if ($null -eq $user) {
48-
Write-Host -ForegroundColor Red "No valid user. Please check the Microsoft Graph connection."
49-
return
63+
# Get the user information for the context
64+
$user = Get-MgUser -UserId $context.Account -Select 'displayName, id, mail, userPrincipalName'
65+
if ($null -eq $user) {
66+
Write-Host -ForegroundColor Red "No valid user. Please check the Microsoft Graph connection."
67+
return
68+
}
5069
}
5170

5271
Write-Host "Attempting to remove $($MessagesWithExtendedProperty.Count) extended properties from the mailbox of $($user.UserPrincipalName)."

M365/Search-MailboxExtendedProperty.ps1

+33-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
#Requires -Modules @{ ModuleName="ExchangeOnlineManagement"; ModuleVersion="3.4.0" }
5-
#Requires -Modules @{ ModuleName="Microsoft.Graph.Users"; ModuleVersion="2.24.0" }
6-
#Requires -Modules @{ ModuleName="Microsoft.Graph.Mail"; ModuleVersion="2.24.0" }
4+
#Requires -Modules @{ ModuleName="ExchangeOnlineManagement"; ModuleVersion="3.7.0" }
5+
#Requires -Modules @{ ModuleName="Microsoft.Graph.Users"; ModuleVersion="2.25.0" }
6+
#Requires -Modules @{ ModuleName="Microsoft.Graph.Mail"; ModuleVersion="2.25.0" }
77

88
<#
99
.SYNOPSIS
@@ -20,7 +20,14 @@
2020
2121
.EXAMPLE
2222
$mailboxExtendedProperty = Get-MailboxExtendedProperty -Identity fred@contoso.com | Where-Object { $_.PropertyName -like '*Some Pattern*' }
23+
24+
Delegated permissions:
25+
2326
$messagesWithExtendedProperty = .\Search-MailboxExtendedProperty.ps1 -MailboxExtendedProperty $mailboxExtendedProperty
27+
28+
Application permissions:
29+
30+
$messagesWithExtendedProperty = .\Search-MailboxExtendedProperty.ps1 -MailboxExtendedProperty $mailboxExtendedProperty -UserPrincipalName fred@contoso.com
2431
#>
2532
param(
2633
[Parameter(Mandatory = $true, Position = 0)]
@@ -31,7 +38,9 @@ param(
3138
throw "The parameter MailboxExtendedProperty doesn't appear to be the result from running 'Get-MailboxExtendedProperty'."
3239
}
3340
})]
34-
$MailboxExtendedProperty
41+
$MailboxExtendedProperty,
42+
[Parameter(Mandatory = $false, Position = 1)]
43+
$UserPrincipalName
3544
)
3645

3746
process {
@@ -69,18 +78,27 @@ process {
6978
# Messages found with the extended property
7079
$message = @()
7180

72-
# Get the current Microsoft Graph context
73-
$context = Get-MgContext
74-
if ($null -eq $context) {
75-
Write-Host -ForegroundColor Red "No valid context. Please connect to Microsoft Graph first."
76-
return
77-
}
81+
if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('UserPrincipalName')) {
82+
# Get the user information for the supplied user principal name
83+
$user = Get-MgUser -UserId $UserPrincipalName -Select 'displayName, id, mail, userPrincipalName'
84+
if ($null -eq $user) {
85+
Write-Host -ForegroundColor Red "No valid user. Please check the name and retry."
86+
return
87+
}
88+
} else {
89+
# Get the current Microsoft Graph context
90+
$context = Get-MgContext
91+
if ($null -eq $context) {
92+
Write-Host -ForegroundColor Red "No valid context. Please connect to Microsoft Graph first."
93+
return
94+
}
7895

79-
# Get the user information for the context
80-
$user = Get-MgUser -UserId $context.Account -Select 'displayName, id, mail, userPrincipalName'
81-
if ($null -eq $user) {
82-
Write-Host -ForegroundColor Red "No valid user. Please check the Microsoft Graph connection."
83-
return
96+
# Get the user information for the context
97+
$user = Get-MgUser -UserId $context.Account -Select 'displayName, id, mail, userPrincipalName'
98+
if ($null -eq $user) {
99+
Write-Host -ForegroundColor Red "No valid user. Please check the Microsoft Graph connection."
100+
return
101+
}
84102
}
85103

86104
Write-Host "Searching for mailbox items with the specified $($MailboxExtendedProperty.Count) extended properties in the mailbox of $($user.UserPrincipalName)."

M365/Test-MailboxExtendedProperty.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
#Requires -Modules @{ ModuleName="ExchangeOnlineManagement"; ModuleVersion="3.4.0" }
4+
#Requires -Modules @{ ModuleName="ExchangeOnlineManagement"; ModuleVersion="3.7.0" }
55

66
<#
77
.SYNOPSIS
@@ -14,7 +14,7 @@
1414
The identity of the user whose mailbox extended properties are to be retrieved.
1515
1616
.PARAMETER Threshold
17-
The quota threshold to check for having exceeded. Default is 0.9, which is 90% of the allowed quota.
17+
The quota threshold to check for having exceeded. Default is 1.0, which is 100% of the allowed quota.
1818
1919
.PARAMETER SelectFirst
2020
The number of sorted descending results to select, when checking any namespace or same name prefix quota. Default is 10.
@@ -30,7 +30,7 @@ param(
3030
$Identity,
3131
[Parameter(Mandatory = $false, Position = 1)]
3232
[ValidateRange(0.0, 1.0)]
33-
[double]$Threshold = 0.9,
33+
[double]$Threshold = 1.0,
3434
[Parameter(Mandatory = $false, Position = 2)]
3535
$SelectFirst = 10
3636
)

docs/M365/Remove-MailboxExtendedProperty.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@ Lastly, repeat the search to check the named properties no longer appear in the
1616

1717
### Syntax:
1818

19-
Example to search the mailbox for messages with any named properties matching the specific pattern and remove them from the messages.
19+
Example to search the mailbox for messages with any named properties matching the specific pattern and remove them from the messages, using delegated access.
2020
```PowerShell
2121
$mailboxExtendedProperty = Get-MailboxExtendedProperty -Identity fred@contoso.com | Where-Object { $_.PropertyName -like '*Some Pattern*' }
22+
2223
$messagesWithExtendedProperty = .\Search-MailboxExtendedProperty.ps1 -MailboxExtendedProperty $mailboxExtendedProperty
2324
.\Remove-MailboxExtendedProperty.ps1 -MessagesWithExtendedProperty $messagesWithExtendedProperty
2425
```
2526

27+
Example to search the mailbox for messages with any named properties matching the specific pattern and remove them from the messages, using application access.
28+
```PowerShell
29+
$mailboxExtendedProperty = Get-MailboxExtendedProperty -Identity fred@contoso.com | Where-Object { $_.PropertyName -like '*Some Pattern*' }
30+
31+
$messagesWithExtendedProperty = .\Search-MailboxExtendedProperty.ps1 -MailboxExtendedProperty $mailboxExtendedProperty -UserPrincipalName fred@contoso.com
32+
.\Remove-MailboxExtendedProperty.ps1 -MessagesWithExtendedProperty $messagesWithExtendedProperty -UserPrincipalName fred@contoso.com
33+
```
34+
2635
## Prerequisites
2736

2837
This script uses the [ExchangeOnlineManagement PowerShell module](Search-MailboxExtendedProperty.md#install-exchangeonlinemanagement-powershell-module) and an Exchange Online connection to be successfully established by a Tenant Admin.
@@ -48,3 +57,13 @@ To connect to Graph, using delegated access, and you don't know the credentials
4857
```PowerShell
4958
Connect-MgGraph -TenantId 2bbb42ba-e564-4f7b-9765-e19bc80c6123 -ClientId 8af900d8-db73-4918-81ef-3d35a873b6b2 -Scopes "User.Read Mail.ReadWrite" -UseDeviceCode
5059
```
60+
61+
To connect to Graph, using application access and a shared secret, to search by a tenant administrator against another mailbox in the tenant.
62+
63+
```PowerShell
64+
$clientId = "8af900d8-db73-4918-81ef-3d35a873b6b2"
65+
$clientSecret = "<your secret>"
66+
$secureClientSecret = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force
67+
$clientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $clientId, $secureClientSecret
68+
Connect-MgGraph -ClientSecretCredential $clientSecretCredential -TenantId 2bbb42ba-e564-4f7b-9765-e19bc80c6123
69+
```

docs/M365/Search-MailboxExtendedProperty.md

+22-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ There are some limitations: the search is limited to messages (extended properti
2626

2727
### Syntax:
2828

29-
Example to search the mailbox for messages with any named properties matching the specific pattern.
29+
Example to search the mailbox for messages with any named properties matching the specific pattern, using delegated access.
3030
```PowerShell
3131
$mailboxExtendedProperty = Get-MailboxExtendedProperty -Identity fred@contoso.com | Where-Object { $_.PropertyName -like '*Some Pattern*' }
32+
3233
$messagesWithExtendedProperty = .\Search-MailboxExtendedProperty.ps1 -MailboxExtendedProperty $mailboxExtendedProperty
3334
```
3435

36+
Example to search the mailbox for messages with any named properties matching the specific pattern, using application access.
37+
```PowerShell
38+
$mailboxExtendedProperty = Get-MailboxExtendedProperty -Identity fred@contoso.com | Where-Object { $_.PropertyName -like '*Some Pattern*' }
39+
40+
$messagesWithExtendedProperty = .\Search-MailboxExtendedProperty.ps1 -MailboxExtendedProperty $mailboxExtendedProperty -UserPrincipalName fred@contoso.com
41+
```
42+
3543
## Prerequisites
3644

3745
This script uses the [ExchangeOnlineManagement PowerShell module](#install-exchangeonlinemanagement-powershell-module) and an Exchange Online connection to be successfully established by a Tenant Admin.
@@ -58,17 +66,27 @@ To connect to Graph, using delegated access, and you don't know the credentials
5866
Connect-MgGraph -TenantId 2bbb42ba-e564-4f7b-9765-e19bc80c6123 -ClientId 8af900d8-db73-4918-81ef-3d35a873b6b2 -Scopes "User.Read Mail.ReadWrite" -UseDeviceCode
5967
```
6068

69+
To connect to Graph, using application access and a shared secret, to search by a tenant administrator against another mailbox in the tenant.
70+
71+
```PowerShell
72+
$clientId = "8af900d8-db73-4918-81ef-3d35a873b6b2"
73+
$clientSecret = "<your secret>"
74+
$secureClientSecret = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force
75+
$clientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $clientId, $secureClientSecret
76+
Connect-MgGraph -ClientSecretCredential $clientSecretCredential -TenantId 2bbb42ba-e564-4f7b-9765-e19bc80c6123
77+
```
78+
6179
### Install ExchangeOnlineManagement PowerShell module
6280

6381
``` PowerShell
64-
Install-Module ExchangeOnlineManagement -RequiredVersion 3.4.0
82+
Install-Module ExchangeOnlineManagement -RequiredVersion 3.7.0
6583
```
6684

6785
### Install Microsoft Graph PowerShell modules
6886

6987
``` PowerShell
70-
Install-Module Microsoft.Graph.Users -RequiredVersion 2.24.0
71-
Install-Module Microsoft.Graph.Mail -RequiredVersion 2.24.0
88+
Install-Module Microsoft.Graph.Users -RequiredVersion 2.25.0
89+
Install-Module Microsoft.Graph.Mail -RequiredVersion 2.25.0
7290
```
7391

7492
### Azure App registration

0 commit comments

Comments
 (0)