首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Powershell模块中使用函数psm1时,是否可以导出ExchangeOnline函数以便在控制台中访问?

在Powershell模块中使用函数psm1时,是否可以导出ExchangeOnline函数以便在控制台中访问?
EN

Stack Overflow用户
提问于 2020-02-21 14:06:00
回答 1查看 84关注 0票数 0

我正在处理几个Powershell函数,它们连接到Office365服务,并自动执行我的许多常规任务。当我在psm1中运行函数以连接到ExchangeOnline时,我发现这些函数不会向控制台公开,只有同一模块中的函数才会公开。

虽然我知道我可以导出模块成员,但这只适用于模块加载时的函数,发生在我连接之前-Office365 -exchangeonline。

是否可以导出在连接后连接到Office 365时加载的命令?

通过将以下函数放置在psm1中而不是ps1中并加载它,然后运行connect-off365 -exchangeonline,所有与Exchange Online相关的命令在控制台中都不可用。然而,在ps1中拥有相同的函数并加载它,ExchangeOnline函数就可以工作了。

代码语言:javascript
复制
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
    }
}

例如

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-02-22 04:26:05

根据Commands from implicit remoting module not available when created from another module's function中的答案,我发现我必须使用导入模块将Exchange Online以及合规性和安全中心模块导入到全局会话中。

导入-模块(导入-PSSession $global:ExchangeSession -AllowClobber -DisableNameChecking) -Global

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60332988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档