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

我inheritance了一些代码,其中一个脚本引用另一个像这样:

#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-command调用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 ap ath was included, verify that the path is correct and try again. 

Script2被许多其他脚本使用(在本地环境中成功),所以我不能将script2重构回script1。

那么,我该如何告诉script1以无论脚本是在本地还是在远程服务器上运行的方式调用script2?

好的,这个工程:

 # 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