Reputation: 1
Working on several Powershell functions which connect to Office365 services and automate many of my usual tasks. Found when I have the function in a psm1, running the function to connect to ExchangeOnline the functions are not exposed to the console and only functions within the same module.
While I know I can export-modulemember, this only works for functions when the module is loaded, happens well before I connect-office365 -exchangeonline.
Is there anyway to export the commands loaded when connecting to Office 365 after connecting?
By placing the following function in a psm1 instead of a ps1 and loading it, and then running connect-off365 -exchangeonline, all Exchange Online related commands are not usable in the console. Yet, having the same function in a ps1 and loading it, the ExchangeOnline functions work.
Function Connect-Office365 {
Param(
[Switch]$ExchangeOnline,
[Switch]$EO
)
if ($EO) { $ExchangeOnline = $true }
elseif ($EO -and $ExchangeOnline) { Write-Warning "No need to declare ExchangeOnline and EO" }
$Credential = Get-Credential -message "Enter Office 365 credentials,`r`nor cancel to not connect to Office 365"
if ($null -eq $Credential) { Write-Host "Skipped entering credentials"; $TryAgain = $False }
if ($ExchangeOnline) {
Write-Host "Connecting to Exchange Online Services"
$global:ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection -EA stop
Import-PSSession $global:ExchangeSession -AllowClobber -DisableNameChecking
}
}
For example
PS C:\Users\TechWithAHammer> import-module C:\scripts\Connect-Office365.psm1
PS C:\Users\TechWithAHammer> connect-office365 -eo Connecting to MSOL Services Connecting to Exchange Online Services
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0 tmp_3zvdxnnc.gd0 {Add-AvailabilityAddressSpace, Add-DistributionGroupMember...
PS C:\Users\TechWithAHammer> get-mailbox
get-mailbox : The term 'get-mailbox' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ get-mailbox
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (get-mailbox:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Upvotes: 0
Views: 240
Reputation: 1
As per the answer in Commands from implicit remoting module not available when created from another module's function, found that I had to use Import-Module to import the Exchange Online and Compliance and Security center modules into the global session.
Import-Module (Import-PSSession $global:ExchangeSession -AllowClobber -DisableNameChecking) -Global
Upvotes: 0