4

I have a simple function on my computer

MYPC> $txt = "Testy McTesterson"
MYPC> function Do-Stuff($file) { 
  cd c:\temp; 
  $txt > $file; 
}

I would like to run it on a remote computer

MYPC> Invoke-Command -ComputerName OTHERPC { Do-Stuff "test.txt" }

Understandably, Do-Stuff does not exist on OTHERPC and this does not work. How could I get it to work though? The Do-Stuff function abstracts some basic code and is invoked in a few other places so I don't want to duplicate it.

Notice that in my example values are passed into the function both via a parameter and via scope closure. Is that possible?

3 Answers 3

9

I don't know how you use the closure value, but you can pass both as parameters like so:

$txt = "Testy McTesterson"
$file = "SomeFile.txt"
function Do-Stuff { param($txt,$file) cd c:\temp; $txt > $file }
Invoke-Command -ComputerName SomeComputer -ScriptBlock ${function:Do-Stuff} -ArgumentList $txt, $file
Sign up to request clarification or add additional context in comments.

2 Comments

This appears to work if the function is defined in the parent script scope. However, if your function is in a module loaded locally, it doesn't appear to work. So far I've not found a solution to running a function defined in a local module, remotely (without the module being present on the remote computer).
To clarify on my above comment I've found the following: If the function is defined in a local module and it's autoloaded by it's location being present in $PSModulePath, the function can't be passed to a remote computer. If the local module is explicitly loaded in the parent script however, the function can be passed to a remote computer.
5

Not sure if anyone still cares, but you can actually load your custom functions saved in a .ps1 file to the remote computer, via a PSSession. I tried it out and it works for me. Check out this guy's solution

2 Comments

Links tend to change or go missing, could you explain some of the contents or quote it here?
Hey, really really super sorry for the late reply. Work and life priorities. Anyway, you can try something like what I did Invoke-Command -Session $myPSSession -FilePath "C:\dir\customFunctionFile.ps1" As long as the PSSession is open, you should be good to use any functions you defined in that loaded file. Don't forget to remove your session when you're done and just as a side note, the loaded file does get unloaded when the session is removed. LMK if you need any further help but this should be more than enough...Aloha!
1

Even less risk that somebody actually cares :), but here's one way of doing it.

MYPC> $myPCscript = @'
$txt = "Testy McTesterson"
function Do-Stuff($file) { 
    cd c:\temp
    $txt > $file
}
'@
MYPC> Invoke-Command -ComputerName OTHERPC { Invoke-Expression $using:myPCscript; Do-Stuff "test.txt" }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.