Skip to content

Commit

Permalink
Improve resource generator
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienTschanz committed Jun 5, 2024
1 parent 263f47e commit b06ae06
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
86 changes: 45 additions & 41 deletions ResourceGenerator/M365DSCResourceGenerator.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,14 @@ function New-M365DSCResource
{
$getAlternativeFilterString.AppendLine(" -Filter `"$alternativeKey eq '`$$alternativeKey'`" ``") | Out-Null
$getAlternativeFilterString.AppendLine(" -ErrorAction SilentlyContinue | Where-Object ``") | Out-Null
$getAlternativeFilterString.AppendLine(" -FilterScript { ``") | Out-Null
$getAlternativeFilterString.AppendLine(" -FilterScript {") | Out-Null
$getAlternativeFilterString.AppendLine(" `$_.AdditionalProperties.'@odata.type' -eq `"`#microsoft.graph.$SelectedODataType`" ``") | Out-Null
$getAlternativeFilterString.Append(" }") | Out-Null
}
else
{
$getAlternativeFilterString.AppendLine(" -ErrorAction SilentlyContinue | Where-Object ``") | Out-Null
$getAlternativeFilterString.AppendLine(" -FilterScript { ``") | Out-Null
$getAlternativeFilterString.AppendLine(" -FilterScript {") | Out-Null
$getAlternativeFilterString.AppendLine(" `$_.$alternativeKey -eq `"`$(`$$alternativeKey)`" ``") | Out-Null
$getAlternativeFilterString.AppendLine(" -and `$_.AdditionalProperties.'@odata.type' -eq `"`#microsoft.graph.$SelectedODataType`" ``") | Out-Null
$getAlternativeFilterString.Append(" }") | Out-Null
Expand Down Expand Up @@ -477,16 +477,16 @@ function New-M365DSCResource
$newDefaultParameterSet = $newCmdlet.ParameterSets | Where-Object -FilterScript { $_.Name -eq 'Create' }
[Array]$newKeyIdentifier = ($newDefaultParameterSet.Parameters | Where-Object -FilterScript { $_.IsMandatory }).Name
$defaultCreateParameters = @"
`$CreateParameters = ([Hashtable]`$BoundParameters).clone()
`$CreateParameters = Rename-M365DSCCimInstanceParameter -Properties `$CreateParameters
`$CreateParameters.Remove('Id') | Out-Null
`$createParameters = ([Hashtable]`$BoundParameters).clone()
`$createParameters = Rename-M365DSCCimInstanceParameter -Properties `$createParameters
`$createParameters.Remove('Id') | Out-Null
`$keys = (([Hashtable]`$CreateParameters).clone()).Keys
`$keys = (([Hashtable]`$createParameters).clone()).Keys
foreach (`$key in `$keys)
{
if (`$null -ne `$CreateParameters.`$key -and `$CreateParameters.`$key.getType().Name -like '*cimInstance*')
if (`$null -ne `$createParameters.`$key -and `$createParameters.`$key.GetType().Name -like '*cimInstance*')
{
`$CreateParameters.`$key = Convert-M365DSCDRGComplexTypeToHashtable -ComplexObject `$CreateParameters.`$key
`$createParameters.`$key = Convert-M365DSCDRGComplexTypeToHashtable -ComplexObject `$createParameters.`$key
}
}
"@
Expand Down Expand Up @@ -520,7 +520,7 @@ function New-M365DSCResource
$keyValue = $key
if ($key -eq 'BodyParameter')
{
$keyValue = 'CreateParameters'
$keyValue = 'createParameters'
}
$newParameterString.Append("-$key `$$keyValue") | Out-Null
}
Expand All @@ -529,7 +529,7 @@ function New-M365DSCResource
$odataType = $null
if ($true)#$isAdditionalProperty)
{
$odataType = " `$CreateParameters.Add(`"@odata.type`", `"#microsoft.graph.$SelectedODataType`")`r`n"
$odataType = " `$createParameters.Add(`"@odata.type`", `"#microsoft.graph.$SelectedODataType`")`r`n"
}

$settingsCatalogProperties = ""
Expand All @@ -544,14 +544,14 @@ function New-M365DSCResource
"@

