[英]Powershell Remoting: Load local script inside script
I launch a local script on a remote computer with: 我使用以下命令在远程计算机上启动本地脚本:
Invoke-Command -ComputerName RemoteServer -FilePath C:\myFolder\Script1.ps1
This script dot-sources another script ( Script2 ), which also is located on my local computer in "C:\\myFolder". 该脚本点源另一个脚本( Script2 ),该脚本也位于我的本地计算机的“ C:\\ myFolder”中。 This fails because Script1 trys to load the Script2 from the remote computer. 这将失败,因为Script1 尝试从远程计算机加载Script2 。
Is there a way to load Script2 from my local computer inside Script1 inside the remoting session? 有没有办法在远程会话中的Script1内从本地计算机加载Script2 ?
I don't think this can work the way you outlined it since code running on the remote machine cannot find a file that is relative to the local machine. 我认为这不能像您概述的那样工作,因为在远程计算机上运行的代码无法找到相对于本地计算机的文件。
The only way to achieve this is to share your script and include it as with a UNC path, or an administrative share, like \\\\localmachine\\c$\\users\\test\\script\\a.ps1
. 实现此目的的唯一方法是共享脚本,并将其包含在UNC路径或管理共享中,例如\\\\localmachine\\c$\\users\\test\\script\\a.ps1
。
But there's a chance you'll run into authentication issues (double hop). 但是,您可能会遇到身份验证问题(双跳)。
The (easiest) solution: copy all your scripts to the remote machine first and make sure that the paths used in the scripts will work. (最简单的)解决方案:首先将所有脚本复制到远程计算机,并确保脚本中使用的路径可以使用。
[Workaround] [解决方法]
eg:- $Session = New-PSSession -ComputerName $Server
例如:- $Session = New-PSSession -ComputerName $Server
Invoke-command -Session $Session -FilePath <script2>
this should load functions Invoke-command -Session $Session -FilePath <script2>
这应该加载功能
Invoke-command -Session $Session -FilePath <script1>
here the functions in script2
will be available for script1
to consume, so no need to refer and dot source script2
. 在这里script2
的函数将可供script1
使用,因此无需引用并加点源script2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.