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

问题描述:

我在创建自定义DSC复合资源并将其上载到Azure自动化模块列表中时遇到问题,我希望有人能够阐明一些事情。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" 
    } 
} 

现在,如果我已经通过执行以下代码创建了一个基本的PowerShell DSC复合资源我将C:\ Program Files \ WindowsPowerShell \ Modules \ CompositeExample文件夹压缩为CompositeExample_1.0.zip,并将其上传到“经典”DSC服务器,并在单独的配置中引用它,它工作得很好。

不过,如果我添加它作为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. 

DO模块需要在Azure中以不同的方式被“捆绑”,或需要额外的文件?

好的,问题是我在创建根模块清单时指定-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的自动化则可以让这个被添加为没有任何错误的模块。