Skip to content

Use ConvertTo-PSFHashtable for splatting in Get-D365Environment (#870)#911

Open
pavankadabala-png wants to merge 9 commits into
d365collaborative:masterfrom
pavankadabala-png:fix/870-get-d365environment-convertto-psfhashtable
Open

Use ConvertTo-PSFHashtable for splatting in Get-D365Environment (#870)#911
pavankadabala-png wants to merge 9 commits into
d365collaborative:masterfrom
pavankadabala-png:fix/870-get-d365environment-convertto-psfhashtable

Conversation

@pavankadabala-png

Copy link
Copy Markdown
Contributor

What this does

Applies the first of the PSFramework quick-wins from #870 (suggested by @FriedrichWeinmann): replaces the manual Get-DeepClone $PSBoundParameters + .Remove(...) splatting pattern in Get-D365Environment with ConvertTo-PSFHashtable -ReferenceCommand.

# before
$Params = Get-DeepClone $PSBoundParameters
if($Params.ContainsKey("ComputerName")){$null = $Params.Remove("ComputerName")}
if($Params.ContainsKey("OutputServiceDetailsOnly")){$null = $Params.Remove("OutputServiceDetailsOnly")}
if($Params.ContainsKey("OnlyStartTypeAutomatic")){$null = $Params.Remove("OnlyStartTypeAutomatic")}
$Services = Get-ServiceList @Params

# after
$Params = $PSBoundParameters | ConvertTo-PSFHashtable -ReferenceCommand Get-ServiceList
$Services = Get-ServiceList @Params

Why it's equivalent

ConvertTo-PSFHashtable -ReferenceCommand Get-ServiceList keeps only the keys that are parameters of Get-ServiceList. For Get-D365Environment that is exactly All, Aos, Batch, DMF, FinancialReporter — the same set the manual code produced by removing ComputerName, OutputServiceDetailsOnly and OnlyStartTypeAutomatic. As a bonus it also drops any other non-matching parameter automatically, so the filtering stays correct if parameters change later.

ConvertTo-PSFHashtable and its -ReferenceCommand parameter are available in the module's minimum required PSFramework version (1.9.308), so no dependency bump is needed.

Scope

Kept to a single file per the contributing guidelines. The same pattern applies to the sibling *-D365Environment cmdlets and a couple of internal SQL helpers (Get-SqlCommand callers) — happy to follow up with those in separate PRs.

Refs #870

@FH-Inway

Copy link
Copy Markdown
Member

@pavankadabala-png Thanks for cleaning this up, looks good. Since it is probably the same pattern in all the files, I would prefer if we could do it all in one pr, even if this touches multiple files.

@pavankadabala-png

Copy link
Copy Markdown
Contributor Author

Makes sense — done. I've expanded the PR to apply the same ConvertTo-PSFHashtable -ReferenceCommand pattern to all the files that used the manual Get-DeepClone $PSBoundParameters + .Remove(...) splatting:

Get-ServiceList callers

  • Start-D365Environment
  • Start-D365EnvironmentV2
  • Stop-D365Environment

Get-SqlCommand callers (internal)

  • Get-AzureServiceObjective
  • Get-InstanceValues
  • Invoke-ClearAzureSpecificObjects
  • Invoke-ClearSqlSpecificObjects

In each case the resulting splat hashtable is identical to what the manual removals produced — the keys that were stripped are exactly the caller-only parameters that aren't parameters of the reference command. -ReferenceCommand is available in the module's minimum PSFramework (1.9.308), so no dependency bump.

@FH-Inway

Copy link
Copy Markdown
Member

@pavankadabala-png Thanks. Found one more, the Restart-D365Environment command also has this pattern. Could you change that as well, then it's all in one pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants