Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PowerShell 7 support with Microsoft365DSC #50

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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