diff --git a/CloudConnect.psd1 b/CloudConnect.psd1 index 9820801..3033f23 100644 Binary files a/CloudConnect.psd1 and b/CloudConnect.psd1 differ diff --git a/Services/Connect-EXO.ps1 b/Services/Connect-EXO.ps1 index ebc90e0..b63a41f 100644 --- a/Services/Connect-EXO.ps1 +++ b/Services/Connect-EXO.ps1 @@ -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 @@ -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 @@ -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