$defaultCreateParameters = @"
`$settings = Get-IntuneSettingCatalogPolicySetting `
-DSCParams ([System.Collections.Hashtable]`$BoundParameters) `
`$settings = Get-IntuneSettingCatalogPolicySetting ``
-DSCParams ([System.Collections.Hashtable]`$BoundParameters) ``
-TemplateId `$templateReferenceId
`$CreateParameters = @{
`$createParameters = @{
Name = `$DisplayName
Description = `$Description
TemplateReference = @{templateId = `$templateReferenceId }
TemplateReference = @{ templateId = `$templateReferenceId }
Platforms = `$platforms
Technologies = `$technologies
Settings = `$settings
Expand Down Expand Up @@ -586,14 +586,14 @@ function New-M365DSCResource
if ($isAdditionalProperty -and $CmdletNoun -notlike "*DeviceManagementConfigurationPolicy")
{
$exportGetCommand.AppendLine(" -ErrorAction Stop | Where-Object ``") | Out-Null
$exportGetCommand.AppendLine(" -FilterScript { ``") | Out-Null
$exportGetCommand.AppendLine(" -FilterScript {") | Out-Null
$exportGetCommand.AppendLine(" `$_.AdditionalProperties.'@odata.type' -eq '#microsoft.graph.$($selectedODataType)' ``") | Out-Null
$exportGetCommand.AppendLine(" }") | Out-Null
}
elseif ($CmdletNoun -like "*DeviceManagementConfigurationPolicy")
{
$exportGetCommand.AppendLine(" -ErrorAction Stop | Where-Object ``") | Out-Null
$exportGetCommand.AppendLine(" -FilterScript { ``") | Out-Null
$exportGetCommand.AppendLine(" -FilterScript {") | Out-Null
$exportGetCommand.AppendLine(" `$_.TemplateReference.TemplateId -eq `$policyTemplateID ``") | Out-Null
$exportGetCommand.AppendLine(" }") | Out-Null
}
Expand Down Expand Up @@ -668,17 +668,17 @@ function New-M365DSCResource
$updateKeyIdentifier = ""
$updateCmdletName = ""
$defaultUpdateParameters = @"
`$settings = Get-IntuneSettingCatalogPolicySetting `
-DSCParams ([System.Collections.Hashtable]`$BoundParameters) `
`$settings = Get-IntuneSettingCatalogPolicySetting ``
-DSCParams ([System.Collections.Hashtable]`$BoundParameters) ``
-TemplateId `$templateReferenceId
Update-DeviceManagementConfigurationPolicy `
-DeviceManagementConfigurationPolicyId `$currentInstance.Id `
-DisplayName `$DisplayName `
-Description `$Description `
-TemplateReference `$templateReferenceId `
-Platforms `$platforms `
-Technologies `$technologies `
Update-DeviceManagementConfigurationPolicy ``
-DeviceManagementConfigurationPolicyId `$currentInstance.Id ``
-DisplayName `$DisplayName ``
-Description `$Description ``
-TemplateReference `$templateReferenceId ``
-Platforms `$platforms ``
-Technologies `$technologies ``
-Settings `$settings`r`n
"@
}
Expand Down Expand Up @@ -733,25 +733,26 @@ function New-M365DSCResource
$AssignmentsNew += " `$assignmentsHash = @()`r`n"
$AssignmentsNew += " foreach (`$assignment in `$Assignments)`r`n"
$AssignmentsNew += " {`r`n"
$AssignmentsNew += " `$assignmentsHash += Get-M365DSCDRGComplexTypeToHashtable -ComplexObject `$Assignment`r`n"
$AssignmentsNew += " `$assignmentsHash += Get-M365DSCDRGComplexTypeToHashtable -ComplexObject `$assignment`r`n"
$AssignmentsNew += " }`r`n"
$AssignmentsNew += "`r`n"
$AssignmentsNew += " if (`$policy.Id)`r`n"
$AssignmentsNew += " {`r`n"
$AssignmentsNew += " Update-DeviceConfigurationPolicyAssignment -DeviceConfigurationPolicyId `$policy.Id ```r`n"
$AssignmentsNew += " Update-DeviceConfigurationPolicyAssignment ```r`n"
$AssignmentsNew += " -DeviceConfigurationPolicyId `$policy.Id ```r`n"
$AssignmentsNew += " -Targets `$assignmentsHash ```r`n"
$AssignmentsNew += " -Repository '$repository'`r`n"
$AssignmentsNew += " }`r`n"

$AssignmentsUpdate += " `$assignmentsHash = @()`r`n"
$AssignmentsUpdate += " foreach (`$assignment in `$Assignments)`r`n"
$AssignmentsUpdate += " {`r`n"
$AssignmentsUpdate += " `$assignmentsHash += Get-M365DSCDRGComplexTypeToHashtable -ComplexObject `$Assignment`r`n"
$AssignmentsUpdate += " `$assignmentsHash += Get-M365DSCDRGComplexTypeToHashtable -ComplexObject `$assignment`r`n"
$AssignmentsUpdate += " }`r`n"
$AssignmentsUpdate += " Update-DeviceConfigurationPolicyAssignment ```r`n"
$AssignmentsUpdate += " -DeviceConfigurationPolicyId `$currentInstance.Id ```r`n"
$AssignmentsUpdate += " -Targets `$assignmentsHash ```r`n"
$AssignmentsUpdate += " -Repository '$repository'`r`n"
$AssignmentsUpdate += " -Repository '$repository'"

$AssignmentsCIM = @'
[ClassVersion("1.0.0.0")]
Expand All @@ -766,9 +767,9 @@ class MSFT_DeviceManagementConfigurationPolicyAssignments
};
'@
$AssignmentsProperty = " [Write, Description(`"Represents the assignment to the Intune policy.`"), EmbeddedInstance(`"MSFT_DeviceManagementConfigurationPolicyAssignments`")] String Assignments[];`r`n"
$AssignmentsProperty = "`r`n [Write, Description(`"Represents the assignment to the Intune policy.`"), EmbeddedInstance(`"MSFT_DeviceManagementConfigurationPolicyAssignments`")] String Assignments[];`r`n"
$AssignmentsConvertComplexToString = @"
if (`$Results.Assignments)
`r`n if (`$Results.Assignments)
{
`$complexTypeStringResult = Get-M365DSCDRGComplexTypeToString -ComplexObject `$Results.Assignments -CIMInstanceName DeviceManagementConfigurationPolicyAssignments
if (`$complexTypeStringResult)
Expand All @@ -779,12 +780,12 @@ class MSFT_DeviceManagementConfigurationPolicyAssignments
{
`$Results.Remove('Assignments') | Out-Null
}
}
}`r`n
"@
$AssignmentsConvertComplexToVariable = @"
if (`$Results.Assignments)
`r`n if (`$Results.Assignments)
{
`$currentDSCBlock = Convert-DSCStringParamToVariable -DSCBlock `$currentDSCBlock -ParameterName "Assignments" -isCIMArray:`$true
`$currentDSCBlock = Convert-DSCStringParamToVariable -DSCBlock `$currentDSCBlock -ParameterName "Assignments" -IsCIMArray:`$true
}
"@
Expand Down Expand Up @@ -3310,7 +3311,7 @@ function New-M365HashTableMapping

