microsoft / Requirements

PowerShell framework for declaratively defining and idempotently imposing system configurations
MIT License
159 stars 26 forks source link

Format-Checklist does not accept parameter input #29

Open bstrautin opened 5 years ago

bstrautin commented 5 years ago

Format-Checklist fails when passing a RequirementEvent[] object as a parameter. It should format the objects as it does when they are passed in from the pipeline.

Error:

Format-Checklist : Cannot bind parameter 'Date' to the target. Exception setting "Date": "Cannot convert null to type
"System.DateTime"."
At line:25 char:1
+ Format-Checklist -RequirementEvent $output   # fails
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Format-Checklist], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Format-Checklist

Code to reproduce:

$mySystem = [collections.generic.list[int]]::new()

$requirements = @(
    @{
        Name     = "Resource 1"
        Describe = "Resource 1 is present in the system"
        Test     = { $mySystem -contains 1 }
        Set      = { $mySystem.Add(1) | Out-Null; Start-Sleep 1 }
    },
    @{
        Name     = "Resource 2"
        Describe = "Resource 2 is present in the system"
        Test     = { $mySystem -contains 2 }
        Set      = { $mySystem.Add(2) | Out-Null; Start-Sleep 1 }
    }
)

$output = $requirements | Invoke-Requirement

$output | Format-Table | Out-Host            # works
Format-Table -InputObject $output | Out-Host # works

$output | Format-Checklist | Out-Host        # works
Format-Checklist -RequirementEvent $output   # fails
chriskuech commented 5 years ago

Seems like

  1. the parameter attribute should be changed from ValueFromPipeline to ValueFromPipelineByPropertyName
  2. the process block should wrap a foreach loop around the current process block content
  3. update the iterated value from $_ to $event or something similar
chriskuech commented 5 years ago

I should probably add that I'm not sure why anyone would do this. Format-Checklist is meant for real-time updating of the host. If you want non-realtime, you're probably better off using the other Format-* cmdlets that support stream redirection.

bstrautin commented 5 years ago

Users' wants and developers' expectations are rarely in sync. :-)

chriskuech commented 5 years ago

Well, my attempted solution seemed to stop the formatters from accepting pipeline input. Not sure if anyone has any ideas here...