This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
/
Functions-ConnectDisconnect.ps1
108 lines (81 loc) · 3.19 KB
/
Functions-ConnectDisconnect.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
<#
These functions can be added to your PowerShell profile to allow you to
quickly and easily connect to Exchange Online (Office 365).
For more info on how they are used see:
http://exchangeserverpro.com/powershell-function-connect-office-365/
http://exchangeserverpro.com/create-powershell-profile
Written by: Paul Cunningham
Find me on:
* My Blog: https://paulcunningham.me
* Twitter: https://twitter.com/paulcunningham
* LinkedIn: https://au.linkedin.com/in/cunninghamp/
* Github: https://github.com/cunninghamp
Change Log:
V1.00, 14/10/2014 - Initial version
V1.01, 22/05/2016 - Updated with new PowerShell endpoint URL
V1.02, 27/09/2016 - Connect-EXOnline updated with new functionality for using stored credentials
#>
Function Connect-EXOnline {
<#
.SYNOPSIS
Connect-EXOnline.ps1 - Connect to Exchange Online
.DESCRIPTION
This function will connect to Exchange Online using PowerShell.
.PARAMETER UseCredential
Uses a stored credential on the computer for authenticating with
Exchange Online. For more information on using stored credentials
refer to
.PARAMETER Prefix
Prefixes the Exchange Online cmdlets with the value that you specify
(such as "Cloud")so that they can be used alongside the on-premises
Exchange cmdlets of the same name.
.EXAMPLE
Connect-EXOnline
Prompts for authentication and connects to Exchange Online.
.EXAMPLE
Connect-EXOnline -UseCredential admin@tenant.onmicrosoft.com
Uses the stored credential for admin@tenant.onmicrosoft.com to connect
to Exchange Online.
.EXAMPLE
Connect-EXOnline -UseCredential admin@tenant.onmicrosoft.com -Prefix EXO
Uses the stored credential for admin@tenant.onmicrosoft.com to connect
to Exchange Online, and prefixes the cmdlets with "EXO", for example
Get-EXOMailbox instead of Get-Mailbox.
.NOTES
Written by: Paul Cunningham
Find me on:
* My Blog: http://paulcunningham.me
* Twitter: https://twitter.com/paulcunningham
* LinkedIn: http://au.linkedin.com/in/cunninghamp/
* Github: https://github.com/cunninghamp
For more Office 365 tips, tricks and news
check out Practical 365.
* Website: https://practical365.com
* Twitter: https://twitter.com/practical365
#>
param(
[Parameter(Mandatory=$false)]
[string]$UseCredential,
[Parameter(Mandatory=$false)]
[string]$Prefix
)
$URL = "https://outlook.office365.com/powershell-liveid/"
if ($UseCredential) {
$Credentials = Get-StoredCredential -UserName $UseCredential
}
else {
$Credentials = Get-Credential -Message "Enter your Office 365 admin credentials"
}
$EXOSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $URL -Credential $Credentials -Authentication Basic -AllowRedirection -Name "Exchange Online"
if ($Prefix) {
Import-PSSession $EXOSession -Prefix $Prefix
}
else {
Import-PSSession $EXOSession
}
}
#This function will disconnect the PS Session "Exchange Online". This may
#leave other PS Sessions open to EXO if others have been created.
Function Disconnect-EXOnline {
Remove-PSSession -Name "Exchange Online"
}