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

This topic contains 12 replies, has 4 voices, and was last updated by Profile photo of GS GS 1 month, 1 week ago.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #49751
    Profile photo of GS
    GS
    Participant

    Hello,

    Is it possible to do something like below?

    function a {
    write-output "AAA"
    }
    
    [scriptblock] $b = {
    a
    }
    #49753
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

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

    #49755
    Profile photo of GS
    GS
    Participant

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

    #49757
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

    #49928
    Profile photo of GS
    GS
    Participant

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of GS
    GS
    Participant

    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
    Profile photo of Dan Potter
    Dan Potter
    Participant

    $.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.

    • This reply was modified 1 month, 2 weeks ago by Profile photo of Dan Potter Dan Potter.
    #49994
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    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
    Profile photo of GS
    GS
    Participant

    What

     $function:aaa 

    inside scriptblock means?

    Greg

    #50433
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    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
    Profile photo of GS
    GS
    Participant

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

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.