-
Notifications
You must be signed in to change notification settings - Fork 27
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
Composite best practices: InstanceName #162
Comments
Thanks for solving this particular issue as well as coming up with the idea of best practice guide, @ChristophHannappel. @gaelcolas, @stehlih, @nyanhp, @ykuijs, do you have by accident or purpose written down some guidance already? |
I don't have anything specifically in writing, but I never copy PSBoundParameters and then edit that copy. I have noticed that copying PSBoundParameters copies the pointer to the object, meaning that if you edit the copy you also edit the original object. That is why I always generate a new hashtable using the method you have also used in the 2nd example. |
As soon as you deal with reference types (classes) and not value types (structs) you always deal with references. Also the method described above may duplicate references. The line $a = @{
k1 = 'v1'
k2 = @{
kk1 = 'vv1'
kk2 = 'vv2'
}
}
$b = @{}
$validKeys = 'k1', 'k2'
foreach ($validKey in $validKeys)
{
if ($a.ContainsKey($validKey))
{
$b.Add($validKey, $a.Item($validKey))
}
}
Write-Host '-------- Before removal of kk2 -------------'
Write-Host "Key count of a.k2: $($a.k2.Keys.Count)"
Write-Host "Key count in b.k2: $($b.k2.Keys.Count)"
$b.k2.Remove('kk2')
Write-Host '-------- After removal of kk2 --------------'
Write-Host "Key count of a.k2: $($a.k2.Keys.Count)"
Write-Host "Key count in b.k2: $($b.k2.Keys.Count)" |
After I lost 2 or 3 days on a stupid oversight and wasted @raandree time I'd like to propose something like a best practices page (if there isn't something already) on composite creation and save someone else a lot of time.
I made a composite where i mixed
Get-DscSplattedResource
and the conventional Resource definition syntax.This code ends up make Composites with the Resouce ID
[SharePointFarm]
instead of[SharePointFarm]SharePointFarm
What i didn't know is that while
Get-DscSplattedResource
needs to have theInstanceName
removed the conventional Resource definition needs it to create the ResourceID correctly.After moving the
.Remove('InstanceName')
to theGet-DscSplattedResource
Hashtable it works as expected.For discussion: Could it be a good habit to never remove a Key from the $PSBoundParameters and always call
Get-DscSplattedResource
with a dedicated Hashtable so you won't stab yourself in the back later if you add a resource.Alternatively don't mix Get-DscSplattedResource with the conventional definition.
The text was updated successfully, but these errors were encountered: