locked
Calling Function from Script Block RRS feed

  • Question

  • Function foo() {

         echo "Hello"

    }

    $myScriptBlock = {

      param($t)

      foo

      echo $t

    }

     

    $s = new-pssession ......

    Invole-Command -session $s -ScriptBlock $myScriptBlock -ArgumentList "Test";

     

    I get the error saying

    The term 'Foo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
    he spelling of the name, or if a path was included, verify that the path is correct and try again.
        + CategoryInfo          : ObjectNotFound: (SwitchFeature:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Why was I not able to compose a script block which has reference to a function and then execute the whole thing on remote machine?

     

    Thursday, June 2, 2011 4:48 PM

Answers

  • The function only exists in the runspace of the local machine.  If you want to use it in the runspace of the remote machine, you'll need to define it there first.

    $myScriptBlock = {

     param($t)

    Function foo() {

         echo "Hello"

    }

       foo

      echo $t

    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Proposed as answer by Kazun Thursday, June 2, 2011 5:09 PM
    • Marked as answer by MSDN Student Monday, June 6, 2011 10:07 AM
    Thursday, June 2, 2011 5:05 PM
  • Since your using a persistent session you can, but you still have to define that function in that remote runspace before you can use it. 

    You can wrap the function def in a script block, and the run that in the remote session, and it should be usable by any subsequent script blocks for the life of that session:

    $MakeFoo = {
        Function foo() {
         echo "Hello"
        }
    }

    $myScriptBlock = {
      param($t)
      foo
      echo $t
    }

    $myScriptBlock2 = {
      param($t)
      foo
      echo $t
    }
     

    $s = new-pssession ......

    Invole-Command -session $s -ScriptBlock $MakeFoo
    Invole-Command -session $s -ScriptBlock $myScriptBlock -ArgumentList "Test"
    Invole-Command -session $s -ScriptBlock $myScriptBlock2 -ArgumentList "Test2";

    If you also want the fuction to be defined in your local session, you'll need to invoke that script block in the current scope:

    .$MakeFoo


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by MSDN Student Monday, June 6, 2011 10:07 AM
    Friday, June 3, 2011 1:16 PM

All replies

  • The function only exists in the runspace of the local machine.  If you want to use it in the runspace of the remote machine, you'll need to define it there first.

    $myScriptBlock = {

     param($t)

    Function foo() {

         echo "Hello"

    }

       foo

      echo $t

    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Proposed as answer by Kazun Thursday, June 2, 2011 5:09 PM
    • Marked as answer by MSDN Student Monday, June 6, 2011 10:07 AM
    Thursday, June 2, 2011 5:05 PM
  • Yes it works. But is it possible to define a function and use it in two script blocks?

    I want to avoid in-lining the function inside of the script block itself.

    Friday, June 3, 2011 12:40 PM
  • Since your using a persistent session you can, but you still have to define that function in that remote runspace before you can use it. 

    You can wrap the function def in a script block, and the run that in the remote session, and it should be usable by any subsequent script blocks for the life of that session:

    $MakeFoo = {
        Function foo() {
         echo "Hello"
        }
    }

    $myScriptBlock = {
      param($t)
      foo
      echo $t
    }

    $myScriptBlock2 = {
      param($t)
      foo
      echo $t
    }
     

    $s = new-pssession ......

    Invole-Command -session $s -ScriptBlock $MakeFoo
    Invole-Command -session $s -ScriptBlock $myScriptBlock -ArgumentList "Test"
    Invole-Command -session $s -ScriptBlock $myScriptBlock2 -ArgumentList "Test2";

    If you also want the fuction to be defined in your local session, you'll need to invoke that script block in the current scope:

    .$MakeFoo


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by MSDN Student Monday, June 6, 2011 10:07 AM
    Friday, June 3, 2011 1:16 PM