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

Это отлично работает локально, но когда оба скрипта развернуты на удаленном сервере и 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

Связанный контент