Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified CloudConnect.psd1
Binary file not shown.
56 changes: 53 additions & 3 deletions Services/Connect-EXO.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ function Connect-EXO {
.LINK
https://github.com/Canthv0/CloudConnect

.PARAMETER Prefix

Adds support for cmdlet noun prefixing. This avoids clobbering existing cmdlets in the current PS environment.

.PARAMETER CommandList

Supports importing only specific cmdlets with this session. This will speed up the creation of the session and ultimately use less memory.

.OUTPUTS
None. Creates a PS session to EXO and imports it into the global scope

Expand All @@ -25,8 +33,26 @@ function Connect-EXO {

Will prompt for credentials and import Exchange PS Session into the global scope

.EXAMPLE
Connect-Exo -Prefix "client"

Will prompt for credentials and import Exchange PS Session into the global scope, prefixing all nouns with "client"
i.e. Get-clientMailbox

.EXAMPLE
Connect-Exo -CommandList "Get-Mailbox","Set-Mailbox"

Will prompt for credentials and import Exchange PS Session into the global scope, including only those commands listed.

#>

param(
[parameter(Mandatory=$false)]
[string]$Prefix,

[Parameter(Mandatory=$false)]
[string[]]$CommandList
)
# Get the token from the service
$Token = Get-ServiceToken -service exo

Expand All @@ -36,13 +62,37 @@ function Connect-EXO {
# Build the auth information
$Authorization = "Bearer {0}" -f $Token.Result.AccessToken
$UserId = ($Token.Result.UserInfo.DisplayableId).tostring()

# create the "basic" token to send to O365 EXO
$Password = ConvertTo-SecureString -AsPlainText $Authorization -Force
$Credtoken = New-Object System.Management.Automation.PSCredential -ArgumentList $UserId, $Password

# Create and import the session
$Session = New-PSSession -Name EXO -ConfigurationName Microsoft.Exchange -ConnectionUri 'https://outlook.office365.com/PowerShell-LiveId?BasicAuthToOAuthConversion=true' -Credential $Credtoken -Authentication Basic -AllowRedirection -ErrorAction Stop
Import-Module (Import-PSSession $Session -AllowClobber) -Global -WarningAction 'SilentlyContinue'

$ImportSessionParams = @{

Session = $Session
AllowClobber = $true
}

$ImportModuleParams = @{

global = $true
WarningAction = "SilentlyContinue"
}

if($Prefix) {

$ImportSessionParams.Add("Prefix",$Prefix)
$ImportModuleParams.Add("Prefix",$Prefix)
}

if($CommandList) {

$ImportSessionParams.Add("CommandName",$CommandList)
}

Import-Module (Import-PSSession @ImportSessionParams) @ImportModuleParams

}
65 changes: 61 additions & 4 deletions Services/Connect-EXOLegacy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ Function Connect-EXOLegacy {
Credential Object used to connect to EXO.
If not provided user will be prompted for it.

.PARAMETER Prefix

Adds support for cmdlet noun prefixing. This avoids clobbering existing cmdlets in the current PS environment.

.PARAMETER CommandList

Supports importing only specific cmdlets with this session. This will speed up the creation of the session and ultimately use less memory.

.OUTPUTS
None. Creates a PS session to EXO and imports it into the global scope

Expand All @@ -28,26 +36,75 @@ Function Connect-EXOLegacy {

Will used the $cred credential to connect to exchange Online


.EXAMPLE
Connect-ExoLefgacy -Prefix "client"

Will prompt for credentials and import Exchange PS Session into the global scope, prefixing all nouns with "client"
i.e. Get-clientMailbox

.EXAMPLE
Connect-ExoLegacy -CommandList "Get-Mailbox","Set-Mailbox"

Will prompt for credentials and import Exchange PS Session into the global scope, including only those commands listed.

#>

Param($Credential)
Param(
[Parameter(Mandatory=$false)]
[PSCredential]$Credential,

[parameter(Mandatory=$false)]
[string]$Prefix,

[Parameter(Mandatory=$false)]
[string[]]$CommandList


)

# If we don't have a credential then prompt for it
$i = 0
while (($Null -eq $Credential) -and ($i -lt 5)) {
$Credential = Get-Credential -Message "Please provide your Exchange Online Credentials"
$i++
}

# If we still don't have a credentail object then abort
if ($null -eq $Credential) {
Write-Error -Message "Failed to get credentials" -ErrorAction Stop
}

# Check for a previous sesison and remove it
Get-PSSession -name Legacy_EXO* | Remove-PSSession -Confirm:$false

# Create and import the session
$session = New-PSSession -Name Legacy_EXO -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $Credential -Authentication Basic -AllowRedirection
Import-Module (Import-PSSession $Session -AllowClobber) -Global -WarningAction 'SilentlyContinue'


$ImportSessionParams = @{

Session = $Session
AllowClobber = $true
}

$ImportModuleParams = @{

global = $true
WarningAction = "SilentlyContinue"
}

if($Prefix) {

$ImportSessionParams.Add("Prefix",$Prefix)
$ImportModuleParams.Add("Prefix",$Prefix)
}

if($CommandList) {

$ImportSessionParams.Add("CommandName",$CommandList)
}

Import-Module (Import-PSSession @ImportSessionParams) @ImportModuleParams

}