From dc1c7e02af9eb88c9ba0bd85ea1ef215eb7aa707 Mon Sep 17 00:00:00 2001 From: Fabien Tschanz Date: Thu, 13 Feb 2025 14:40:22 +0100 Subject: [PATCH 1/2] Fix credential export and indentation in export --- CHANGELOG.md | 5 ++ .../Modules/M365DSCDRGUtil.psm1 | 30 +++-------- .../Microsoft365DSC/Modules/M365DSCUtil.psm1 | 52 ++----------------- 3 files changed, 16 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0c0572625..56d05f4626 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change log for Microsoft365DSC +# UNRELEASED + +* M365DSCUtil + * Fixes an issue with `Credential` property being escaped and indentation. + # 1.25.212.1 * AADApplication diff --git a/Modules/Microsoft365DSC/Modules/M365DSCDRGUtil.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCDRGUtil.psm1 index fdeec8809f..7b55351fba 100644 --- a/Modules/Microsoft365DSC/Modules/M365DSCDRGUtil.psm1 +++ b/Modules/Microsoft365DSC/Modules/M365DSCDRGUtil.psm1 @@ -292,8 +292,9 @@ function Get-M365DSCDRGComplexTypeToString { $currentProperty = @() $IndentLevel++ - foreach ($item in $ComplexObject) + for($i = 0; $i -lt $ComplexObject.Count; $i++) { + $item = $ComplexObject[$i] $splat = @{ 'ComplexObject' = $item 'CIMInstanceName' = $CIMInstanceName @@ -370,7 +371,6 @@ function Get-M365DSCDRGComplexTypeToString { $hashPropertyType = ([Array]($ComplexTypeMapping | Where-Object -FilterScript { $_.Name -eq $key }).CimInstanceName)[0] $hashProperty = $itemValue - #$currentProperty += "`r`n" } else { @@ -387,7 +387,7 @@ function Get-M365DSCDRGComplexTypeToString if ($ComplexObject.$key.Count -gt 0) { $currentProperty += $indent + $key + ' = ' - $currentProperty += '@(' + $currentProperty += "@(" } } @@ -413,11 +413,13 @@ function Get-M365DSCDRGComplexTypeToString } if ($i -ne 0) { - # Remove the line break at the start because every item contains a trailing line break - # which would lead to two line breaks between each item $nestedPropertyString = $nestedPropertyString.Substring(2) } $currentProperty += $nestedPropertyString + if (-not $currentProperty.EndsWith("`r`n")) + { + $currentProperty += "`r`n" + } } $IndentLevel-- } @@ -483,28 +485,12 @@ function Get-M365DSCDRGComplexTypeToString $indent = '' $indent = ' ' * ($IndentLevel -1) - if ($key -in $ComplexTypeMapping.Name) + if ($key -in $ComplexTypeMapping.Name -and -not $currentProperty.EndsWith("`r`n")) { $currentProperty += "`r`n" } $currentProperty += "$indent}" - <# - if ($IsArray -or $IndentLevel -gt 4) - { - $currentProperty += "`r`n" - } - #> - - #Indenting last parenthesis when the cim instance is an array - <# - if ($IndentLevel -eq 5) - { - $indent = ' ' * ($IndentLevel -2) - $currentProperty += $indent - } - #> - $emptyCIM = $currentProperty.Replace(' ', '').Replace("`r`n", '') if ($emptyCIM -eq "MSFT_$CIMInstanceName{}") { diff --git a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 index f329c58844..42a7cdc4e1 100644 --- a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 +++ b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 @@ -3603,6 +3603,7 @@ function Update-M365DSCExportAuthenticationResults if ($ConnectionMode -eq 'Credentials') { $Results.Credential = Resolve-Credentials -UserName 'credential' + $noEscape += 'Credential' if ($Results.ContainsKey('ApplicationId')) { $Results.Remove('ApplicationId') | Out-Null @@ -3631,6 +3632,7 @@ function Update-M365DSCExportAuthenticationResults elseif ($ConnectionMode -eq 'CredentialsWithTenantId') { $Results.Credential = Resolve-Credentials -UserName 'credential' + $noEscape += 'Credential' if ($Results.ContainsKey('ApplicationId')) { $Results.Remove('ApplicationId') | Out-Null @@ -3661,6 +3663,7 @@ function Update-M365DSCExportAuthenticationResults elseif ($Results.ContainsKey('Credential') -and $ConnectionMode -eq 'CredentialsWithApplicationId') { $Results.Credential = Resolve-Credentials -UserName 'credential' + $noEscape += 'Credential' } if (-not [System.String]::IsNullOrEmpty($Results.ApplicationId)) { @@ -3950,55 +3953,6 @@ function Get-M365DSCExportContentForResource [void]$content.Append(" $ResourceName `"$instanceName`"`r`n") [void]$content.Append(" {`r`n") $partialContent = Get-DSCBlock -Params $Results -ModulePath $ModulePath -NoEscape $NoEscape - # Test for both Credentials and CredentialsWithApplicationId - if ($ConnectionMode -match 'Credentials') - { - $partialContent = Convert-DSCStringParamToVariable -DSCBlock $partialContent ` - -ParameterName 'Credential' - if (![System.String]::IsNullOrEmpty($Results.ApplicationId)) - { - $partialContent = Convert-DSCStringParamToVariable -DSCBlock $partialContent ` - -ParameterName 'ApplicationId' - } - } - else - { - if (![System.String]::IsNullOrEmpty($Results.ApplicationId)) - { - $partialContent = Convert-DSCStringParamToVariable -DSCBlock $partialContent ` - -ParameterName 'ApplicationId' - } - if (![System.String]::IsNullOrEmpty($Results.TenantId)) - { - $partialContent = Convert-DSCStringParamToVariable -DSCBlock $partialContent ` - -ParameterName 'TenantId' - } - if (![System.String]::IsNullOrEmpty($Results.ApplicationSecret)) - { - $partialContent = Convert-DSCStringParamToVariable -DSCBlock $partialContent ` - -ParameterName 'ApplicationSecret' - } - if (![System.String]::IsNullOrEmpty($Results.CertificatePath)) - { - $partialContent = Convert-DSCStringParamToVariable -DSCBlock $partialContent ` - -ParameterName 'CertificatePath' - } - if (![System.String]::IsNullOrEmpty($Results.CertificateThumbprint)) - { - $partialContent = Convert-DSCStringParamToVariable -DSCBlock $partialContent ` - -ParameterName 'CertificateThumbprint' - } - if (![System.String]::IsNullOrEmpty($Results.CertificatePassword)) - { - $partialContent = Convert-DSCStringParamToVariable -DSCBlock $partialContent ` - -ParameterName 'CertificatePassword' - } - if (![System.String]::IsNullOrEmpty($Results.AccessTokens)) - { - $partialContent = Convert-DSCStringParamToVariable -DSCBlock $partialContent ` - -ParameterName 'AccessTokens' - } - } if ($partialContent.ToLower().IndexOf($OrganizationName.ToLower()) -gt 0) { From 6bd7885e660e80011e39d9adf883aaccb21f0750 Mon Sep 17 00:00:00 2001 From: Fabien Tschanz Date: Thu, 13 Feb 2025 17:20:27 +0100 Subject: [PATCH 2/2] Allow variables in strings and no authentication results update --- CHANGELOG.md | 2 ++ .../Microsoft365DSC/Modules/M365DSCUtil.psm1 | 23 ++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56d05f4626..f4d52c2e45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ * M365DSCUtil * Fixes an issue with `Credential` property being escaped and indentation. + * Adds the possibility to allow variables in strings and no authentication + results update during conversion to final export. # 1.25.212.1 diff --git a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 index 42a7cdc4e1..f637d76470 100644 --- a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 +++ b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 @@ -3810,13 +3810,24 @@ function Get-M365DSCExportContentForResource [Parameter()] [System.String[]] - $NoEscape + $NoEscape, + + [Parameter()] + [switch] + $SkipAuthenticationUpdate, + + [Parameter()] + [switch] + $AllowVariablesInStrings ) - $withoutAuthentication = Update-M365DSCExportAuthenticationResults -ConnectionMode $ConnectionMode ` - -Results $Results - $Results = $withoutAuthentication.Results - $NoEscape += $withoutAuthentication.NoEscape + if (-not $SkipAuthenticationUpdate) + { + $withoutAuthentication = Update-M365DSCExportAuthenticationResults -ConnectionMode $ConnectionMode ` + -Results $Results + $Results = $withoutAuthentication.Results + $NoEscape += $withoutAuthentication.NoEscape + } $NoEscape = $NoEscape | Select-Object -Unique $OrganizationName = '' @@ -3952,7 +3963,7 @@ function Get-M365DSCExportContentForResource $content = [System.Text.StringBuilder]::New() [void]$content.Append(" $ResourceName `"$instanceName`"`r`n") [void]$content.Append(" {`r`n") - $partialContent = Get-DSCBlock -Params $Results -ModulePath $ModulePath -NoEscape $NoEscape + $partialContent = Get-DSCBlock -Params $Results -ModulePath $ModulePath -NoEscape $NoEscape -AllowVariablesInStrings:$AllowVariablesInStrings if ($partialContent.ToLower().IndexOf($OrganizationName.ToLower()) -gt 0) {