From ce468e2e73159d9c18db544e0f0c54c93ad1b3a4 Mon Sep 17 00:00:00 2001 From: VSTS Admin Date: Wed, 16 Oct 2019 10:17:11 -0500 Subject: [PATCH] Added support for cmdlet noun prefixing, and reducing the amount of cmdlets imported --- CloudConnect.psd1 | Bin 11008 -> 11192 bytes Services/Connect-EXO.ps1 | 56 ++++++++++++++++++++++++++-- Services/Connect-EXOLegacy.ps1 | 65 +++++++++++++++++++++++++++++++-- 3 files changed, 114 insertions(+), 7 deletions(-) diff --git a/CloudConnect.psd1 b/CloudConnect.psd1 index 982080145697e6e66174a7cc6f0a6f04262b9594..3033f236224fdeccd8a7017d56e196de75e76be7 100644 GIT binary patch delta 160 zcmZn&+Y!D&Pl?fZvbU;seKJEXLkdF8Lr7>hOQ~+@v zLpp;FP&5(9N&(U!xfGzvWT>11Pe4q&mKvf7kih=x0Ain@8SHw`l W0y1c`pwe@e$&=JoHtVQgU + param( + [parameter(Mandatory=$false)] + [string]$Prefix, + + [Parameter(Mandatory=$false)] + [string[]]$CommandList + ) # Get the token from the service $Token = Get-ServiceToken -service exo @@ -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 } \ No newline at end of file diff --git a/Services/Connect-EXOLegacy.ps1 b/Services/Connect-EXOLegacy.ps1 index c8608d1..1917f43 100644 --- a/Services/Connect-EXOLegacy.ps1 +++ b/Services/Connect-EXOLegacy.ps1 @@ -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 @@ -28,9 +36,32 @@ 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 @@ -38,16 +69,42 @@ Function Connect-EXOLegacy { $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 + } \ No newline at end of file