invoke-command + local fucntion + parameter

hi! i am using a local function combined with invoke-command. The function has a name parameter. How do i pass a string to the name paramater using the below syntax. as always Thank you for any help.

Invoke-Command -ComputerName 'PC01' -ScriptBlock ${function:GetTasks} -Credential $cred

The Get-Tasks function lives locally on your computer, or on the remote computer?

It is a local function on my computer

Gotcha. So… thing is, what you’re doing is probably not going to work the way you want it to. You need to get the function to the remote computer as a first step, and then execute it as a second step. When you execute it, you’ll pass parameters. The help for Invoke-Command (Example 10, I think) shows how to pass local variables to the remote computer to use as parameter values. But first you’ve got to solve the problem of actually getting the local function to the remote runspace; simply executing the function won’t do that, nor will simply referring to it.

For example…

$func = Get-Content function:\my-function
Invoke-Command -computer blah -scriptblock { $using:func } -arg one,two,three

$func will not be a function definition; it’ll be the code inside the function, including the param() block. So this should get the function definition to the remote machine. -Arg should be a positional list of parameter values - those will be fed into the param block on the remote machine and should do what you want, if I’m following you correctly. The trick here is that $func is actually a script, not a function, so there’s no need to actually “call the function.” It’ll simply run as-is.

H Man’s original code should work fine as-is, actually. When you use ${function:Whatever}, it refers to the ScriptBlock that is that function’s definition. (Shorter version of (Get-Item Function:\Whatever).ScriptBlock)

All that should need to be added is the -ArgumentList parameter. (Keep in mind that arguments passed with Invoke-Command are by position, only, so your Name parameter needs to be in the first position for this to work.)

Invoke-Command -ComputerName 'PC01' -ScriptBlock ${function:GetTasks} -Credential $cred -ArgumentList 'SomeName'

thank you both ! much appreciated