Skip to content

Commit

Permalink
Improve PowerShell 7 support with Microsoft365DSC
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienTschanz committed Jul 29, 2024
1 parent 44d2c4a commit 3872aaf
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions Modules/DSCParser/Modules/DSCParser.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function Update-DSCResultWithMetadata
$Script:IsPowerShellCore = $PSVersionTable.PSEdition -eq 'Core'

if ($Script:IsPowerShellCore)
{
Import-Module -Name 'PSDesiredStateConfiguration' -MinimumVersion 2.0.7 -Prefix 'Pwsh'
}

function Update-DSCResultWithMetadata
{
[CmdletBinding()]
[OutputType([Array])]
Expand Down Expand Up @@ -47,7 +54,7 @@
} while ($tokens[$i-$stepback].Kind -ne 'Identifier' -and $tokens[$i-$stepback].Kind -ne 'NewLine')
if ($tokens[$i-$stepback].Kind -eq 'Identifier') {
$commentAssociatedProperty = $tokens[$i-$stepback].Text

# Loop through all instances in the ParsedObject to retrieve
# the one associated with the comment.
for ($j = 0; $j -le $ParsedObject.Length; $j++)
Expand Down Expand Up @@ -125,7 +132,14 @@ function ConvertFrom-CIMInstanceToHashtable

if ($null -eq $CIMClassObject)
{
$dscResourceInfo = Get-DSCResource -Name $ResourceName
if ($Script:IsPowerShellCore)
{
$dscResourceInfo = Get-PwshDscResource -Name $ResourceName
}
else
{
$dscResourceInfo = Get-DscResource -Name $ResourceName
}
$InvokeParams = @{
Name = $ResourceName
Method = 'Get'
Expand All @@ -142,7 +156,7 @@ function ConvertFrom-CIMInstanceToHashtable
try
{
Invoke-DscResource @InvokeParams | Out-Null

$CIMClassObject = Get-CimClass -ClassName $CimInstanceName `
-Namespace 'ROOT/Microsoft/Windows/DesiredStateConfiguration' `
-ErrorAction SilentlyContinue
Expand Down Expand Up @@ -258,7 +272,7 @@ function ConvertFrom-CIMInstanceToHashtable
[array]$regexResult = [Regex]::Split($subExpression, $regex)

for ($i = 0; $i -lt $regexResult.Count; $i++)
{
{
$regexResult[$i] = $regexResult[$i].Trim().Trim("'").Trim('"')
}

Expand All @@ -272,20 +286,20 @@ function ConvertFrom-CIMInstanceToHashtable
{
$convertedFromString = $subExpression.ToString() | ConvertFrom-String -Delimiter "`n"
}

if ([String]::IsNullOrEmpty($convertedFromString))
{
$convertedFromString = $subExpression.ToString() | ConvertFrom-String -Delimiter "`r`n"
}

if ([String]::IsNullOrEmpty($convertedFromString))
{
$convertedFromString = $subExpression.ToString() | ConvertFrom-String
}

if (-not [String]::IsNullOrEmpty($convertedFromString))
{
$definitions = ($convertedFromString | Get-Member | ?{ $_.Name -match "P\d+" }).Definition
$definitions = ($convertedFromString | Get-Member | Where-Object -FilterScript { $_.Name -match "P\d+" }).Definition
$subExpression = @()
foreach ($definition in $definitions)
{
Expand All @@ -296,7 +310,7 @@ function ConvertFrom-CIMInstanceToHashtable
{
$subExpression = $subExpression.ToString().Trim().Trim("`"").Trim("'")
}

if ($subExpression.Count -eq 1)
{
$currentResult.Add($entry.Item1.ToString(), $subExpression)
Expand Down Expand Up @@ -451,13 +465,13 @@ function ConvertTo-DSCObject
if ($statement.CommandElements[$i].ParameterName -eq 'ModuleName' -and `
($i+1) -lt $statement.CommandElements.Count)
{
$moduleName = $statement.CommandElements[$i+1].Value
$moduleName = $statement.CommandElements[$i+1].Value
$currentModule.Add('ModuleName', $moduleName)
}
elseif ($statement.CommandElements[$i].ParameterName -eq 'ModuleVersion' -and `
($i+1) -lt $statement.CommandElements.Count)
{
$moduleVersion = $statement.CommandElements[$i+1].Value
$moduleVersion = $statement.CommandElements[$i+1].Value
$currentModule.Add('ModuleVersion', $moduleVersion)
}
}
Expand All @@ -468,14 +482,21 @@ function ConvertTo-DSCObject
foreach ($moduleToLoad in $ModulesToLoad)
{
$loadedModuleTest = Get-Module -Name $moduleToLoad.ModuleName -ListAvailable | Where-Object -FilterScript {$_.Version -eq $moduleToLoad.ModuleVersion}

if ($null -eq $loadedModuleTest -and -not [System.String]::IsNullOrEmpty($moduleToLoad.ModuleVersion))
{
throw "Module {$($moduleToLoad.ModuleName)} version {$($moduleToLoad.ModuleVersion)} specified in the configuration isn't installed on the machine/agent. Install it by running: Install-Module -Name '$($moduleToLoad.ModuleName)' -RequiredVersion '$($moduleToLoad.ModuleVersion)'"
}
else
{
$currentResources = Get-DSCResource -Module $moduleToLoad.ModuleName
if ($Script:IsPowerShellCore)
{
$currentResources = Get-PwshDscResource -Module $moduleToLoad.ModuleName
}
else
{
$currentResources = Get-DSCResource -Module $moduleToLoad.ModuleName
}

if (-not [System.String]::IsNullOrEmpty($moduleToLoad.ModuleVersion))
{
Expand Down Expand Up @@ -661,7 +682,7 @@ function ConvertTo-DSCObject
$currentResourceInfo.Add($key, $value) | Out-Null
}
}

$result += $currentResourceInfo
$totalCount++
}
Expand Down

0 comments on commit 3872aaf

Please sign in to comment.