PowerShell-RFC icon indicating copy to clipboard operation
PowerShell-RFC copied to clipboard

Incremental Dynamic Parameter Bind

Open dkattan opened this issue 2 years ago • 20 comments

Summary

  1. Incremental Binding: As RuntimeDefinedParameter objects are written to the output in the dynamicparam block, they are immediately bound if an argument is available. This behavior differs from the current state, where all parameters are bound after the entire dynamicparam block has executed.

  2. Variable Creation: When a RuntimeDefinedParameter is bound, a variable with the name of that parameter should be created in the dynamicparam block scope. This makes the bound value immediately available for subsequent logic in the dynamicparam block.

I already have a PR with the required code changes: https://github.com/PowerShell/PowerShell/pull/20069  

Example

For brevity, this example assumes a user-supplied New-Parameter function.

Function Get-EdgeUpdates
{
    param()
    dynamicparam
    {   
        $EdgeVersions = Invoke-RestMethod https://edgeupdates.microsoft.com/api/products
        # Emit a RuntimeDefinedParameter to the pipeline
        New-Parameter -Name Product -ValidValues $EdgeVersions.Product -Type ([string]) -Mandatory
        # $Product is immediately available for use
        if($Product)
        {
            $EdgeReleases = $EdgeVersions | ?{$_.Product -eq $Product} | select -Expand Releases # Beta,Stable,Dev,Canary
            New-Parameter -Name Platform -ValidValues ($EdgeReleases.Platform | select -Unique) -Type ([string]) -Mandatory # Windows,Linux,MacOS,Android
            if($Platform)
            {
                $EdgeReleases = $EdgeReleases | ?{$_.Platform -eq $Platform}                
                New-Parameter -Name Architecture -ValidValues ($EdgeReleases.Architecture | select -Unique) -Type ([string]) -Mandatory # x86,x64,arm64,universal
                if($Architecture)
                {
                    $EdgeReleases = $EdgeReleases | ?{$_.Architecture -eq $Architecture}                
                    New-Parameter -Name Version -ValidValues ($EdgeReleases.ProductVersion | select -Unique) -Type ([string]) -Mandatory    
                }
            }
        }
    }
    end
    {
    }
}

dkattan avatar Oct 19 '23 22:10 dkattan

@SydneyhSmith When do things like this get reviewed? I would love to be part of the conversation.

dkattan avatar Nov 17 '23 15:11 dkattan

@JamesWTruher can you see if the Engine WG can review this?

SteveL-MSFT avatar Jan 18 '24 17:01 SteveL-MSFT

@JamesWTruher can you see if the Engine WG can review this?

I'd really love to be a part of the conversation with the working group. This is a non-breaking change, only net new functionality but the last time they reviewed it I was told to create an RFC so I did.

dkattan avatar Jan 18 '24 18:01 dkattan

@SteveL-MSFT Community call is tomorrow, still looking for feedback on this.

dkattan avatar Feb 14 '24 18:02 dkattan

I would really really like to have this feature. So much easier to write and I have several use cases.

IntegriComCalvin avatar Feb 20 '24 21:02 IntegriComCalvin

Would love to see this in PowerShell it would open up some interesting and more complex use cases not possible with the current implementation.

homotechsual avatar Feb 20 '24 22:02 homotechsual

I could definitely find use in this, seems useful for complex multi-site automation tasks that may have a varying set of environments.

gigacodedev avatar Feb 20 '24 23:02 gigacodedev

+1. Have some existing modules that use dynamic parameters which would be streamlined with this. This would also open the door for less confusion when it comes to writing user-driven PowerShell modules (i.e. a helper module for technical folks that are not necessarily PowerShell savvy). This would be awesome.

noaht8um avatar Feb 21 '24 02:02 noaht8um

+1 This would make it substantially easier to deal with situations such as the example, which likely has equal application in dealing with the mess of versions and channels of Office/Apps for Business/Enterprise besides Edge, in another Microsoft example.

This would be helpful in automating both with larger scripting toolsets and in adding better validation to more complex system admin and management scripts, so I support the change.

dszp avatar Feb 21 '24 05:02 dszp

Anything Darren writes generally makes my life easier as one of his customers. Please consider the changes and merge so many, MANY people can benefit from his recommendations. Thanks.

immolate avatar Feb 24 '24 01:02 immolate

I think this will add a lot of incentive for people to jump on dynamicparams. Currently, they don't seem to be as robust as one would assume for something called "dynamicparams".

Dakota-LM avatar Feb 24 '24 18:02 Dakota-LM

I think this will add a lot of incentive for people to jump on dynamicparams. Currently, they don't seem to be as robust as one would assume for something called "dynamicparams".

I 100% agree with this- it seems like Darren is the one trying to make the name true here

MWG-Logan avatar Feb 24 '24 18:02 MWG-Logan

I’d love to see this pushed into production. Would be super helpful.

etopple avatar Feb 25 '24 03:02 etopple

So, net new functionality with no breaking changes. Sounds like a win. What's the problem?

dimitrirodis avatar Feb 26 '24 06:02 dimitrirodis

Agree with all the above comments - can this be prioritized for inclusion ASAP? This PR is already months old.

jbbehar avatar Feb 26 '24 18:02 jbbehar

This would be a huge game changer for sure.

JerBar avatar Feb 27 '24 13:02 JerBar

Add one more to the list that needs this!

andrewqit avatar Mar 14 '24 20:03 andrewqit

I hope this gets implemented dkattan

tylernocwing avatar Mar 14 '24 20:03 tylernocwing

Very needed for partners

herringms avatar Mar 14 '24 20:03 herringms

I think this would be a cool addition! It could make scripts way smarter by adapting on the fly as you define parameters.

dlfoster311 avatar Mar 15 '24 19:03 dlfoster311