Why the function works incorrectly when loaded from a module?

There is a function in a module:
function Connect-Exchange {
[Cmdletbinding()]
param(
 [Parameter(Mandatory = $false)]
 [string]$URL = (Get-Content $HOME\.psvariables\exchangeserver)
)
 Import-PSSession -Verbose (New-PSSession -Verbose -ConfigurationName Microsoft.Exchange -ConnectionUri http://$URL/PowerShell/ -Authentication Kerberos) -AllowClobber
}


If I run commands interactively in the console, then everything works as expected:
PS> [string]$URL = $(Get-Content $HOME\.psvariables\exchangeserver)
PS> Import-PSSession -Verbose (New-PSSession -Verbose -Name Exchange2013 -ConfigurationName Microsoft.Exchange -ConnectionUri http://$URL/PowerShell/ -Authentication Kerberos)
WARNING: The names of some imported commands from the module 'tmp_wyqpohdw.ids' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.

ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0 tmp_wyqpohdw.ids {Add-ADPermission Add-AvailabilityAddressSpace Add-ContentFilterPhrase Add-DatabaseAvailabilityGroupServer cmdlet...}

PS> get-mailbox test

Name Alias ServerName ProhibitSendQuota
---- ----- ---------- -----------------
test test exchange2013 Unlimited


But if this function is in a module, something is not working:
PS> Connect-Exchange
WARNING: The names of some imported commands from the module 'tmp_fan0rj3t.nzp' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.

ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0 tmp_fan0rj3t.nzp {Add-ADPermission, Add-AvailabilityAddressSpace, Add-Conte...


PS> get-mailbox test
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 test
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (get-mailbox:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException


PS: if function to determine the current session or in the profile file, then everything works.
June 7th 19 at 15:03
1 answer
June 7th 19 at 15:05
Because of different scope:
https://docs.microsoft.com/en-us/powershell/module...

Sessions, modules, and nested prompts are self-contained environments, but they are not child scopes of the global scope in the session.

Find more questions by tags Microsoft ExchangePowerShell