Created
January 19, 2016 13:25
-
-
Save psrdrgz/4f4cdbfd7f04f7987d84 to your computer and use it in GitHub Desktop.
Implicitly remote to on-premise Exchange & Exchange Online.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Connect-ExchangeOnline | |
{ | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory = $False)] | |
[pscredential]$Credential = $null, | |
[Parameter(Mandatory = $False)] | |
[string]$ConnectionURI = 'https://outlook.office365.com/powershell-liveid', | |
[Parameter(Mandatory = $False)] | |
[System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, | |
[Parameter(Mandatory = $False)] | |
[switch]$SkipImportModule, | |
[Parmeter(Mandatory = $False)] | |
[string]$Prefix | |
) | |
Begin{} | |
Process{ | |
$NewPSSessionParams = @{ | |
Name = 'ExchangeOnline' | |
ConfigurationName = 'Microsoft.Exchange' | |
ConnectionUri = $ConnectionURI | |
Authentication = 'Basic' | |
AllowRedirection = $True | |
ErrorAction = 'Stop' | |
} | |
If($null -ne $Credential) | |
{ | |
$NewPSSessionParams.Credential = $Credential | |
} | |
Try | |
{ | |
Remove-PSSession -Name ExchangeOnline -ErrorAction SilentlyContinue | |
Write-Verbose -Message 'Connecting to Exchange Online ...' | |
$Session = New-PSSession @NewPSSessionParams 3> $null | |
Write-Verbose -Message 'Successfully connected to Exchange Online.' | |
} | |
Catch | |
{ | |
Write-Warning -Message "Unable to connect to Exchange Online. $($_.Exception.Message)" | |
} | |
If($null -ne $Session) | |
{ | |
If(-not $SkipImportModule) | |
{ | |
$ImportSessionParams = @{ | |
Session = $Session | |
ErrorAction = 'Stop' | |
Verbose = $False | |
AllowClobber = $True | |
} | |
If($PSBoundParameters.ContainsKey('Certificate')) | |
{ | |
$ImportSessionParams.Certificate = $Certificate | |
} | |
If($PSBoundParameters.ContainsKey('Prefix')) | |
{ | |
$ImportSessionParams.Prefix = $Prefix | |
} | |
Try | |
{ | |
Write-Verbose -Message 'Importing Exchange Online cmdlets and functions ...' | |
$ModuleInfo = Import-PSSession @ImportSessionParams 3> $null | |
Import-Module -ModuleInfo $ModuleInfo -Global -Verbose:$False 3> $null | |
Write-Verbose -Message 'Exchange Online cmdlets and functions imported successfully.' | |
} | |
Catch | |
{ | |
Write-Warning -Message "Import failed. $($_.Exception.Message)" | |
} | |
} | |
} | |
Else | |
{ | |
Write-Warning -Message 'No session found. Exchange Online cmdlets and functions not imported.' | |
} | |
} | |
End{} | |
} | |
function Connect-Exchange | |
{ | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory = $False)] | |
[pscredential]$Credential = $null, | |
[Parameter(Mandatory = $True)] | |
[string]$Server, | |
[Parameter(Mandatory = $False)] | |
[System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate, | |
[Parameter(Mandatory = $False)] | |
[switch]$SkipImportModule, | |
[Parmeter(Mandatory = $False)] | |
[string]$Prefix | |
) | |
Begin{} | |
Process{ | |
$NewPSSessionParams = @{ | |
Name = 'Exchange' | |
ConfigurationName = 'Microsoft.Exchange' | |
ConnectionUri = "http://$Server/powershell" | |
ErrorAction = 'Stop' | |
} | |
If($null -ne $Credential) | |
{ | |
$NewPSSessionParams.Credential = $Credential | |
} | |
Try | |
{ | |
Remove-PSSession -Name Exchange -ErrorAction SilentlyContinue | |
Write-Verbose -Message 'Connecting to Exchange server ...' | |
$Session = New-PSSession @NewPSSessionParams 3> $null | |
Write-Verbose -Message 'Successfully connected to Exchange server.' | |
} | |
Catch | |
{ | |
Write-Warning -Message "Unable to connect $Server. $($_.Exception.Message)" | |
} | |
If($null -ne $Session) | |
{ | |
If(-not $SkipImportModule) | |
{ | |
$ImportSessionParams = @{ | |
Session = $Session | |
ErrorAction = 'Stop' | |
Verbose = $False | |
AllowClobber = $True | |
} | |
If($PSBoundParameters.ContainsKey('Certificate')) | |
{ | |
$ImportSessionParams.Certificate = $Certificate | |
} | |
If($PSBoundParameters.ContainsKey('Prefix')) | |
{ | |
$ImportSessionParams.Prefix = $Prefix | |
} | |
Try | |
{ | |
Write-Verbose -Message 'Importing Exchange cmdlets and functions ...' | |
$ModuleInfo = Import-PSSession @ImportSessionParams 3> $null | |
Import-Module -ModuleInfo $ModuleInfo -Global -Verbose:$False 3> $null | |
Write-Verbose -Message 'Exchange cmdlets and functions imported successfully.' | |
} | |
Catch | |
{ | |
Write-Warning -Message "Import failed. $($_.Exception.Message)" | |
} | |
} | |
} | |
Else | |
{ | |
Write-Warning -Message 'No session found. Exchange cmdlets and functions not imported.' | |
} | |
} | |
End{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment