简体   繁体   中英

powershell session connection cmdlets not work when executed in function

I get connected to Exchange in the cloud by issuing a series of powershell commands:

$credential = new-object -typename ... -argumentList ...
$session = New-PSSession -configurationName ... -connectionUri ... -credential $credential ...
Import-PSSession $session ...

I can then issue commands to do things I need to do, eg,

get-mailbox | ? {$_.aliast -like "*[.]*"} | select alias

alias
-----
john.public
jane.doe
...

However, the cmdlets for obtaining a PSSession are long, and typing them is error-prone even if I could manage to memorize them correctly. So I saved all the three long command lines verbatim in a function:

function get-365session() {
   $credential = new-object -typename ... -argumentList ...
   $session = New-PSSession -configurationName ... -connectionUri ... -credential $credential ...
   Import-PSSession $session ...
}

But it does not work out as expected:

PS> get-365session
ModuleType Version    Name             ExportedCommands
---------- -------    ----             -----------------
...

PS> get-mailbox 
get-mailbox: The term 'get-mailbox' is not recognized as the name of a cmdlt, function, script file, ....

I thought the session was obtained but was gone with the function's "sub-shell" as soon as the function has completed its run. I therefore tried

PS> . get-365session

But it was to no avail.

Hope there is a way and someone can help me out. Many thanks!

You need to import the session in the current scope using Import-Module with the -Global flag.

Your Import-PSSession line should look like

Import-Module (Import-PSSession $session365 -AllowClobber) -Global

Here is a working example:

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

Connect-O365

Reference

This technet forum thread from 2012

Local functions and variables are not available in other sessions. However, you can use the following trick to specify your function as a script block:

Invoke-Command -Session $session -ScriptBlock ${Function:My-FunctionName}

The article I linked to above goes into more detail about more advanced use-cases for this, but as your script doesn't seem to require any parameters. Do note that this requires the use of Invoke-Command to run the code in the other session, from the session where the function is defined , so I'm not sure how, if you could, get the function body if you have already done Enter-PSSession and are currently in the remote session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM