Powershell Remoting:在远程 psession 中使用导入的模块 cmdlet
有没有办法在远程会话中使用本地会话中导入的模块?我查看了 import-pssession,但不知道如何获取本地会话。这是我想做的事情的示例。
import-module .\MyModule\MyModule.ps1
$session = new-pssession -computerName RemoteComputer
invoke-command -session $session -scriptblock { Use-CmdletFromMyModule }
另外,我不想在远程会话中导入模块,因为 ps1 文件不在该服务器上。
Is there a way to use modules that were imported in a local session in a remote session? I looked at import-pssession, but I don't know how to get the local session. Here's a sample of what I want to do.
import-module .\MyModule\MyModule.ps1
$session = new-pssession -computerName RemoteComputer
invoke-command -session $session -scriptblock { Use-CmdletFromMyModule }
Also, I do not want to import-module in the remote session, as the ps1 files are not on that server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我最终破解了这个工作。我所做的是创建一个本地会话,将模块导入到该会话中,并使用 import-pssession 将模块从创建的本地会话导入到远程会话中。这很慢。如果有人有更好的方法来做到这一点,或者如果有人知道如何获取基本会话的实例,我很乐意听取您的意见!
远程处理.psm1
MyModule.psm1
远程处理测试.ps1
I ended up hacking this to work. What I did was create a local session, import modules into that session and used import-pssession to import modules from the created local session into the remote session. This is slow. If anyone has a better way of doing this, or if someone knows how to get an instance of the base session I'd love to hear from you!
Remoting.psm1
MyModule.psm1
RemotingTest.ps1
作为乔纳森提到的替代方案,如果您有想要通过电线推送的源模块,那么您可以毫不费力地做到这一点。如果您有二进制文件,您也许能够做类似的事情。但我想说所有的赌注都在那里。本质上,您将文件作为参数推送到哈希中,写入临时文件,然后导入。
理论上也可以调用
,将当前的本地主机会话导出到模块并通过网络推送它——未经测试的伪代码。这可能行不通...
As an alternate to what Jonathan mentions, if you have source modules you want to push over the wire, then you can do that without too much trouble. If you have binaries, you might be able to do something similar.but I'd say all bets are off there. Essentially you push the files over as params in a hash, write to temp, then import.
Call like
Also theoretically possible, exporting a current localhost session to module and pushing that over the wire -- untested pseudo-code. This might not work...
如果这有帮助:
如果您可以切换到 PowerShell 3.0,那么
Get-Module 和 Import-Module cmdlet 支持远程计算机上的模块:
http: //technet.microsoft.com/en-us/library/hh857339.aspx#BKMK_REM
应该可以在远程计算机上运行 PowerShell,执行导入模块,
远程计算机上没有任何 ps1 脚本。
in case this helps:
if you can switch to PowerShell 3.0, then
the Get-Module and Import-Module cmdlets support modules on remote computers:
http://technet.microsoft.com/en-us/library/hh857339.aspx#BKMK_REM
It should be possible to run PowerShell on remote computer, that performs Import-Module,
without having any ps1 scripts on the remote computer.
所以我一直在寻找类似的东西......在我的例子中,我只需要将单个函数导出到远程会话......这就是我想出的。也许你可以循环它来尝试一下。它不适用于内部命令,但它确实适用于自定义模块中的函数(在我所做的测试中)。
So I was looking for something similar... In my case I just needed to export a single function to a remote session... this is what I came up with. Maybe you could loop over it to try it. It doesn't work with internal commands, but it does work on functions in custom modules (in the testing I've done).
我不相信你可以。您可以采用另一种方式 - 将远程计算机上加载的命令导入到本地计算机上运行的远程会话中。您可以指定一个脚本来调用命令,它会将该脚本复制到远程计算机并运行它。但是,如果您需要管理单元或其他模块,则需要确保它们安装在每台远程计算机上,然后通过命令或脚本将它们加载到远程会话中。
I don't believe you can. You can go the other way - import commands loaded on the remote machine into your remote session running on the local machine. You can specify a script to invoke-command and it will copy that script over to the remote machine and run it. But if you require snapins or additional modules, you will need to make sure those are installed on each remote machine and then load them into the remote session via commands or your script.
我建议如下:
从那时起,您可以在会话中使用 ActiveDirectory cmdlet。
希望这有帮助。
I would suggest something like:
Since then, you can use ActiveDirectory cmdlets in your session.
Hope this helps.
使用 credssp 身份验证
use credssp authentication