Azure 自动化中的自定义 PowerShell DSC 复合资源



我在创建自定义 DSC 复合资源并将其上传到 Azure 自动化模块列表中时遇到问题,我希望有人可以对此有所了解。我通过执行以下代码创建了一个基本的 PowerShell DSC 复合资源:

$parentModulePath = 'C:Program FilesWindowsPowerShellModulesCompositeExample'
mkdir $parentModulePath
New-ModuleManifest  -RootModule CompositeExample –Path "$parentModulePathCompositeExample.psd1"
$resourceModulePath = "$parentModulePathDSCResourcesCompositeResource"
mkdir $resourceModulePath
New-ModuleManifest  -RootModule 'CompositeResource.schema.psm1' –Path "$resourceModulePathCompositeResource.psd1"
Add-Content –Path "$resourceModulePathCompositeResource.schema.psm1" –Value ''

然后在 CompositeResource.schema.psm1 文件中,我添加了以下代码:

Configuration CompositeResource
{
    Import-DscResource -ModuleName PSDesiredStateConfiguration
    File ExampleFolder
    {
        DestinationPath = "C:Example"
        Type            = "Directory"
    }
}

现在,如果我将 C:\Program Files\WindowsPowerShell\Modules\CompositeExample 文件夹压缩为 CompositeExample_1.0.zip并将其上传到"经典"DSC 服务器并在单独的配置中引用它,它工作得很好。

但是,如果我将其添加为 Azure 自动化中的模块(作为复合模块.zip),则会收到以下错误:

Error importing the module CompositeExample. Import failed with the following error:
Orchestrator.Shared.AsyncModuleImport.ModuleImportException: An error occurred during
module validation. When importing the module to an internal PowerShell session, it was not
able to be loaded by PowerShell. There is likely an issue with the contents of the module
that results in PowerShell's not being able to load it. Please verify that the module
imports successfully in a local PowerShell session, correct any issues, and then try
importing again.

对于 Azure,模块是否需要以不同的方式"捆绑",或者需要其他文件?

好的,问题是我在创建根模块清单时指定了 -RootModule 行。"经典"DSC 似乎只是忽略它,而 Azure 自动化会引发错误,因为该文件不存在。因此,创建复合模块的代码将是

$parentModulePath = 'C:Program FilesWindowsPowerShellModulesCompositeExample'
mkdir $parentModulePath
New-ModuleManifest –Path "$parentModulePathCompositeExample.psd1"
$resourceModulePath = "$parentModulePathDSCResourcesCompositeResource"
mkdir $resourceModulePath
New-ModuleManifest  -RootModule 'CompositeResource.schema.psm1' –Path "$resourceModulePathCompositeResource.psd1"
Add-Content –Path "$resourceModulePathCompositeResource.schema.psm1" –Value ''

然后,Azure 自动化允许将其添加为模块,而不会出现任何错误。

最新更新