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?

George Mauer
  • 117,483
  • 131
  • 382
  • 612

3 Answers3

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
dugas
  • 12,025
  • 3
  • 45
  • 51
  • 1
    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). – Robin Sep 17 '14 at 13:01
  • 2
    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. – Robin Sep 18 '14 at 10:34
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

Community
  • 1
  • 1
Molasses
  • 661
  • 9
  • 12
  • 1
    Links tend to change or go missing, could you explain some of the contents or quote it here? – abarisone Oct 01 '15 at 07:11
  • 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! – Molasses Oct 08 '15 at 04:33
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" }
Ingemar
  • 139
  • 9