Is it possible to pass scriptblock as parameter to Invoke-Command?

Following produces an error, is it possible to do it and what would be the syntax?

$ScriptBlockCode = {
	param ($Message)

        # scriptblock code ...

Invoke-Command -ArgumentList $ScriptBlockCode -ScriptBlock {
         param ([scriptblock] $sbParam)

         # call scriptblock
         & sbParam

Its a gotcha during remote executions. I do it like.

btwn, hope its a typo not having $

1 Like

yes, “sbParam” is a typo.

I guess to pass $Message parameter to your scriptblock invocation it should be:

$Message = "asdf"

Thank you, this is very useful!

1 Like