Skip to content

Commit 7240485

Browse files
authored
Merge branch 'dev' into master
2 parents 55405b3 + 4171b76 commit 7240485

8 files changed

+183
-27
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Microsoft Graph PowerShell SDK Preview
1+
# Microsoft Graph PowerShell SDK
22
The Microsoft Graph PowerShell SDK is a collection of PowerShell modules that contain commands for calling Microsoft Graph service.
33

44
# Modules

samples/0-InstallModule.ps1

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# Installing the Module
22

3-
# Installing the Graph PowerShell module for the Beta API with no previous versions installed
3+
# Installing the Graph PowerShell module with no previous versions installed
44
Install-module Microsoft.Graph
55

6+
# If upgrading from our preview modules, run install-module with AllowClobber and Force parameter to avoid command name conflicts
7+
Install-Module Microsoft.Graph -AllowClobber -Force
8+
9+
# Updating from an earlier version of MS Graph PowerShell installed from PS Gallery
10+
Update-module Microsoft.Graph
11+
612
# Uninstalling the old preview version, before installing the new
713

814
# Remove the main meta module

samples/3-TenantInformation.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Get-MgOrganization | Select-Object DisplayName, City, State, Country, PostalCode
77
Get-MgOrganization | Select-Object -expand AssignedPlans
88

99
## List application registrations in the tenant
10-
Get-MgApplication | Select-Object DisplayName, Appid, WebRedirectUris
10+
Get-MgApplication | Select-Object DisplayName, Appid, SignInAudience
1111

1212
## List service principals in the tenant
13-
get-MgServicePrincipal | Select-Object id, AppDisplayName | Where-Object { $_.AppDisplayName -like "*powershell*" }
13+
Get-MgServicePrincipal | Select-Object id, AppDisplayName | Where-Object { $_.AppDisplayName -like "*powershell*" }
1414

samples/5-Teams.ps1

+57-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1+
# Switch to beta profile. This loads cmdlets that call MS Graph beta endpoint.
2+
Select-MgProfile -Name beta
3+
4+
# Create a new team.
5+
$TeamName = "2020 Interns"
6+
New-MgTeam -DisplayName $TeamName -Description $TeamName `
7+
-AdditionalProperties @{
8+
"[email protected]" = "https://graph.microsoft.com/beta/teamsTemplates('standard')"
9+
}
10+
11+
# Filter groups by displayName and resourceProvisioningOptions to find team.
12+
$InternsTeam = Get-MgGroup -Filter "StartsWith(DisplayName, '$TeamName')" `
13+
| Where-Object { $_.ResourceProvisioningOptions -Contains "Team" }
14+
15+
# Add team owner.
16+
$teamOwner = Get-MgUser -UserId "{TEAM_OWNER_UPN}"
17+
New-MgTeamMember -TeamId $InternsTeam.Id -Roles "owner" `
18+
-AdditionalProperties @{
19+
"@odata.type" = "#microsoft.graph.aadUserConversationMember";
20+
"[email protected]" = "https://graph.microsoft.com/beta/users/" + $teamOwner.Id
21+
}
22+
23+
# Filter users to find users who have a UPN that starts with 't-'.
24+
$TeamMembers = Get-MgUser -Filter "startswith(userPrincipalName, 't-')"
25+
26+
# Add team members.
27+
foreach ($teamMember in $TeamMembers) {
28+
New-MgTeamMember -TeamId $InternsTeam.Id -Roles "member" `
29+
-AdditionalProperties @{
30+
"@odata.type" = "#microsoft.graph.aadUserConversationMember";
31+
"[email protected]" = "https://graph.microsoft.com/beta/users/" + $teamMember.Id
32+
}
33+
}
34+
35+
# Send a welcome message to the channel.
36+
$PrimaryChannel = Get-MgTeamPrimaryChannel -TeamId $InternsTeam.Id
37+
New-MgTeamChannelMessage -TeamId $InternsTeam.Id `
38+
-ChannelId $PrimaryChannel.Id `
39+
-Body @{
40+
Content = "Welcome to Teams!"
41+
}
42+
43+
# Delete team.
44+
Remove-MgGroup -GroupId $InternsTeam.Id
45+
146
# Teams Chat snippets
247

348
# Get list of 1:1 chats
@@ -7,17 +52,24 @@ Get-MgChat
752
Get-MgChatMessage -chatId $chatId
853

954
# Send a message in that 1:1 chat
10-
New-MgChatMessage -chatId $chatId -BodyContent "Hi from VSCode again!"
55+
New-MgChatMessage -chatId $chatId -Body @{ Content = "Hi from VSCode again!" }
1156

