C
C
Crovax2018-03-01 16:04:19
PowerShell
Crovax, 2018-03-01 16:04:19

Why does the function work incorrectly when loaded from a module?

There is a function in the 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 in the console interactively, 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...}

PS> get-mailbox test

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

But if this function is in a module, then something doesn't work:
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 the function is defined in the current session, or in the profile file, then everything works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly, 2018-09-05
@rbobot

Because different scopes:
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.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question