Skip to content

Commit

Permalink
Merge pull request #5472 from ricmestre/fix5469
Browse files Browse the repository at this point in the history
M365DSCUtil: Fix generation of instance names
  • Loading branch information
NikCharlebois authored Nov 27, 2024
2 parents e991dd3 + 514aca9 commit 5bcf702
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 13 additions & 23 deletions Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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"
}
}

Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -3840,20 +3829,21 @@ 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
if (-not [System.String]::IsNullOrEmpty($primaryKey))
{
$instanceName += "-$primaryKey"
}
elseif (-not $Keys.Contains('IsSingleInstance'))
{
$instanceName += "-" + (New-Guid).ToString()
}

if ($Results.ContainsKey('Workload'))
{
Expand Down

0 comments on commit 5bcf702

Please sign in to comment.