我遇到了一个问题,即在通过 Azure Pipelines 运行 Packer 作业期间无法导入 PowerShell DSC 资源。
Packer 指出一个错误:
==> 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:
这一配置的设置脚本首先确保安装了模块。我从他们那里删除了 import 语句,因为其他帖子表明它可能会导致某种类型的冲突。
$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"
}
}
安装完成后,我在同一个 ps1 中运行 DSC 配置的调用。但是,尽管进行了许多不同的测试,但我无法让它识别已安装的 DSC 资源。
我怀疑 Windows 2016 中内置资源的某些东西会导致冲突,但我不是 DSC 专家,还不知道该怎么做。
失败的配置是:
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
}
请注意,对于我尝试转换的其他 DSC 安装(而不是我自己开发的脚本),这对我来说是一个问题,并且所有这些安装都一直存在这个问题。
此外,考虑到我正在测试的 Windows Server 2016 映像没有最新的 nuget 和 PowerShellGet,我确实确保现在是最新版本:
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
任何指导将不胜感激!