简体   繁体   English

在函数中执行时,powershell 会话连接 cmdlet 不起作用

[英]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:我通过发出一系列 powershell 命令连接到云中的 Exchange:

$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.但是,用于获取 PSSession 的 cmdlet 很长,即使我能够正确记住它们,输入它们也容易出错。 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.您需要使用带有-Global标志的Import-Module在当前范围内Import-Module会话。

Your Import-PSSession line should look like您的Import-PSSession行应如下所示

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 这个技术网论坛帖子来自 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.请注意,这需要使用Invoke-Command在另一个会话中运行代码,从定义函数的会话开始,所以我不确定如何(如果可以)获取函数主体(如果您已经完成) Enter-PSSession并且当前处于远程会话中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell 模块手动安装。 在帮助下找到但在执行时无法识别的 cmdlet - PowerShell module installed manually. cmdlets found with help but not recognized when executed 在远程 Powershell session 中使用 Active Directory cmdlet - Using Active Directory cmdlets in a remote Powershell session 在与Lync的远程会话中执行标准PowerShell cmdlet - Executing standard PowerShell cmdlets in remote session to Lync 将连接字符串与AzureRM.ServiceBus PowerShell cmdlet一起使用? - Use connection string with AzureRM.ServiceBus PowerShell cmdlets? 在远程会话中执行时,将PowerShell stdout重定向到文件 - Redirect PowerShell stdout to a file when executed in a remote session 通过Powershell远程会话执行时,程序输出是不同的 - Program output is different when executed through Powershell remote session 在远程会话中执行时,PowerShell Get-AppVServerPackage的行为有所不同 - PowerShell Get-AppVServerPackage behaves differently when executed in a remote session 如何使用Powershell cmdlet处理logman结果? - How do I work with logman results using powershell cmdlets? 用于将所有文件夹名称存储在 PowerShell 中的数组中的内置函数或 Cmdlet - Built-in function or Cmdlets to store all folders names in an array in PowerShell 当由PHP执行时,PowerShell脚本不起作用 - PowerShell script doesn't work when executed by PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM