-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fn-NewRosterPlanner.ps1
79 lines (62 loc) · 2.7 KB
/
Fn-NewRosterPlanner.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function Fn-NewRosterPlanner {
param (
[Parameter(Mandatory = $true)]
[string]$Domain,
[Parameter(Mandatory = $true)]
[string]$UPN,
[Parameter(Mandatory = $true)]
[string]$NameOfPlanner
)
try {
# Check if PnP module is installed
$pnpModule = Get-Module -Name "PnP.PowerShell" -ListAvailable
if (-not $pnpModule) {
# Install and Import the PnP module
Install-Module -Name PnP.PowerShell -RequiredVersion 1.12.0 -Force -scope CurrentUser
Import-Module -Name PnP.PowerShell -RequiredVersion 1.12.0 -Force
}
# Connect to the tenant
Connect-PnPOnline -Url "$Domain.sharepoint.com" -Interactive
# Get the graph access token
$Token = Get-PnPGraphAccessToken
# Create a ROSTER PLAN
$headerParams = @{
'Authorization' = "Bearer $Token"
}
$requestBody1 = @{
"@odata.type" = "#microsoft.graph.plannerRoster"
}
$RosterDetails = Invoke-RestMethod -Method Post -Uri "https://graph.microsoft.com/beta/planner/rosters" -Body $requestBody1 -Headers $headerParams
$ID = $RosterDetails.id
# Add one user to the roster
$headerParams = @{
'Authorization' = "Bearer $Token"
'Content-Type' = 'application/json'
}
$requestBody2 = @{
"@odata.type" = "#microsoft.graph.plannerRosterMember"
"userId" = $UPN
}
$AddMembers= Invoke-RestMethod -Method Post -Uri "https://graph.microsoft.com/beta/planner/rosters/$ID/members" -Headers $headerParams -Body ($requestBody2 | ConvertTo-Json)
# Creating the Planner Plan linked with the roster
$headerParams = @{
'Authorization' = "Bearer $Token"
'Content-Type' = 'application/json'
}
$requestBody3 = @{
"container" = @{
"url" = "https://graph.microsoft.com/beta/planner/rosters/$ID"
}
"title" = "$NameOfPlanner"
}
$Response = Invoke-RestMethod -Method Post -Uri "https://graph.microsoft.com/beta/planner/plans" -Headers $headerParams -Body ($requestBody3 | ConvertTo-Json)
$URL = "https://tasks.office.com/$Domain.onmicrosoft.com/en-US/Home/Planner/#/plantaskboard?groupId=$ID&planId=$($Response.id)"
$URL
Start-Process $URL
}
catch {
Write-Host "An error occurred: $_" -ForegroundColor Red
}
}
# Call the function with parameters
Fn-NewRosterPlanner -Domain "Nectaa" -UPN "[email protected]" -NameOfPlanner "Bhola"