Powershell:从另一个远程脚本调用一个远程脚本

Powershell:从另一个远程脚本调用一个远程脚本

我继承了一些代码,其中一个脚本引用另一个脚本,如下所示:

#Script1.ps1
param($arg1)

# do some stuff

# call script2 with argument from script1
.\script2.ps1 $arg1


#script2.ps1
param($arg1)
# do some stuff unique to this script

这在本地运行良好,但是当两个脚本都部署到远程服务器,并且通过invoke命令调用script1时,远程PS实例会抱怨它找不到script2:

invoke-command $remoteServer { param($arg1, $remoteScript) powershell $remoteScript $arg1 } -argumentList $arg1, $remoteScript
# output from script1
# more output from script 1    
# here comes the error when script1 calls script2:
The term '.\script2.ps1' is not recognized as the name of a cmdlet, functi

on, script file, or operable program. Check the spelling of the name, or if a p

ath was included, verify that the path is correct and try again.

许多其他脚本(在本地环境中成功)都使用了 Script2,因此我无法将 script2 重构回 script1。

那么,我怎样才能告诉 script1 调用 script2,使得无论脚本是在本地运行还是在远程服务器上运行都能正常工作?

答案1

好的,这有效:

# script1.ps1

# do stuff

# get the current folder
$currentFolder = Split-Path $MyInvocation.MyCommand.Path

# call the second script in the remote folder with arguments from the first
. (Join-Path $currentFolder script2.ps1) $arg1

相关内容