Skip to content

Commit 7b1c6cf

Browse files
Merge pull request #230466 from vinnieangel/eduscript
full api script
2 parents 21a1fb0 + a8bd2d0 commit 7b1c6cf

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
title: How to deploy Azure Education Hub labs through a PowerShell script
3+
description: This article shows you how to deploy labs in Education Hub through a PowerShell script
4+
author: vinnieangel
5+
ms.author: vangellotti
6+
ms.service: azure-education
7+
ms.topic: how-to
8+
ms.date: 3/13/2023
9+
ms.custom: template-how-to
10+
---
11+
12+
# Run a script to create lab and call other dependent APIs
13+
14+
With Education Hub's public APIs, you can deploy labs through APIs alone. However, there are a few more APIs that need to be called which are shown in the following PowerShell script.
15+
16+
## Prerequisites
17+
18+
- Know billing account ID, Billing profile ID, and Invoice Section ID
19+
- Have an Edu approved Azure account
20+
21+
## Run the script
22+
23+
Run the script and replace the <> with your information
24+
25+
```ps
26+
# Requires -Modules Microsoft.Graph.Identity.SignIns, Microsoft.Graph.Users
27+
28+
# this should be the professors tenantId
29+
$tenantId='<Professor TenantId>'
30+
31+
Connect-AzAccount -TenantId $tenantId
32+
$azContext = Get-AzContext
33+
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
34+
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
35+
$token = $profileClient.AcquireAccessToken($tenantId)
36+
$authHeader = @{
37+
'Content-Type'='application/json'
38+
'Authorization'='Bearer ' + $token.AccessToken
39+
}
40+
41+
# Set your billing scope.
42+
$billingScope='/billingAccounts/<BillingAccountId>/billingProfiles/<BillingProfileId>/invoiceSections/<InvoiceSectionId>'
43+
44+
# Create a lab
45+
$labName='<LabName>'
46+
$labExpiresOn='<Expiration Date> Ex. 2023-11-14T22:11:29.422Z'
47+
$createLabUri ='https://management.azure.com/providers/Microsoft.Billing'+$billingScope+'/providers/Microsoft.Education/labs/default?api-version=2021-12-01-preview'
48+
$createLabRequestBody = "{
49+
`"properties`": {
50+
`"displayName`": `"$labName`",
51+
`"budgetPerStudent`": {
52+
`"currency`": `"USD`",
53+
`"value`": <Budget>
54+
},
55+
`"description`": `"<Description>`",
56+
`"expirationDate`": `"$labExpiresOn`"
57+
}
58+
}
59+
60+
$response = Invoke-RestMethod -Uri $createLabUri -Method PUT -Headers $authHeader -Body $createLabRequestBody
61+
ConvertTo-Json $response
62+
63+
64+
# Create a lab user
65+
$studentId=New-Guid
66+
$studentEmail='<StudentEmail>'
67+
$studentFname='<StudentFirstName>'
68+
$studentLName='<StudentLastName>'
69+
70+
# Connect to graph and give the user User.ReadWrite.All permissions if it's not already given
71+
Connect-MgGraph -Scopes User.ReadWrite.All
72+
73+
# Send the invitation, optionally you can check if the user already exists in the tenant using Get-MgUser -Filter "Mail eq '<StudentEmail>'"
74+
New-MgInvitation -InvitedUserDisplayName $studentFname+' '+$studentLName -InvitedUserEmailAddress $studentEmail -InviteRedirectUrl "https://aka.ms/startedu" -SendInvitationMessage:$false
75+
76+
77+
$createLabUserUri ='https://management.azure.com/providers/Microsoft.Billing'+$billingScope+'/providers/Microsoft.Education/labs/default/students/$studentId?api-version=2021-12-01-preview'
78+
$createLabUserRequestBody = "{
79+
`"properties`": {
80+
`"firstName`": `"$studentFname`",
81+
`"lastName`": `"$studentLname`",
82+
`"email`": `"$studentEmail`",
83+
`"role`": `"Student`",
84+
`"budget`": {
85+
`"currency`": `"USD`",
86+
`"value`": 100
87+
},
88+
`"expirationDate`": `"$labExpiresOn`"
89+
}
90+
}"
91+
92+
$response = Invoke-RestMethod -Uri $createLabUserUri -Method PUT -Headers $authHeader -Body $createLabUserRequestBody
93+
ConvertTo-Json $response
94+
95+
# send the subscription invite
96+
$subAlias="fddb-5899-abc-127" #should be randomly generated in the format xxxx-xxxx-xxx-xxx
97+
$subInviteUri='https://management.azure.com/providers/Microsoft.Subscription/aliases/'+$subAlias+'?api-version=2021-01-01-privatepreview'
98+
$displayName=$labName+'_'+$studentFname+'_'+$studentLName
99+
#Format of displayName should be LabName_StudentFname_StudentLname
100+
$createSubInviteRequestBody = "{
101+
`"properties`": {
102+
`"displayName`": `"$displayName`",
103+
`"workload`": `"Production`",
104+
`"billingScope`": `"$billingScope`",
105+
`"additionalProperties`": {
106+
`"subscriptionTenantId`": `"$tenantId`",
107+
`"subscriptionOwnerId`": `"$studentEmail`",
108+
}
109+
}
110+
}"
111+
112+
$response = Invoke-RestMethod -Uri $subInviteUri -Method PUT -Headers $authHeader -Body $createSubInviteRequestBody
113+
ConvertTo-Json $response
114+
```
115+
116+
## Next steps
117+
- [Manage your Academic Grant using the Overview page](hub-overview-page.md)
118+
119+
- [Support options](educator-service-desk.md)

articles/education-hub/index.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ landingContent:
6060
url: find-ids.md
6161
- linkListType: how-to-guide
6262
links:
63+
- text: How to use a script to automate lab functionality
64+
url: full-api-script.md
6365
- text: How to create a lab
6466
url: create-lab-education-hub.md
6567
- text: How to add a student to a lab

articles/education-hub/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
items:
5555
- name: How-to guides
5656
items:
57+
- name: How to use a script to automate lab functionality
58+
href: full-api-script.md
5759
- name: How to create a lab
5860
href: create-lab-education-hub.md
5961
- name: How to add students to a lab

0 commit comments

Comments
 (0)