1257
# Mention a user in a channel message.
13-
$UserToMention = Get-MGUser -UserId $userUPN
58+
$User = Get-MgUser -UserId $userUPN | select id, displayName, userIdentityType
59+
$UserToMention = @{
60+
Id = $User.Id;
61+
DisplayName = $User.DisplayName;
62+
UserIdentityType = "aadUser";
63+
}
1464

1565
New-MgTeamChannelMessage -ChannelId $ChannelId -TeamId $TeamId `
16-
-BodyContentType "html" `
17-
-BodyContent "Welcome to the channel! <at id='0'>$($UserToMention.DisplayName)</at>" `
66+
-Body @{ `
67+
ContentType = "html"; `
68+
Content = "Welcome to the channel! <at id='0'>$($UserToMention.DisplayName)</at>" `
69+
} `
1870
-Mentions @( `
1971
@{ `
2072
id = 0; `
2173
mentionText = $UserToMention.DisplayName; `
2274
mentioned = @{user = $UserToMention } `
23-
})
75+
})

samples/8-ConditionalAccess.ps1

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#Minimum required parameters to create a conditional access policy
22

3-
New-MgConditionalAccessPolicy -DisplayName 'Minimum required Parameters' `
4-
-GrantControlBuiltInControls @('mfa') `
3+
New-MgIdentityConditionalAccessPolicy -DisplayName 'Minimum required Parameters' `
4+
-GrantControls @{ `
5+
BuiltInControls = @('mfa'); `
6+
Operator = 'OR' `
7+
} `
58
-State 'disabled' `
6-
-GrantControlOperator 'OR' `
79
-Conditions @{ `
8-
applications = @{includeApplications = 'none'}; `
9-
users = @{includeUsers = 'none'} `
10+
Applications = @{includeApplications = 'none'}; `
11+
Users = @{includeUsers = 'none'} `
1012
}
1113

1214
# Get a list of conditional access policies where DisplayName starts with Minimum.
13-
Get-MgConditionalAccessPolicy -Filter "StartsWith(DisplayName, 'Minimum')"
15+
Get-MgIdentityConditionalAccessPolicy -Filter "StartsWith(DisplayName, 'Minimum')"

samples/9-Applications.ps1

+42-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
1+
# Incremental scope consent.
2+
Connect-Graph -Scopes "Application.ReadWrite.All"
13

24
# Create an application for use with DeviceCodeFlow
3-
$app1 = new-mgApplication -displayName "DeviceCodeFlowApp" `
5+
$app1 = New-MgApplication -displayName "DeviceCodeFlowApp" `
46
-IsFallbackPublicClient `
5-
-PublicClientRedirectUris "https://login.microsoftonline.com/common/oauth2/nativeclient"
7+
-PublicClient @{ `
8+
RedirectUris = "https://login.microsoftonline.com/common/oauth2/nativeclient" `
9+
}
610

711
# Create an application for use with Native Client an interactive sign in
8-
$app2 = new-mgApplication -displayName "NativeAppInteractiveFlowApp" `
12+
$app2 = New-MgApplication -displayName "NativeAppInteractiveFlowApp" `
913
-IsFallbackPublicClient `
10-
-PublicClientRedirectUris "http://localhost"
14+
-PublicClient @{ `
15+
RedirectUris = "http://localhost" `
16+
}
1117

