PowerShell / DscResource.Template

MIT License
10 stars 15 forks source link

Mandatory Parameters for get-targetresource #18

Open kungfoome opened 5 years ago

kungfoome commented 5 years ago

I ran into an issue working on another resource passing mandatory parameters to get-target. See here https://github.com/PowerShell/DscResource.Template/blob/2cc2000bfe83bffc9dafea0e61385a659e5815ae/DSCResources/MSFT_ResourceName/MSFT_ResourceName.psm1#L107

This is fine, but the problem is when you have parameters needed for get-target, but they are not mandatory to pass in. An example may be Credential. I may not need a credential to be a mandatory parameter, but I need to pass it to get-target.

I came up with something like this:

    $getTargetResourceParameters = @{
        MandatoryParameter = $ServiceAccountName
        Credential          = $Credential
    }

    @($getTargetResourceParameters.Keys) | ForEach-Object {
        if( !$PSBoundParameters.ContainsKey($_) )
        {
            $getTargetResourceParameters.Remove($_)
        }
    }

    $getTargetResource = Get-TargetResource @getTargetResourceParameters
PlagueHO commented 5 years ago

This looks OK to me (needs style corrections though 😁 I can't help see HQRM violations anymore! 😢 ).

kungfoome commented 5 years ago

@PlagueHO lol thats fine. I'm still getting used to PowerShell styling as this is my first time working on anything PowerShell related at this capacity.

johlju commented 5 years ago

Can we get this snippet as an example in the existing template resource? 🤔 Not sure where we should add best practices like this? Style Guideline in DscResources repo