$convertToVariable += " if (`$Results.$parameterName)`r`n"
$convertToVariable += " {`r`n"
$convertToVariable += " `$currentDSCBlock = Convert-DSCStringParamToVariable -DSCBlock `$currentDSCBlock -ParameterName `"$parameterName`" -isCIMArray:`$$($property.IsArray)`r`n"
$convertToVariable += " `$currentDSCBlock = Convert-DSCStringParamToVariable -DSCBlock `$currentDSCBlock -ParameterName `"$parameterName`" -IsCIMArray:`$$($property.IsArray)`r`n"
$convertToVariable += " }`r`n"
}
if ($property.IsEnumType)
Expand Down Expand Up @@ -3782,8 +3783,9 @@ function New-SettingsCatalogSettingDefinitionSettingsFromTemplate {

$childSettings = @()
$childSettings += $SettingTemplate.SettingDefinitions | Where-Object -FilterScript {
($_.AdditionalProperties.dependentOn.Count -gt 0 -and $_.AdditionalProperties.dependentOn.parentSettingId.Contains($SettingDefinition.Id)) -or
($_.AdditionalProperties.options.dependentOn.Count -gt 0 -and $_.AdditionalProperties.options.dependentOn.parentSettingId.Contains($SettingDefinition.Id))
$_.visibility -notlike "*none*" -and `
(($_.AdditionalProperties.dependentOn.Count -gt 0 -and $_.AdditionalProperties.dependentOn.parentSettingId.Contains($SettingDefinition.Id)) -or
($_.AdditionalProperties.options.dependentOn.Count -gt 0 -and $_.AdditionalProperties.options.dependentOn.parentSettingId.Contains($SettingDefinition.Id)))
}

$setting = [ordered]@{
Expand Down Expand Up @@ -3830,26 +3832,28 @@ function New-ParameterDefinitionFromSettingsCatalogTemplateSetting {
"SimpleIntegerCollection" = "System.Int32[]"
}

$mofParameterTemplate = " [Write, Description(""<DisplayName> <Options>"")<ValueMap>] <Type> <Name><Collection>;"
$mofParameterTemplate = " [Write, Description(""<DisplayName><Options>"")<ValueMap>] <Type> <Name><Collection>;"
$powerShellParameterTemplate = @"
[Parameter()]<Restriction>
[<Type>]
$<Name>
"@

$mofDefinition = $mofParameterTemplate.Replace("<DisplayName>", $TemplateSetting.DisplayName)
$optionsString = ""
$valueMapString = ""
if ($TemplateSetting.Options) {
$options = @()
$values = @()
$TemplateSetting.Options | ForEach-Object {
$options += "$($_.Id)" + ": " + $_.Name.Replace("""", "'")
$values += """$($_.Id)"""
}
$optionsString = "(" + ($options -join ", ") + ")"
$ValueMapString = ", ValueMap{$($values -join ", ")}, Values{$($values -join ", ")}"
$optionsString = " (" + ($options -join ", ") + ")"
$valueMapString = ", ValueMap{$($values -join ", ")}, Values{$($values -join ", ")}"
}
$mofDefinition = $mofDefinition.Replace("<Options>", $optionsString)
$mofDefinition = $mofDefinition.Replace("<ValueMap>", $ValueMapString)
$mofDefinition = $mofDefinition.Replace("<ValueMap>", $valueMapString)
$mofDefinition = $mofDefinition.Replace("<Type>", $mofTypeMapping[$TemplateSetting.Type])
$mofDefinition = $mofDefinition.Replace("<Name>", $TemplateSetting.Name)
$isCollection = $TemplateSetting.Type -like "*Collection"
Expand Down
12 changes: 6 additions & 6 deletions ResourceGenerator/Module.Template.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ function Get-TargetResource
if ($null -eq $getValue)
{
Write-Verbose -Message "Could not find an <ResourceDescription> with <PrimaryKey> {$<PrimaryKey>}"
Write-Verbose -Message "Could not find an <ResourceDescription> with <PrimaryKey> {$<PrimaryKey>}."
if (-not [string]::IsNullOrEmpty($<FilterKey>))
if (-not [System.String]::IsNullOrEmpty($<FilterKey>))
{
$getValue = <GetCmdLetName> `
<AlternativeFilter>
Expand All @@ -78,7 +78,7 @@ function Get-TargetResource
#endregionResourceGenerator#>
if ($null -eq $getValue)
{
Write-Verbose -Message "Could not find an <ResourceDescription> with <FilterKey> {$<FilterKey>}"
Write-Verbose -Message "Could not find an <ResourceDescription> with <FilterKey> {$<FilterKey>}."
return $nullResult
}
$<PrimaryKey> = $getValue.<PrimaryKey>
Expand Down Expand Up @@ -176,12 +176,12 @@ function Set-TargetResource
<#AssignmentsRemove#>
<#DefaultUpdateParameters#><#ResourceGenerator
#region resource generator code
<UpdateDataType><UpdateCmdLetName> <#UpdateKeyIdentifier#>
<#AssignmentsUpdate#> #endregionResourceGenerator#>
<UpdateDataType><UpdateCmdLetName><#UpdateKeyIdentifier#><#AssignmentsUpdate#>
#endregionResourceGenerator#>
}
elseif ($Ensure -eq 'Absent' -and $currentInstance.Ensure -eq 'Present')
{
Write-Verbose -Message "Removing the <ResourceDescription> with <PrimaryKey> {$($currentInstance.<PrimaryKey>)}" <#ResourceGenerator
Write-Verbose -Message "Removing the <ResourceDescription> with <PrimaryKey> {$($currentInstance.<PrimaryKey>)}"<#ResourceGenerator
#region resource generator code
<RemoveCmdLetName> <#removeKeyIdentifier#>
#endregionResourceGenerator#>
Expand Down

0 comments on commit b06ae06

Please sign in to comment.