Crovax
@Crovax

Почему функция неправильно отрабатывает при загрузке из модуля?

Имеется функция в модуле:
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
}


Если я запускаю команды в консоли интерактивно, то все работает как и ожидается:
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


Но если эта функция находится в модуле, то что-то не работает:
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: если функцию определить в текущем сеансе, либо в файле профиля, то все работает.
  • Вопрос задан
  • 296 просмотров
Пригласить эксперта
Ответы на вопрос 1
Потому что разные области видимости:
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.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы