从另一个模块的函数创建时,来自隐式远程处理模块的命令不可用 [英] Commands from implicit remoting module not available when created from another module's function

查看:38
本文介绍了从另一个模块的函数创建时,来自隐式远程处理模块的命令不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是高级 PowerShell 技术的初学者.我正在尝试编写自己的 PS 模块.希望我能正确表达我的问题.

背景:我创建了一个包含所有常用函数的模块,名为 MyTools.PSM1 文件只是点源同一模块文件夹中的其他 PS1 文件.在模块的最后,我使用 Export-ModuleMember -Function * -Alias * -Cmdlet * 导出模块成员.(我还创建了一个清单,但我不确定这对我的问题是否重要.)

其中一个函数(称为 Connect-O365)在一个点源 PS1 文件中定义.该功能自动连接到 Office 365 远程 powershell.函数的关键部分只是做:

$O365PS = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $O365URL -Credential $Credential -Authentication Basic -AllowRedirection -Name "O365-$($Credential.UserName)" #-WarningAction SilentlyContinue导入-PSSession -Session $O365PS -Prefix $CommandPrefix -WarningAction SilentlyContinue -DisableNameCheckingConnect-MsolService -Credential $Credential

Connect-MSOLService"来自不同的模块,MSOnline.

问题:当我打开PowerShell时,通过Import-Module MyTools加载我的模块,然后运行Connect-O365,会话被创建.我看到创建了隐式远程处理模块,并且正在从会话中接收命令(至少进度条告诉我的是).

但是,一旦完成,远程会话中的这些 Office 365 命令都不可用.不过,从本地 Connect-MSOLservice 加载的命令是可用的.

如果我点源定义 Connect-O365 函数的单个 PS1 文件,该函数工作正常.从我的自定义模块加载函数定义时我遇到了问题.换句话说,如果我在模块加载时调用该函数,则导出的命令不可用.但是,如果我通过调用 PS1 文件来加载该函数,则它可以正常工作.

这是范围问题还是当 MyTools 模块最初加载时模块没有导出导入的命令(Export-ModuleMember)?

我使用的是 PowerShell 4.0

显示已加载但在功能完成后不可用的命令的附加屏幕截图.

根据 2012 年的这个问题(我的问题非常相似):"

I'm a beginner to advanced PowerShell techniques. I'm attempting to write my own PS module. Hopefully I can properly articulate my problem.

Background: I've created a module of all of my commonly used functions, called MyTools. The PSM1 file simply dot sources other PS1 files in the same module folder. At the end of the module, I export the module members with Export-ModuleMember -Function * -Alias * -Cmdlet *. (I've also created a manifest, but I'm not sure that matters for my problem.)

One of these functions (called Connect-O365) is defined in one of the dot-sourced PS1 files. The function automates the connection to Office 365 remote powershell. The key parts of the function simply do:

$O365PS = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $O365URL -Credential $Credential -Authentication Basic -AllowRedirection -Name "O365-$($Credential.UserName)" #-WarningAction SilentlyContinue

Import-PSSession -Session $O365PS -Prefix $CommandPrefix -WarningAction SilentlyContinue -DisableNameChecking

Connect-MsolService -Credential $Credential

"Connect-MSOLService" is from a different module, MSOnline.

Problem: When I open PowerShell, load my module via Import-Module MyTools, then run Connect-O365, the session is created. I see that the implicit remoting module is created and the commands are being received from the session (at least that's what the progress bar tells me).

However, none of those Office 365 commands from the remote session are available once it's done. The commands loaded from the local Connect-MSOLservice are available, though.

If I dot-source the individual PS1 file that defines the Connect-O365 function, the function works fine. I just have a problem when loading the function definition from my custom module. In other words, if I call the function when its loaded by the module, the exported commands are not available. Yet if I load the function by calling the PS1 file, it works fine.

Is this a problem of scope or that the imported commands were not exported by the module when the MyTools module was initially loaded (Export-ModuleMember)?

EDITS:

I'm using PowerShell 4.0

Additional screenshot showing the commands that are loaded, yet are not available after the function finishes.

Per this question from 2012 (which my question is remarkably similar to): Import-Pssession is not importing cmdlets when used in a custom module

It suggests wrapping the function with another Import-Module (Connect-O365) -Global.

I've tried this both at the PS prompt (once the MyTools module is loaded), as well as within the function itself using:

Import-Module (Import-PSSession -Session $O365PS -Prefix $CommandPrefix -WarningAction SilentlyContinue -DisableNameChecking -AllowClobber) -Global

But neither worked.

Update [7/23] - Simple illustration of problem added below

This function is stored in a *.PSM1 module file (e.g. TestModule.psm1). The module is then loaded via "Import-Module TestModule"

Function ConnectToAD {
    $Sess1 = New-PSSession -ComputerName DC01 -Credential (Get-Credential)
    Invoke-Command -Session $Sess1 {Import-Module ActiveDirectory}
    Import-PSSession $Sess1 -Prefix Remote -Module ActiveDirectory
}

Export-ModuleMember -Function ConnectToAD

Once the module is loaded, and the function is called, none of the "Remote"-prefixed commands work.

解决方案

Quoting the answer from the other thread that BHall linked above since it worked for me:

"With some assistance from TechNet I was able to modify the script module so it worked the way I expected.

function Connect-O365 {
    $o365cred = Get-Credential username@domain.onmicrosoft.com
    $session365 = New-PSSession `
                    -ConfigurationName Microsoft.Exchange `
                    -ConnectionUri "https://ps.outlook.com/powershell/" `
                    -Credential $o365cred `
                    -Authentication Basic `
                    -AllowRedirection 
    Import-Module (Import-PSSession $session365 -AllowClobber) -Global
}

TechNet Post"

这篇关于从另一个模块的函数创建时,来自隐式远程处理模块的命令不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