/
Connect-Service.ps1
208 lines (189 loc) · 7.05 KB
/
Connect-Service.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
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
<#
.SYNOPSIS
Connect to any customers domain as a partner.
.DESCRIPTION
This script is a collection of login functions that can be used to connect to any customers domain as a partner.
.PARAMETER service
Toggle through the services you can connect to.
.PARAMETER domain
What domain to connect to.
.EXAMPLE
Connect-Service MicrosoftTeams mydomain.com
.NOTES
FileName: Connect-Service.ps1
Author: Daniel Kåven
Contact: @DKaaven
Created: 2022-03-25
Updated: 2023-12-20
Version history:
1.0.0 - (2022-03-25) Script created
1.0.1 - (2023-12-20) Added supporting functions into same file.
#>
# Inspired by: https://seanmcavinue.net/2020/11/19/using-delegated-access-permissions-in-powershell-to-manage-all-microsoft-365-services/
function Get-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
$data = ConvertFrom-Json $request.Content
return $Data.token_endpoint.split('/')[3]
}
function Install-Requirements {
param (
[parameter(Mandatory = $true, ValueFromRemainingArguments, HelpMessage = "Modules required")]
[ValidateNotNullOrEmpty()]
[psobject[]]$Modules
)
# Install required modules for script execution
foreach ($Module in $Modules) {
try {
$CurrentModule = Get-InstalledModule -Name $Module -ErrorAction Stop | Out-Null
if (!$CurrentModule) {
$LatestModuleVersion = (Find-Module -Name $Module -ErrorAction Stop).Version
if ($LatestModuleVersion -gt $CurrentModule.Version) {
Update-Module -Name $Module -Force -ErrorAction Stop -Confirm:$false
}
}
}
catch [System.Exception] {
try {
# Install NuGet package provider
Install-PackageProvider -Name NuGet -Force -ErrorAction SilentlyContinue
# Install current missing module
Install-Module -Name $Module -Scope CurrentUser -Force -ErrorAction Stop -Confirm:$false
}
catch [System.Exception] {
Write-Warning -Message "An error occurred while attempting to install $Module module."
Write-Error -Message "Error message: $($_.Exception.Message)" -Category NotInstalled
}
}
Import-Module -Name $Module
}
}
function Get-PSversion {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, Position=0)]
[ValidateSet("Core","Desktop")]
[string]$version
)
if ($PSEdition -ne $version) {
return $false
}
else {
return $true
}
}
function Connect-Service {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0, HelpMessage="The command to execute")]
[validateset("MicrosoftTeams", "MSOnline", "Exchange","Security", "AzureAD")]
[String]$service,
[Parameter(Mandatory=$true, Position=1, HelpMessage="Customers Domain")]
[String]$domain
)
# Get customers TenantId
$TenantId = Get-TenantId -domain $domain
#endregion
#region - List of services and connectscripts
$services = @()
while (!$service) {
Write-Host "Choose one of the following services:" -ForegroundColor Green
foreach ($service in $services) {
Write-Output "* $service"
}
while ($service -ne $services) {
$service = Read-Host "Service: "
}
}
switch ($service){
"MicrosofTeams" {
Install-Requirements("MicrosoftTeams")
try {
Write-Host "Connecting to Microsoft Teams..." -foregroundcolor Yellow
Connect-MicrosoftTeams -tenantId $TenantId
Write-Host "Connected to Microsoft Teams" -ForegroundColor Green
Write-Output ""
Write-Output "Commands: https://docs.microsoft.com/en-us/powershell/module/teams/"
$disconnect = "Disconnect-MicrosoftTeams"
}
catch [System.Exception] {
Write-Host $_.Exception.Message
exit 1
}
}
"MSOnline" {
Install-Requirements("MSOnline")
Try {
Connect-MsolService
Write-Host "Connected to MSOnline" -ForegroundColor Green
Write-Output "To use this service, you need to add this to the end of the command:"
Write-Output "-TenantId $TenantId"
Write-Output ""
Write-Output "Commands: https://docs.microsoft.com/en-us/powershell/module/msonline/"
$disconnect = "Disconnect-MsolService"
}
catch [System.Exception] {
Write-Host $_.Exception.Message
exit 1
}
}
"Exchange" {
$PSVersion = Get-PSversion -version "Desktop"
if ($PSVersion -eq $false) {
Write-Warning "Exchange is not supported on PowerShell Core"
powershell
}
Install-Requirements("ExchangeOnlineManagement")
try {
Connect-ExchangeOnline -DelegatedOrganization $domain
Write-Host "Connected to Exchange" -ForegroundColor Green
$disconnect = "Disconnect-ExchangeOnline"
}
catch [System.Exception] {
Write-Host $_.Exception.Message
Start-Sleep -s 15
exit 1
}
}
"AzureAD" {
$PSVersion = Get-PSversion -version "Desktop"
if ($PSVersion -eq $false) {
Write-Warning "AzureAD is not supported on PowerShell Core"
powershell
}
Install-Requirements("AzureADPreview")
try {
Connect-AzureAD -TenantId $TenantId
Write-Host "Connected to AzureAD" -ForegroundColor Green
Write-Output ""
Write-Output "Commands: https://docs.microsoft.com/en-us/powershell/module/azuread"
$disconnect = "Disconnect-AzureAD"
}
catch [System.Exception] {
Write-Host $_.Exception.Message
exit 1
}
}
"Security" {
Install-Requirements("ExchangeOnlineManangement")
try {
Connect-IPPSSession -DelegatedOrganization $domain
Write-Host "Connected to Security & Compliance" -ForegroundColor Green
$disconnect = "Disconnect-ExchangeOnline"
}
catch [System.Exception] {
Write-Host $_.Exception.Message
exit 1
}
}
}
#endregion
Write-Host "Remember to disconnected from the service" -ForegroundColor Green
Write-Output ""
Write-Output $disconnect
Write-Output ""
}