PowerShell : GetNewClosure() and Cmdlets with validation

前端 未结 2 1970
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-19 21:18

I\'m trying to understand how .GetNewClosure() works within the context of a script cmdlet in PowerShell 2.

In essence I have a function that returns an object like

2条回答
  •  抹茶落季
    2021-01-19 21:40

    I believe this might work:

    function Get-AnObject {
    param(
          [CmdletBinding()]
          [Parameter(....)]
          [String[]]$Id
          ..
          [ValidateSet('Option1','Option2')]
          [String[]]$Options
        )
    
    ...
    $sb = [scriptblock]::create('$this | Get-ExpensiveStuff')
    $T = New-Object PSCustomObject -Property @{ ..... } 
    $T | Add-Member -MemberType ScriptProperty -Name ExpensiveScriptProperty -Value $sb 
    
    .. }
    

    That delays creation of the script block until run time.

提交回复
热议问题