Is it possible to include function reference in scripblock{}?

Welcome Forums General PowerShell Q&A Is it possible to include function reference in scripblock{}?

This topic contains 12 replies, has 4 voices, and was last updated by

GS
 
Participant
2 years, 4 months ago.

  • Author
    Posts
  • #49751
    GS

    Participant
    Points: 1
    Rank: Member

    Hello,

    Is it possible to do something like below?

    function a {
    write-output "AAA"
    }
    
    [scriptblock] $b = {
    a
    }
  • #49753

    Member
    Points: 0
    Rank: Member

    Yep, that's fine. The only difference between a function and a script block assigned to a variable like that is the name. 🙂

  • #49755
    GS

    Participant
    Points: 1
    Rank: Member

    This does not work though. How do I calla function a in scriptblock?

  • #49757

    Member
    Points: 0
    Rank: Member

    Works fine for me. Keep in mind that in your example code, you haven't actually _executed_ the script block that's assigned to the $b variable. If you add this statement after your code, you execute $b, which calls "a":

    & $b
    
  • #49926

    Participant
    Points: 21
    Rank: Member

    Can you give a real example of why you would do this?

  • #49928
    GS

    Participant
    Points: 1
    Rank: Member

    I need to include function in scriptblock and then use tis scripblock as a parameter to third party cmdlet (namely Invoke-Parallel). So far I did not figure out how to do that except for pasting entire function to into scriptblock before passing it to cmdlet.

  • #49930

    Member
    Points: 0
    Rank: Member

    Ah. That would depend on what Invoke-Parallel is doing with that object once you pass it in. It's probably running it in a separate runspace, where your other function doesn't exist.

  • #49932
    GS

    Participant
    Points: 1
    Rank: Member

    Yes, that's what is being used (https://github.com/RamblingCookieMonster/Invoke-Parallel).
    So I assume I have to hardcode everything into [scriptblock] for this to work.

  • #49953

    Participant
    Points: 21
    Rank: Member

    $.02 While Boe is as smart as they come in the PS arena and his scripts are intriguing..I don't think all this runspace stuff is even necessary. If you do, you need to relax your schedule a bit. The box of pop-tarts says to microwave for three seconds..I don't have that kinda time 😀

    Pass your script block to the function as a parameter.

    $b = {$env:computername}

    function a
    {
    param($scriptblock)

    &$scriptblock

    }

    a $b

    Powershell is not meant to be complicated.

  • #49994

    Participant
    Points: 0
    Rank: Member

    this code works.
    Is is not plain function using but not including full function text 🙂

    PS C:\> function aaa($msg) { Write-Host ('FUNC says '+$msg+" to You") }
    PS C:\> $sb1 = [scriptblock]::Create( "function aaa { $function:aaa }; aaa `$_ " )
    PS C:\> 1..10 | invoke-parallel -scriptblock $sb1
    FUNC says 1 to You
    FUNC says 2 to You
    FUNC says 3 to You
    FUNC says 4 to You
    FUNC says 5 to You
    FUNC says 6 to You
    FUNC says 7 to You
    FUNC says 8 to You
    FUNC says 9 to You
    FUNC says 10 to You
    
  • #50012
    GS

    Participant
    Points: 1
    Rank: Member

    What

     $function:aaa 

    inside scriptblock means?

    Greg

  • #50433

    Participant
    Points: 0
    Rank: Member

    you can see it yourself 🙂
    just type

    PS C:\> function aaa($msg) { Write-Host ('FUNC says '+$msg+" to You") }
    PS C:\> $function:aaa

    it's redefinition of your outside function (aaa) in a scriptblock 🙂
    I use $function namespace just like variables $global namespace instead of code typing, bracket matching and so on.

    Technically You get "pasting entire function to into scriptblock before passing it to cmdlet" but without pasting 🙂

  • #50469
    GS

    Participant
    Points: 1
    Rank: Member

    Спасибо. Век живи, век учись.

The topic ‘Is it possible to include function reference in scripblock{}?’ is closed to new replies.