
한 스크립트가 다음과 같이 다른 스크립트를 참조하는 일부 코드를 상속했습니다.
#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이 호출 명령을 통해 호출되면 원격 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