-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCreate a Guest User.ps1
61 lines (52 loc) · 1.8 KB
/
Create a Guest User.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
#Define App Reg Details
# https://docs.microsoft.com/en-us/graph/api/invitation-post?view=graph-rest-1.0&tabs=http
$clientID = "your Application ID"
$Clientsecret = "your Secret"
$tenantID = "your Tenant ID"
# Set Variables
#Guest Details
$GuestUserName = "Michael Seidl (GMAIL)"
$GuestUserMail = "[email protected]"
#Send Invitation CC to this USer
$CCRecipientName = "Michael Seidl"
$CCRecipientMail = "[email protected]"
#Add Personal Text do Invite Mail
$InviteMessage = "You have been invited to join the Tenant au2mator.com"
$InviteRedirectURL="https://au2mator.com" #URL where the USer is redirected after Invite Acceptance
#Auth MS Graph API and Get Header
$tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $clientID
Client_Secret = $Clientsecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
"Authorization" = "Bearer $($tokenResponse.access_token)"
"Content-type" = "application/json"
}
#Build Request
$URL = "https://graph.microsoft.com/v1.0/invitations"
$Method = "POST"
$body = @"
{
"invitedUserEmailAddress":"$GuestUserMail",
"inviteRedirectUrl":"$InviteRedirectURL",
"invitedUserDisplayName":"$GuestUserName",
"sendInvitationMessage": true,
"invitedUserMessageInfo": {
"messageLanguage": null,
"ccRecipients": [
{
"emailAddress": {
"name": "$CCRecipientName",
"address": "$CCRecipientMail"
}
}
],
"customizedMessageBody": "$InviteMessage"
}
}
"@
#Send Request
Invoke-RestMethod -Method $Method -Uri $URL -Body $body -Headers $headers