
Heredé un código donde un script hace referencia a otro como este:
#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
Esto funciona bien localmente, pero cuando ambos scripts se implementan en un servidor remoto y se llama al script1 mediante el comando invocar, la instancia remota de PS se queja de que no puede encontrar el 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 es utilizado por varios otros scripts (con éxito en el entorno local), por lo que no puedo refactorizar script2 nuevamente en script1.
Entonces, ¿cómo puedo decirle al script1 que llame al script2 de una manera que funcione ya sea que el script se ejecute localmente o en un servidor remoto?
Respuesta1
Bien, esto funciona:
# 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