jdhitsolutions / PSTeachingTools

:mortar_board: A set of commands and tools for teaching PowerShell. The module should work in Windows PowerShell, PowerShell 7.x and run cross-platform.
MIT License
48 stars 22 forks source link

Cannot change the CookedState back to Raw #11

Closed andreasjordan closed 2 years ago

andreasjordan commented 2 years ago

I can change the CookedState from Raw to something else, but not back to Raw.

I'm too blind to see where in the code this is tested - can you help me?

image

andreasjordan commented 2 years ago

Found the problem:

image

The test if ($CookingState) (line 111) does not use the string but the index of the enum - and that is 0 as it is the first one and so it is false. You should test for the $PSBoundParameters and see if the Parameter was used.

Not a problem but not nice: When piping to Set-Vegetable, $count is always set because of [Parameter(ValueFromPipelineByPropertyName)]. So the Count property is always set as you can see in the verbose output.

andreasjordan commented 2 years ago

Code for copy-paste:

Get-Vegetable -Name zucchini | Set-Vegetable -CookingState Grilled -Passthru
Get-Vegetable -Name zucchini | Set-Vegetable -CookingState Baked -Passthru -Verbose
Get-Vegetable -Name zucchini | Set-Vegetable -CookingState Raw -Passthru -Verbose

$CookingState = 'Raw'
if ($CookingState) { 'will work' } else { 'will not work' }

$CookingState = [PSTeachingTools.VegStatus]::Raw
if ($CookingState) { 'will work' } else { 'will not work' }
jdhitsolutions commented 2 years ago

This is one of the challenges when working with classes and enums in your own code. This wasn't an issue the way I was using the commands for beginning PowerShell classes but there is room for improvement.

jdhitsolutions commented 2 years ago

The other problem I see is that I never finished setting up parameter sets. Working on cleaning this up.

jdhitsolutions commented 2 years ago

I think I have this sorted out. Update to v4.2.0 from the PowerShell Gallery and try again.

andreasjordan commented 2 years ago

Yes, it works.

Only drawback: Verbose is a bit noisey now: image

I think $PSBoundParameters | Out-String | Write-Verbose should be $PSBoundParameters | Out-String | Write-Debug.

jdhitsolutions commented 2 years ago

Verbose is supposed to be noisy. But I get your point. Lately, I've started balancing these kinds of details between verbose and debug streams. Although, because these commands are designed as teaching tools, I don't mind the extra "noise".

andreasjordan commented 2 years ago

Totally agree - thanks for the quick fix.