1218
# Create an web app with implicit auth
13-
$app3 = new-mgApplication -displayName "ImplicitWebApp" `
14-
-ImplicitGrantSettingEnableAccessTokenIssuance `
15-
-ImplicitGrantSettingEnableIdTokenIssuance `
16-
-WebRedirectUris "https://localhost:3000/"
17-
19+
$app3 = New-MgApplication -displayName "ImplicitWebApp" `
20+
-Web @{ `
21+
RedirectUris = "https://localhost:3000/"; `
22+
ImplicitGrantSettings = @{ `
23+
EnableAccessTokenIssuance = $true; `
24+
EnableIdTokenIssuance = $true; `
25+
} `
26+
}
27+
28+
# Create an application for use with Confidential Client flow using a certificate.
29+
# Get certificate from current user store.
30+
$CertificateThumbprint = "YOUR_THUMBPRINT"
31+
$Certificate = Get-ChildItem -Path "Cert:\CurrentUser\My\$CertificateThumbprint"
32+
33+
# Graph resource Id
34+
$GraphResourceId = "00000003-0000-0000-c000-000000000000"
35+
36+
# Graph permissions constants
37+
$UserReadAll = @{ Id = "df021288-bdef-4463-88db-98f22de89214"; Type = "Role" }
38+
$GroupReadAll = @{ Id = "5b567255-7703-4780-807c-7be8301ae99b"; Type = "Role" }
39+
$MailboxSettingsRead = @{ Id = "40f97065-369a-49f4-947c-6a255697ae91"; Type = "Role" }
40+
$MailSend = @{ Id = "b633e1c5-b582-4048-a93e-9f11b44c7e96"; Type = "Role" }
41+
42+
# Create an application registration.
43+
$AppName = "ScriptedGraphPSApp"
44+
$app4 = New-MgApplication -"ClientCredentialApp" $AppName `
45+
-SignInAudience "AzureADMyOrg" `
46+
-RequiredResourceAccess @{ ResourceAppId = $graphResourceId; ResourceAccess = $UserReadAll, $GroupReadAll, $MailboxSettingsRead, $MailSend } `
47+
-KeyCredentials @(@{ Type = "AsymmetricX509Cert"; Usage = "Verify"; Key= $Certificate.RawData })
48+
49+
# Create corresponding service principal.
50+
New-MgServicePrincipal -AppId $appRegistration.AppId

samples/Scripts/Get-TeamMembers.ps1

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# Replace with your ClientId
22
# Replace with your TenantId
33
# Replace with your Cert subject
4-
Connect-Graph -ClientId "1e4ee20b-9a64-44cb-bb6f-0d693e76490a" `
5-
-TenantId "d5fe491b-5987-4770-a68f-477c204cd1ca" `
6-
-CertificateName "CN=GraphPowerShellScriptCert"
4+
Connect-Graph -ClientId "YOUR_CLIENT_ID" `
5+
-TenantId "YOUR_TENANT_ID" `
6+
-CertificateName "YOUR_CERTIFICATE_NAME"
7+
8+
# Switch to beta profile to use these samples.
9+
Select-MgProfile -Name beta
10+
711
$groups = Get-MgGroup
812
$teams = $groups | Where-Object { $_.ResourceProvisioningOptions -Contains "Team" }
913

14+
# List owners and memebers of all Teams team in your tenant.
1015
foreach($team in $teams) {
1116
Write-Host "Team: " + $team.DisplayName -ForegroundColor Blue
1217

samples/Scripts/UsersAndGroups.ps1

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
$TenantId = "YOUR_TENANT_ID"
2+
$ClientId = "YOUR_CLIENT_ID"
3+
$CertThumbprint = "YOUR_CERTIFICATE_THUMBPRINT"
4+
$AdminMail = "ADMIN_MAIL"
5+
6+
# Consent to permissions using app created in the last demo.
7+
$AdminConsentUrl = "https://login.microsoftonline.com/$TenantId/adminconsent?client_id=$ClientId"
8+
Write-Host -ForeGroundColor Yellow "Please go to the following URL in your browser to provide admin consent"
9+
Write-Host $AdminConsentUrl
10+
Write-Host "Press any key to continue when done ....."
11+
$Key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
12+
13+
# Authenticate as a confidential client for app only calls.
14+
Connect-Graph -ClientId $ClientId -TenantId $TenantId -CertificateThumbprint $CertThumbprint
15+
16+
# Get group named 'Weekday Employees'
17+
$WeekdayEmployeesGroup = Get-MgGroup -Filter "DisplayName eq 'Weekday Employees'"
18+
19+
# Get Members of the group.
20+
$GroupMembers = Get-MgGroupMember -GroupId $WeekdayEmployeesGroup.Id
21+
$ToRecipients = @()
22+
foreach ($member in $GroupMembers) {
23+
$User = Get-MgUser -UserId $member.Id -Select "displayName", "mail", "mailboxSettings" | `
24+
Select DisplayName, Mail, MailboxSettings
25+
26+
# Get users with invalid workdays in their mailbox setting.
27+
if ( $User.MailboxSettings.WorkingHours.DaysOfWeek -contains "saturday" || `
28+
$User.MailboxSettings.WorkingHours.DaysOfWeek -contains "sunday" ) {
29+
Write-Host -ForegroundColor Yellow "User "$User.DisplayName" has an invalid workday."
30+
31+
$ToRecipients += @{
32+
emailAddress = @{
33+
name = $User.DisplayName;
34+
address = $User.Mail
35+
}}
36+
}
37+
}
38+
39+
if ($ToRecipients.Length) {
40+
# Compose message.
41+
$Message = @{
42+
subject = "Update Your Mailbox Settings!";
43+
toRecipients = $ToRecipients;
44+
body = @{
45+
contentType = "Text";
46+
content = "Please update to your mailbox settings to reflect your working hours." + `
47+
" You currently have Saturday and/or Sunday set as workday."
48+
}
49+
}
50+
51+
$Admin = Get-MgUser -UserId $AdminMail
52+
53+
# Send mail to users with invalid workdays.
54+
Send-MgUserMail -UserId $Admin.Id -BodyParameter @{message = $Message}
55+
Write-Host -ForegroundColor Green "Mail sent to affected users."
56+
}
57+
58+
Disconnect-Graph

0 commit comments

Comments
 (0)