Skip to content

Instantly share code, notes, and snippets.

@miracles1315
Last active September 11, 2017 16:30
Show Gist options
  • Save miracles1315/4ffd7b10d5f3fa5f0b09b29a154db512 to your computer and use it in GitHub Desktop.
Save miracles1315/4ffd7b10d5f3fa5f0b09b29a154db512 to your computer and use it in GitHub Desktop.
The function creates a remote PowerShell session to the Office 365 'Exchange Online Protection' service.
Function Connect-O365ExchangeOnlineProtection
{
<#
.SYNOPSIS
The function creates a remote PowerShell session to the Office 365 'Exchange Online Protection' service.
.DESCRIPTION
The function creates a remote PowerShell session to the Office 365 'Exchange Online Protection' service. Valid Office 365 credentials must be supplied. The function has an alias of Connect-O365EOP.
.PARAMETER Credential
Valid Office 365 credentials.
.EXAMPLE
PS C:\>$Credential = Get-Credential
PS C:\>Connect-O365ExchangeOnlineProtection -Credential $Credential
OR
PS C:\>$Credential = Get-Credential
PS C:\>Connect-O365EOP -Credential $Credential
.EXAMPLE
PS C:\>Connect-O365ExchangeOnlineProtection -Credential administrator@contoso.com
OR
PS C:\>Connect-O365EOP -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-O365ExchangeOnlineProtection
OR
PS C:\>Connect-O365EOP
In the above examples, the user will be prompted for credentials. Valid Office 365 credentials will need to be supplied.
.INPUTS
Valid Office 365 credentials.
.OUTPUTS
The exported commands from the 'Exchange Online Protection' service (Ex: Get-InboundConnector).
.NOTES
Last Updated: 9/11/17
.LINK
New-PSSession
https://technet.microsoft.com/en-us/library/dn621036(v=exchg.160).aspx
https://technet.microsoft.com/en-us/library/dn621038(v=exchg.160).aspx
#>
[CmdletBinding()]
Param
(
[PSCredential] $Credential = $Null
)
[String] $ServiceName = 'Exchange Online Protection'
[String] $FunctionName = 'Connect-O365ExchangeOnlineProtection'
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://ps.protection.outlook.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection -ErrorAction Stop
$Global:EOPExportedCommands = Import-PSSession $Session -ErrorAction Stop
Return $EOPExportedCommands
} #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
} #End 'If' block
} #End 'Connect-O365ExchangeOnlineProtection' function
$FunctionAliasExists = Get-Alias -Name Connect-O365EOP -ErrorAction SilentlyContinue
If(!$FunctionAliasExists)
{
New-Alias -Name Connect-O365EOP -Value Connect-O365ExchangeOnlineProtection
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment