功能完成后导入 PsSession 不可用 [英] Import-PsSession not available after function completes

查看:51
本文介绍了功能完成后导入 PsSession 不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建远程 PS 会话的函数.然后我导入会话,所有导出的命令在代码运行时可用于其他函数.当该功能完成时,有一个可用"的 PS 会话,但是之后没有任何导出的命令可用.下面是一个例子:

I have a function which creates a remote PS Session. I then import the session and all exported commands are available to other functions while the code runs. When the function completes there is an 'available' PS Session however none of the exported commands are available afterward. Here is an example:

Function DoSomething{
    $lyncsession = New-CsOnlineSession -Credential (Get-Credential -Message "Authenticate to Skype for Business Online")
    $remoteSession = Import-PSSession $lyncsession -AllowClobber | Out-Null
}

如果我想再次运行该函数,我需要拆除旧的 PSSession 并创建一个新的(重新进行身份验证).

If I want to run the function again I need to tear down the old PSSession and create a new one (authenticating all over again).

有没有办法在函数内创建 PSSession 并在函数完成后使导出的 cmdlet 可用?

Is there a way to create a PSSession within a function and make the exported cmdlets available when the function is done?

顺便说一句,如果我在函数外运行命令,这不是问题.

By the way, this isn't an issue if I run the commands outside a function.

推荐答案

定义一个全局变量并不能解决这个问题.我不得不简单地重新导入 PSSession(如果它健康可用).

Defining a global variable doesn't solve this one. I had to simply re-import the PSSession (if it's healthy and usable).

这篇关于功能完成后导入 PsSession 不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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