forked from mardahl/PSBucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-AddAADGroupToSPOListItemPermission.ps1
44 lines (39 loc) · 1.82 KB
/
Invoke-AddAADGroupToSPOListItemPermission.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<#
.SYNOPSIS
Gets Azure AD Group and sets its permissions on a Sharepoint List Item.
.DESCRIPTION
Connect to Azure AD to get the ID of a specific AAD Security Group, then connects to SharepointOnline to set that groups permissions on a list item.
.NOTES
Version: 1.0
Author: Michael Mardahl
Creation Date: Oktober 8th 2020
Purpose/Change: Initial script development
License: MIT, Please leave author credits
.EXAMPLE
Run the script as is after modyfying the declarations
#>
#Requires -Modules SharePointPnPPowerShellOnline, AzureAd
#Declarations
$SPOSite = "https://<tenantname>.sharepoint.com/sites/<MySiteName>" #List url in Sharepoint Online
$SPOList = "MyList" #The list that holds the items your wish to set permissions on
$ItemId = "xxxx" #Id op the list item
$SGName = "MySecurityGroup" #AAD Group Name
$Permissions = "Editor" #Find the right ones for your tenant by running Get-PnPRoleDefinition (it's language dependent!)
try {
#Connect to Sharepoint Online
Connect-PnPOnline -Url $SPOSite -UseWebLogin -ErrorAction Stop
#Connect to Azure AD
Connect-AzureAD -ErrorAction Stop
} catch {
Write-Output "Either Azure AD or Sharepoint login failed - Stopping script!"
exit 1
}
#Find AD Group ID from name and convert to SPO naming
$groupObj = Get-AzureADGroup -SearchString $SGName | Where-Object DisplayName -EQ $SGName
$groupId = 'c:0t.c|tenant|{0}' -f $groupObj.ObjectId
Write-Verbose "Found ID of group: $($groupObj.DisplayName)" -Verbose
#Testing group existence before doing anything rash
if($AADGroupSPO = Get-PnPUser -Identity $groupId){
Write-Verbose "Setting `"$Permissions`" for group: $($groupObj.DisplayName) on list item Id no. $ItemId " -Verbose
Set-PnPListItemPermission -Identity $ItemId -List $SPOList -AddRole $Permissions -User $groupId -ErrorAction Continue
}