Powershell: Expose `Parameters` on ScriptBlock, same as with functions (`[System.Management.Automation.FunctionInfo]`)

Created on 13 Oct 2020  路  3Comments  路  Source: PowerShell/PowerShell

Summary of the new feature/enhancement

Currently, when I want to list what parameters a function has, I can call (Get-Command fn).Parameters. When I want to do the same with scriptblock, I have to use $block.Ast and manually parse the parameters from the AST, which is quite convoluted. It would be much more convenient if the same .Parameters attribute worked for scriptblocks.

Proposed technical implementation details (optional)

Add .Parameters property to [scriptblock] that returns same output as on [System.Management.Automation.FunctionInfo]

Issue-Question WG-Engine

Most helpful comment

You can do a quick workaround for the same currently:

$function:temp = $scriptblock
(Get-Command temp -CommandType function).Parameters
Remove-Item function:temp

All 3 comments

You can do a quick workaround for the same currently:

$function:temp = $scriptblock
(Get-Command temp -CommandType function).Parameters
Remove-Item function:temp

That's an awesome hack, thanks. :)

It'd be nice if you could just cast a scriptblock to ScriptInfo. Giving it a public constructor that takes ScriptBlock and pulls ExecutionContext from TLS would be great.

Was this page helpful?
0 / 5 - 0 ratings