From 514aca93eb30e9960429538ea6cd1223e866315a Mon Sep 17 00:00:00 2001 From: Ricardo Mestre Date: Wed, 27 Nov 2024 09:38:32 +0000 Subject: [PATCH] Fix generation of instance names --- CHANGELOG.md | 4 +++ .../Microsoft365DSC/Modules/M365DSCUtil.psm1 | 36 +++++++------------ 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 230f896151..8c6cef851b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,10 @@ * TeamsUpgradePolicy * DEPRECATED: Users properties. Use the TeamsUserPolicyAssignment resource instead. +* M365DSCUtil + * When exporting generate the instance names of resources with their mandatory + keys instead of random GUIDs , this makes exports idempotent again + FIXES [#5469](https://github.com/microsoft/Microsoft365DSC/issues/5469) * MISC * Removed hardcoded Graph urls and replaced by MSCloudLoginAssistant values. * Add separate module handling for PowerShell Core. diff --git a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 index 93e08311d2..ee5301bb56 100644 --- a/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 +++ b/Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1 @@ -3204,14 +3204,11 @@ function Update-M365DSCDependencies [Parameter()] [Switch] $ValidateOnly, - [Parameter()] [ValidateSet("CurrentUser", "AllUsers")] $Scope = "AllUsers" ) - $isPSResourceGetInstalled = Get-Module -Name Microsoft.PowerShell.PSResourceGet -ListAvailable - try { $Global:MaximumFunctionCount = 32767 @@ -3261,14 +3258,14 @@ function Update-M365DSCDependencies } if (-not $errorFound) { - if (($dependency.PowerShellCore -eq $false -or $dependency.InstallLocation -eq "WindowsPowerShell") -and $Script:IsPowerShellCore) + if (-not $dependency.PowerShellCore -and $Script:IsPowerShellCore) { - Write-Warning "The dependency {$($dependency.ModuleName)} requires Windows PowerShell for installation. Please run Update-M365DSCDependencies in Windows PowerShell." + Write-Warning "The dependency {$($dependency.ModuleName)} does not support PowerShell Core. Please run Update-M365DSCDependencies in Windows PowerShell." continue } elseif ($dependency.PowerShellCore -and -not $Script:IsPowerShellCore) { - Write-Warning "The dependency {$($dependency.ModuleName)} requires PowerShell Core for installation. Please run Update-M365DSCDependencies in PowerShell Core." + Write-Warning "The dependency {$($dependency.ModuleName)} requires PowerShell Core. Please run Update-M365DSCDependencies in PowerShell Core." continue } @@ -3279,15 +3276,7 @@ function Update-M365DSCDependencies Remove-Module 'Microsoft.Graph.Authentication' -Force -ErrorAction SilentlyContinue } Remove-Module $dependency.ModuleName -Force -ErrorAction SilentlyContinue - - if ($null -eq $isPSResourceGetInstalled) - { - Install-Module $dependency.ModuleName -RequiredVersion $dependency.RequiredVersion -AllowClobber -Force -Scope $Scope - } - else - { - Install-PSResource -Name $dependency.ModuleName -Version $dependency.RequiredVersion -AcceptLicense -Scope $Scope -Reinstall -TrustRepository - } + Install-Module $dependency.ModuleName -RequiredVersion $dependency.RequiredVersion -AllowClobber -Force -Scope "$Scope" } } @@ -3787,13 +3776,13 @@ function Get-M365DSCExportContentForResource Import-Module $Resource.Path -Force $moduleInfo = Get-Command -Module $ModuleFullName -ErrorAction SilentlyContinue $cmdInfo = $moduleInfo | Where-Object -FilterScript {$_.Name -eq 'Get-TargetResource'} - $Keys = $cmdInfo.Parameters.Keys + $Keys = $cmdInfo.Parameters.Values.Where({ $_.ParameterSets.Values.IsMandatory }).Name } } else { $cmdInfo = $moduleInfo | Where-Object -FilterScript {$_.Name -eq 'Get-TargetResource'} - $Keys = $cmdInfo.Parameters.Keys + $Keys = $cmdInfo.Parameters.Values.Where({ $_.ParameterSets.Values.IsMandatory }).Name } if ($Keys.Contains('IsSingleInstance')) @@ -3840,9 +3829,14 @@ function Get-M365DSCExportContentForResource { $primaryKey = $Results.UserPrincipalName } - elseif ($Keys.Contains('User')) + + if ([String]::IsNullOrEmpty($primaryKey) -and ` + -not $Keys.Contains('IsSingleInstance')) { - $primaryKey = $Results.User + foreach ($Key in $Keys) + { + $primaryKey += $Results.$Key + } } $instanceName = $ResourceName @@ -3850,10 +3844,6 @@ function Get-M365DSCExportContentForResource { $instanceName += "-$primaryKey" } - elseif (-not $Keys.Contains('IsSingleInstance')) - { - $instanceName += "-" + (New-Guid).ToString() - } if ($Results.ContainsKey('Workload')) {