Search code examples
powershellpowershell-remoting

Powershell Remoting: Load local script inside script


I launch a local script on a remote computer with:

Invoke-Command -ComputerName RemoteServer -FilePath C:\myFolder\Script1.ps1

This script dot-sources another script (Script2), which also is located on my local computer in "C:\myFolder". This fails because Script1 trys to load the Script2 from the remote computer.

Is there a way to load Script2 from my local computer inside Script1 inside the remoting session?


Solution

  • [Workaround]

    1. Create a session to the remote server
    2. Use the session to load the script2
    3. use the same session to run the script1.

    eg:- $Session = New-PSSession -ComputerName $Server

    Invoke-command -Session $Session -FilePath <script2> this should load functions

    Invoke-command -Session $Session -FilePath <script1>

    here the functions in script2 will be available for script1 to consume, so no need to refer and dot source script2 .