-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartnerShell.psm1
255 lines (227 loc) · 9.75 KB
/
PartnerShell.psm1
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
$MODULES_AVAILABLE = @(
"Connect-ExchangeOnline",
"Connect-IPPSSession",
"Connect-MicrosoftTeams",
"Connect-MgGraph"
)
function Get-TenantID { # Credit to Daniel Kåven | https://teams.se/powershell-script-find-a-microsoft-365-tenantid/
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0, HelpMessage="The domain name of the tenant")]
[String]$domain
)
$request = Invoke-WebRequest -Uri https://login.windows.net/$domain/.well-known/openid-configuration -UseBasicParsing
$data = ConvertFrom-Json $request.Content
return $Data.token_endpoint.split('/')[3]
}
function Connect-ExchangeOnline-ViaPartner {
param (
[Parameter(Mandatory=$true, HelpMessage="Client Tenant ID")]
[String] $Tenant
)
Process {
$token = New-PartnerAccessToken -ApplicationId "fb78d390-0c51-40cd-8e17-fdbfab77341b" -Scopes "https://outlook.office365.com/powershell-liveid" -Tenant $Tenant
Connect-ExchangeOnline -AccessToken $token.AccessToken -DelegatedOrganization $Tenant -UserPrincipalName $token.Account.Username
}
}
function Connect-IPPSSession-ViaPartner {
param (
[Parameter(Mandatory=$true, HelpMessage="Client Tenant ID")]
[String] $Tenant
)
Process {
$token = New-PartnerAccessToken -ApplicationId "fb78d390-0c51-40cd-8e17-fdbfab77341b" -Scopes "https://ps.compliance.protection.outlook.com/PowerShell-LiveId" -Tenant $Tenant
Connect-ExchangeOnline -ConnectionUri "https://ps.compliance.protection.outlook.com/PowerShell-LiveId" -AccessToken $token.AccessToken -DelegatedOrganization $Tenant -UserPrincipalName $token.Account.Username
}
}
function Connect-MicrosoftTeams-ViaPartner {
param (
[Parameter(Mandatory=$true, HelpMessage="Client Tenant ID")]
[String] $Tenant
)
Process {
$graphToken = New-PartnerAccessToken -ApplicationId "12128f48-ec9e-42f0-b203-ea49fb6af367" -Scopes "https://graph.microsoft.com/.default" -Tenant $Tenant
$teamsToken = New-PartnerAccessToken -ApplicationId "12128f48-ec9e-42f0-b203-ea49fb6af367" -RefreshToken $graphToken.RefreshToken -Scopes "48ac35b8-9aa8-4d74-927d-1f4a14a0b239/.default" -Tenant $Tenant
Connect-MicrosoftTeams -AccessTokens [$graphToken.AccessToken, $teamsToken.AccessToken]
}
}
function Connect-MgGraph-ViaPartner {
param (
[Parameter(Mandatory=$true, HelpMessage="Client Tenant ID")]
[String] $Tenant
)
Process {
Write-Host @"
Due to the nature of the Micrsoft Graph plugin and the functionality of Graph itself,
An application with a set of API permissions must be instantiated in the client tenant.
"@
do {
$scopes = Read-Host "Please enter a list of API permissions, separated by commas"
$scopesList = $scopes.Split(",")
Write-Output "List of scopes: $scopes"
$Confirmation = Read-Host "Confirm? (y/N)"
} while ($Confirmation.ToLower -ne "y")
Connect-PartnerCenter -AccessToken $authResult.AccessToken
$newGrant = New-Object -TypeName Microsoft.Store.PartnerCenter.Models.ApplicationConsents.ApplicationGrant
$newGrant.EnterpriseApplicationId = "00000002-0000-0000-c000-000000000000"
$newGrant.Scope = $scopesList
New-PartnerCustomerApplicationConsent -ApplicationId "14d82eec-204b-4c2f-b7e8-296a70dab67e" -CustomerId $Tenant -ApplicationGrants @($newGrant) -DisplayName "Microsoft Graph Command Line Tools"
$token = New-PartnerAccessToken -ApplicationId "14d82eec-204b-4c2f-b7e8-296a70dab67e" -Scopes "https://graph.microsoft.com/.default" -Tenant $Tenant
Connect-MgGraph -AccessToken $token.AccessToken
}
}
function Get-Choice {
param (
[Parameter(Mandatory=$true, HelpMessage="Input data")]
[Array] $In,
[Parameter(Mandatory=$false, HelpMessage="Relevant parameters")]
[String[]] $Params,
[Parameter(Mandatory=$false, HelpMessage="Allow selection of multiple values")]
[switch] $MultipleChoice = $false,
[Parameter(Mandatory=$false, HelpMessage="Page size")]
[int] $pageSize = 8
)
Process {
$currentPage = 0
do {
for ($i = $currentPage * $pageSize; $i -lt ($currentPage + 1) * $pageSize; $i++) {
if ($i -ge $In.Length) {
break;
}
$outStr = $i.ToString() + ". "
if ($PSBoundParameters.ContainsKey("Params")) {
for ($j = 0; $j -lt $Params.Length; $j++) {
$outStr += $In[$i].$($Params[$j]) + " | "
}
}
else {
$outStr += $In[$i]
}
Write-Host $outStr
}
$selectStr = "Pick the relevant option. "
if ($MultipleChoice) {
$selectStr += "Select several options by separating via commas. "
}
if ($pageSize -le $In.Length) {
if (($currentPage + 1) * $pageSize -lt $In.Length) {
$selectStr += "(N) Next Page "
}
if ($currentPage -ne 0) {
$selectStr += "(P) Previous Page "
}
}
$pick = Read-Host $selectStr
switch ($pick) {
{$_.ToLower() -eq "n"} {
if ((($currentPage + 1) * $pageSize) -gt $In.Length) {
Write-Host "Cannot go further: this is the last page."
continue
}
else {
$currentPage++
}
}
{$_.ToLower() -eq "p"} {
if ($currentPage -eq 0) {
Write-Host "Cannot go back any further: this is the first page."
continue
}
else {
$currentPage--
}
}
default {
try {
if ($MultipleChoice) {
$pickMultiple = $pick.split(",")
$choices = foreach ($num in $pickMultiple) { ([int]::parse($num)) }
}
else {
$choices = @([int]::parse($pick))
}
}
catch {
Write-Host "Invalid input."
continue
}
$valid = $true
foreach ($option in $choices) {
if (!(($option -ge 0) -and ($option -lt $In.Length))) {
$valid = $false
Write-Host "Invalid option: $($option)"
}
}
if (!$valid) {
Write-Host "Invalid input."
continue
}
do {
foreach ($option in $choices) {
$confStr = ""
if ($PSBoundParameters.ContainsKey("Params")) {
for ($i = 0; $i -lt $Params.Length; $i++){
$confStr += $In[$option].$($Params[$i]) + " | "
}
}
else {
$confStr += $In[$option]
}
Write-Host $confStr
}
$Confirmation = Read-Host "Confirm? (y/N)"
if (($null -eq $Confirmation) -or ($Confirmation.ToLower() -eq "n")) {
break
}
} while ($Confirmation.ToLower() -ne "y")
if ($Confirmation.ToLower() -eq "y") {
$choice = foreach ($option in $choices) { $In[$option] }
}
}
}
} while ($null -eq $choice)
return $choice
}
}
function PartnerShell {
[CmdletBinding()]
param (
[Parameter(Mandatory=$false, HelpMessage="Tenant ID / Domain'")]
[String] $Tenant
)
Process {
# Authenticate to Partner Center and store token information
Connect-PartnerCenter
# Acquire all customers associated to partner
$customers = Get-PartnerCustomer
# Determine client tenant to be authenticated into
if ($PSBoundParameters.ContainsKey("Tenant")) {
try {
$tenantId = Get-TenantID $Tenant
$inPartner = $false
for ($i = 0; $i -lt $customers.Length; i++) {
if ($tenantId -eq $customers[$i].CustomerId) {
$inPartner = $true
break
}
}
if (!$inPartner) {
throw "Specified tenant " + $tenantId + " is not in available customers."
}
}
catch {
Write-Output "The specified tenant ID / domain is not acceptable."
Write-Error -Message $_ -ErrorAction Stop
}
}
else {
$tenantId = (Get-Choice -In $customers -Params "Name","Domain").CustomerId
}
# List available modules to authenticate into and prompt for selected modules
$actions = Get-Choice -In $MODULES_AVAILABLE -MultipleChoice
foreach ($action in $actions) {
$partnerAction = $action + "-ViaPartner"
& $partnerAction -Tenant $tenantId
}
}
}