简体   繁体   English

Powershell远程处理:在脚本中加载本地脚本

[英]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] [解决方法]

  1. Create a session to the remote server 创建到远程服务器的会话
  2. Use the session to load the script2 使用会话加载脚本2
  3. use the same session to run the script1. 使用相同的会话来运行脚本1。

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.

相关问题 使用psexec作为脚本启用PowerShell远程处理 - Enabling PowerShell Remoting with psexec as script Powershell 远程处理 Windows -&gt; Linux:提交参数化脚本 - Powershell remoting Windows -> Linux : submit a parametrized script 本地Powershell脚本可触发Citrix实例中的VBS脚本 - Local Powershell script to trigger VBS script inside Citrix instance 远程处理时通过Powershell脚本访问UNC路径时出错 - Error when accessing UNC paths through powershell script when remoting 批处理脚本运行正常,但通过PowerShell Remoting执行时失败 - Batch script runs fine, but fails when executed through PowerShell Remoting 在具有提升权限的远程系统上运行Powershell脚本以启用远程处理 - Run a powershell script on a remote system with elevated permissions to enable remoting 两次运行脚本时,Powershell隐式远程会话会提示您输入密码 - Powershell Implicit Remoting Session prompts for password when running the script twice 在另一个 Powershell 脚本中运行 Powershell 脚本 - Running a Powershell script inside another Powershell script 如果 ComputerName 是 localhost 或“.”,则将 powershell 脚本作为本地脚本运行 - Run powershell script as local script if ComputerName is localhost or '.' Powershell脚本,是否从CSV加载? - Powershell script, load from CSV?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM