Powershell: 1 つのリモート スクリプトから別のリモート スクリプトを呼び出す

Powershell: 1 つのリモート スクリプトから別のリモート スクリプトを呼び出す

次のように、あるスクリプトが別のスクリプトを参照するコードを継承しました。

#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

これはローカルでは正常に動作しますが、両方のスクリプトがリモート サーバーにデプロイされ、script1 がinvoke-command 経由で呼び出されると、リモート 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

関連情報