-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveDirectoryDsc: Using splatting when calling cmdlets where line exceeds 120 characters #415
Comments
I gathered the lines that could potentially be affected using the script Get-ScriptLinesWithMinimumLength.ps1. Also the 120 character limit used is calculated including any starting whitespace. 120 characters is not a hard limit, just a hint what might look better in review tools. Any of the below lines might not be better off splitting, splatting or refactoring. Use common sense. 🙂 A bug in the script outputted the same lines several times. Missed that at first. 😕 Below should be a correct output. List updated 25/8/2020
|
A lot of the long lines are the ones where formatting is taking place. Would you be OK with something like this? BeforeWrite-Verbose -Message ($script:localizedData.NotDesiredPropertyState -f 'DisplayName', $DisplayName, $targetResource.DisplayName)
$targetResourceInCompliance = $false AfterWrite-Verbose -Message (
$script:localizedData.NotDesiredPropertyState -f 'ManagedBy', $ManagedBy, $targetResource.ManagedBy
)
$targetResourceInCompliance = $false or AfterWrite-Verbose -Message ($script:localizedData.NotDesiredPropertyState -f
'ManagedBy', $ManagedBy, $targetResource.ManagedBy
)
$targetResourceInCompliance = $false or this last example based off the Style Guidelines AfterWrite-Verbose -Message `
($script:localizedData.NotDesiredPropertyState -f 'ManagedBy', $ManagedBy, $targetResource.ManagedBy)
$targetResourceInCompliance = $false |
I’m good with the first one: Write-Verbose -Message (
$script:localizedData.NotDesiredPropertyState -f 'ManagedBy', $ManagedBy, $targetResource.ManagedBy
)
$targetResourceInCompliance = $false Or if there are one with more variables to format we could use an array after Write-Verbose -Message (
$script:localizedData.NotDesiredPropertyState -f @(
'ManagedBy', $ManagedBy, $targetResource.ManagedBy
)
)
$targetResourceInCompliance = $false Since this issue is a bit old now let’s ask for @X-Guardian opinion too. 🙂 |
Powershell and PSScriptAnalyzer naturally splits and indents after the Write-Verbose -Message ($script:localizedData.NotDesiredPropertyState -f
'ManagedBy', $ManagedBy, $targetResource.ManagedBy) This pattern is used throughout the newer and refactored resources (ADFineGrainedPasswordPolicy, ADManagedServiceAccount etc) |
Sometime there are arguments that are longer so using a array makes it possible to do this Write-Verbose -Message (
$script:localizedData.NotDesiredPropertyState -f @(
('ManagedBy{0}{1}' -f property1, property2)
$ManagedBy
$targetResource.ManagedBy
)
) But then, that probably should be formatted on a row before the Write-Verbose 😊 So I’m good with @X-Guardian’s suggestion. |
There are places in the code that make calls to cmdlets and use a long list of parameters.
Example:
https://github.com/PowerShell/xActiveDirectory/blob/07c878522df6015f5a74692e931ddeeeb5d9decf/DSCResources/MSFT_xADOrganizationalUnit/MSFT_xADOrganizationalUnit.psm1#L28
We should use splatting at these locations throughout the repo to make the code easier to read. See style guideline https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#correct-parameter-usage-in-function-and-cmdlet-calls
The text was updated successfully, but these errors were encountered: