问题
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.
回答1:
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"
回答2:
Can you try dot-sourcing the function execution?
. Connect-O365
This would execute the function in the current scope (just like dot-sourcing a script runs it in your own scope).
Import-module is scoped, so this makes some sense. :-)
来源:https://stackoverflow.com/questions/24249271/commands-from-implicit-remoting-module-not-available-when-created-from-another-m