Skip to content

Instantly share code, notes, and snippets.

@miracles1315
Last active September 11, 2017 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miracles1315/3bea4e830a83ca350012fc6ea9b08e8e to your computer and use it in GitHub Desktop.
Save miracles1315/3bea4e830a83ca350012fc6ea9b08e8e to your computer and use it in GitHub Desktop.
The function creates a remote PowerShell session to the Office 365 'Exchange Online' service.
Function Connect-O365ExchangeOnline
{
<#
.SYNOPSIS
The function creates a remote PowerShell session to the Office 365 'Exchange Online' service.
.DESCRIPTION
The function creates a remote PowerShell session to the Office 365 'Exchange Online' service. Valid Office 365 credentials must be supplied. The function has an alias of Connect-O365EXO.
.PARAMETER Credential
Valid Office 365 credentials.
.PARAMETER AllowClobber
Indicates that this cmdlet imports the specified commands, even if they have the same names as commands in the current session.
If you import a command with the same name as a command in the current session, the imported command hides or replaces the original commands. For more information, see about_Command_Precedence.
By default, Connect-O365ExchangeOnline does not import commands that have the same name as commands in the current session.
.PARAMETER Prefix
Specifies a prefix to the nouns in the names of imported commands.
Use this parameter to avoid name conflicts that might occur when different commands in the session have the same name.
For instance, if you specify the prefix Remote and then import a Get-Date cmdlet, the cmdlet is known in the session as Get-RemoteDate, and it is not confused with the original Get-Date cmdlet.
.EXAMPLE
PS C:\>$Credential = Get-Credential
PS C:\>Connect-O365ExchangeOnline -Credential $Credential
OR
PS C:\>$Credential = Get-Credential
PS C:\>Connect-O365EXO -Credential $Credential
.EXAMPLE
PS C:\>Connect-O365ExchangeOnline -Credential administrator@contoso.com
OR
PS C:\>Connect-O365EXO -Credential administrator@contoso.com
In the above examples, the user will be prompted for credentials, with administrator@contoso.com shown in the 'User name' field. A valid password will need to be supplied.
.EXAMPLE
PS C:\>Connect-O365ExchangeOnline
OR
PS C:\>Connect-O365EXO
In the above examples, the user will be prompted for credentials. Valid Office 365 credentials will need to be supplied.
.EXAMPLE
PS C:\>Connect-O365ExchangeOnline administrator@contoso.com -Prefix O365
OR
PS C:\>Connect-O365EXO administrator@contoso.com -Prefix O365
In the above examples, after the appropriate Office 365 credentials are entered, a prefix of 'O365' (without the quotes) is added to the beginning of the noun part of a cmdlet name, which is in the form of Verb-Noun. For example, instead of Get-Mailbox, the cmdlet with a specified prefix of 'O365' (without the quotes) would be Get-O365Mailbox.
.EXAMPLE
PS C:\>Connect-O365ExchangeOnline -Credential administrator@contoso.com -AllowClobber
OR
PS C:\>Connect-O365EXO -Credential administrator@contoso.com -AllowClobber
In the above examples, the user will be prompted for credentials, with administrator@contoso.com shown in the 'User name' field. A valid password will need to be supplied. The AllowClobber parameter causes the imported commands to hide, or replace, the original/local commands (i.e. local commands with the same names). For more information, see about_Command_Precedence.
.INPUTS
Valid Office 365 credentials.
.OUTPUTS
The exported commands from the 'Exchange Online' service (Ex: Get-Mailbox).
.NOTES
Last Updated: 9/11/17
.LINK
New-PSSession
https://technet.microsoft.com/en-us/library/jj984289(v=exchg.160).aspx
https://technet.microsoft.com/en-us/library/jj200780(v=exchg.160).aspx
#>
[CmdletBinding()]
Param
(
[PSCredential] $Credential = $Null,
[Switch] $AllowClobber,
[String] $Prefix = $Null
)
[String] $ServiceName = 'Exchange Online'
[String] $FunctionName = 'Connect-O365ExchangeOnline'
If(!$Credential)
{
Try
{
Write-Verbose "Prompting user for Office 365 credentials."
$Credential = Get-Credential -ErrorAction Stop
} #End 'Try' block
Catch
{
Write-Warning "User cancelled the request for credentials. Exiting the $FunctionName function."
} #End Catch block
} #End 'If' block
If($Credential)
{
Try
{
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection -ErrorAction Stop
} #End 'Try' block
Catch
{
Write-Warning "Unable to create a remote session to the Office 365 `'$ServiceName`' service. See the error, below, for details on the failure.`n"
throw
} #End 'Catch' block
If($Session)
{
If($AllowClobber -And $Prefix)
{
$Global:EXOExportedCommands = Import-PSSession $Session -AllowClobber -Prefix $Prefix -ErrorAction Stop
} #End 'If' block
ElseIf($AllowClobber)
{
$Global:EXOExportedCommands = Import-PSSession $Session -AllowClobber -ErrorAction Stop
} #End 'ElseIf' block
ElseIf($Prefix)
{
$Global:EXOExportedCommands = Import-PSSession $Session -Prefix $Prefix -ErrorAction Stop
} #End 'ElseIf' block
Else
{
$Global:EXOExportedCommands = Import-PSSession $Session -ErrorAction Stop
} #End 'Else' block
Return $EXOExportedCommands
} #End 'If' block
} #End 'If' block
} #End 'Connect-O365ExchangeOnline' function
$FunctionAliasExists = Get-Alias -Name Connect-O365EXO -ErrorAction SilentlyContinue
If(!$FunctionAliasExists)
{
New-Alias -Name Connect-O365EXO -Value Connect-O365ExchangeOnline
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment