简体   繁体   中英

Custom PowerShell DSC Composite Resource in Azure Automation

I'm having problems creating a custom DSC composite resource and getting it uploaded into a Azure Automation module list that I was hoping someone could shed some light on. I've created a basic PowerShell DSC composite resource by executing the following code:

$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 ''

And then in the CompositeResource.schema.psm1 file I've added the following code:

Configuration CompositeResource
{
    Import-DscResource -ModuleName PSDesiredStateConfiguration

    File ExampleFolder
    {
        DestinationPath = "C:\Example"
        Type            = "Directory"
    }
}

Now if I zip up the C:\\Program Files\\WindowsPowerShell\\Modules\\CompositeExample folder as CompositeExample_1.0.zip and upload it to a 'classic' DSC server and reference it in a separate configuration it works perfectly fine.

However, if I add it as a module in Azure Automation (as CompositeModule.zip) I get the following error:

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 modules need to be 'bundled' in a different way for Azure, or require additional files?

Ok, the problem was me specifying the -RootModule line when creating the root module manifest. 'Classic' DSC just seems to ignore it, whereas Azure Automation throws an error as the file doesn't exist. So the code to create the composite module would be

$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 then allows this to be added as a module without any errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM