I'm having an issue where PowerShell DSC resources are failing to import during a Packer job being run through Azure Pipelines.
Packer indicates an error saying:
==> amazon-ebs: Provisioning with Powershell...
==> amazon-ebs: Provisioning with powershell script: ./scripts/dsc-windows-powershell-policy.ps1
==> amazon-ebs: At C:\Windows\Temp\script-5e6ad1c0-dea6-f683-86ea-f173e577e85d.ps1:24 char:5
==> amazon-ebs: + Import-DscResource -ModuleName ComputerManagementDsc # -ModuleVer ...
==> amazon-ebs: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
==> amazon-ebs: Could not find the module 'ComputerManagementDsc'.
==> amazon-ebs: At C:\Windows\Temp\script-5e6ad1c0-dea6-f683-86ea-f173e577e85d.ps1:28 char:9
==> amazon-ebs: + PowerShellExecutionPolicy ExecutionPolicy
==> amazon-ebs: + ~~~~~~~~~~~~~~~~~~~~~~~~~
==> amazon-ebs: Undefined DSC resource 'PowerShellExecutionPolicy'. Use Import-DSCResource to import the resource.
==> amazon-ebs: + CategoryInfo : ParserError: (:) [], ParseException
==> amazon-ebs: + FullyQualifiedErrorId : ModuleNotFoundDuringParse
==> amazon-ebs:
The setup script for this one configuration starts with ensuring the modules are installed. I removed the import statement from them due to other posts indicating it might perhaps cause some type of conflict.
$ErrorActionPreference = 'Stop'
@(
'PSDscResources'
'ComputerManagementDsc'
) | ForEach-Object {
$m = $_
if(-not (Get-InstalledModule $m))
{
Write-Host "Installing Module: $m"
Find-Module -Name $m -Repository PSGallery | Install-Module -Scope AllUsers -Force -AllowClobber
}
else
{
Write-Host "Bypassed install of $m per already installed"
}
}
Once this install is finished, I run in the same ps1 the invocation of DSC configuration. However, despite many different tests I can't get it to recognize the installed DSC Resource.
I'm suspicious that something with the built in resources in Windows 2016 is causing a conflict, but I'm not an expert in DSC yet to know what to do about it.
The configuration that is failing is:
Configuration DSC_PowerShellExecutionPolicy_config
{
Import-DscResource -ModuleName ComputerManagementDsc -ModuleVersion 8.0.0
node 'localhost'
{
PowerShellExecutionPolicy ExecutionPolicy
{
ExecutionPolicy = 'RemoteSigned'
ExecutionPolicyScope = 'LocalMachine'
}
}
}
try
{
Write-Host "⚙ Initiating DSC_PowerShellExecutionPolicy_config"
DSC_PowerShellExecutionPolicy_config
Start-DscConfiguration DSC_PowerShellExecutionPolicy_config -Verbose -force -Wait
}
catch
{
throw
exit 1
}
Note, this is an issue for me with other DSC installs I'm trying to convert to (instead of my homegrown scripts), and all of them keep having this issue.
Also, considering the Windows Server 2016 image I'm testing on doesn't have the latest nuget and PowerShellGet, I do ensure the latest version as of now:
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module PowerShellGet -MinimumVersion 2.2.3 -Force -Scope AllUsers -AllowClobber
Import-Module PowershellGet -MinimumVersion 2.2.3 -Force -verbose
Any guidance would be appreciated!
The problem you're seeing is that DSC configs are parsed before any code in the file is run, even before they are loaded into memory. If the modules aren't there before the config script is run then it'll fail. You'll be better off installing those modules before you run the config, probably in a separate Packer step.