-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGraphTutorial.ps1
69 lines (57 loc) · 1.9 KB
/
GraphTutorial.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
Write-Host 'PowerShell Graph Tutorial'
# Load settings
$settings = Get-Content './settings.json' -ErrorAction Stop | Out-String | ConvertFrom-Json
$clientId = $settings.clientId
$tenantId = $settings.tenantId
$graphScopes = $settings.graphUserScopes
# <UserAuthSnippet>
# Authenticate the user
Connect-MgGraph -ClientId $clientId -TenantId $tenantId -Scopes $graphScopes -UseDeviceAuthentication
# </UserAuthSnippet>
# <GetContextSnippet>
# Get the Graph context
Get-MgContext
# </GetContextSnippet>
# <SaveContextSnippet>
$context = Get-MgContext
# </SaveContextSnippet>
# <GetUserSnippet>
# Get the authenticated user by UPN
$user = Get-MgUser -UserId $context.Account -Select 'displayName, id, mail, userPrincipalName'
# </GetUserSnippet>
# <GreetUserSnippet>
Write-Host "Hello," $user.DisplayName
# For Work/school accounts, email is in Mail property
# Personal accounts, email is in UserPrincipalName
Write-Host "Email:", ($user.Mail ?? $user.UserPrincipalName)
# </GreetUserSnippet>
# <GetInboxSnippet>
Get-MgUserMailFolderMessage -UserId $user.Id -MailFolderId Inbox -Select `
"from,isRead,receivedDateTime,subject" -OrderBy "receivedDateTime DESC" `
-Top 25 | Format-Table Subject,@{n='From';e={$_.From.EmailAddress.Name}}, `
IsRead,ReceivedDateTime
# </GetInboxSnippet>
# <DefineMailSnippet>
$sendMailParams = @{
Message = @{
Subject = "Testing Microsoft Graph"
Body = @{
ContentType = "text"
Content = "Hello world!"
}
ToRecipients = @(
@{
EmailAddress = @{
Address = ($user.Mail ?? $user.UserPrincipalName)
}
}
)
}
}
# </DefineMailSnippet>
# <SendMailSnippet>
Send-MgUserMail -UserId $user.Id -BodyParameter $sendMailParams
# </SendMailSnippet>
Disconnect-MgGraph | Out-Null