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

时间:2017-10-20 12:03:47

标签: powershell azure composite dsc

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

$parentModulePath = 'C:\Program Files\WindowsPowerShell\Modules\CompositeExample'
mkdir $parentModulePath
New-ModuleManifest  -RootModule CompositeExample –Path "$parentModulePath\CompositeExample.psd1"

$resourceModulePath = "$parentModulePath\DSCResources\CompositeResource"
mkdir $resourceModulePath
New-ModuleManifest  -RootModule 'CompositeResource.schema.psm1' –Path "$resourceModulePath\CompositeResource.psd1"
Add-Content –Path "$resourceModulePath\CompositeResource.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服务器并在单独的配置中引用它,它可以正常工作。 / p>

但是,如果我将其作为模块添加到Azure自动化(作为CompositeModule.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,或者需要其他文件?

1 个答案:

答案 0 :(得分:0)

好的,问题是我在创建根模块清单时指定了-RootModule行。 “Classic”DSC似乎忽略了它,而Azure Automation因文件不存在而抛出错误。因此,创建复合模块的代码将是

$parentModulePath = 'C:\Program Files\WindowsPowerShell\Modules\CompositeExample'
mkdir $parentModulePath
New-ModuleManifest –Path "$parentModulePath\CompositeExample.psd1"

$resourceModulePath = "$parentModulePath\DSCResources\CompositeResource"
mkdir $resourceModulePath
New-ModuleManifest  -RootModule 'CompositeResource.schema.psm1' –Path "$resourceModulePath\CompositeResource.psd1"
Add-Content –Path "$resourceModulePath\CompositeResource.schema.psm1" –Value ''

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