Powershell DSC:参数无效的复合资源

时间:2014-03-07 12:59:01

标签: powershell

我正在研究DSC复合资源,但我似乎无法让它工作。我的代码:

E:\开发\ DSCResources \ run.ps1:

Import-Module Test -Force

Configuration Run
{
    Import-DscResource -ModuleName Test

    Node localhost 
    {
        Test Common {
            TestString = "blabla"
        }
    }
}

Run

复合资源:

E:\开发\ DSCResources \测试\ test.schema.psm1:

Configuration Test {
    Param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Teststring
    )

    Log Example {
        Message = $Teststring
    }
}

E:\开发\ DSCResources \测试\ test.psd1:

#
# Module manifest for module 'test'
#
# Generated by: Administrator
#
# Generated on: 3/12/2014
#

@{

# Script module or binary module file associated with this manifest.
RootModule = 'test.schema.psm1'

# Version number of this module.
ModuleVersion = '1.0'

# ID used to uniquely identify this module
GUID = 'a0c8ed64-00bc-4112-997f-d65d567bc043'

# Author of this module
Author = 'Administrator'

# Company or vendor of this module
CompanyName = 'Unknown'

# Copyright statement for this module
Copyright = '(c) 2014 Administrator. All rights reserved.'

# Description of the functionality provided by this module
# Description = ''

# Minimum version of the Windows PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module
# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @("")

# Functions to export from this module
#FunctionsToExport = '*'

# Cmdlets to export from this module
#CmdletsToExport = '*'

# Variables to export from this module
#VariablesToExport = '*'

# Aliases to export from this module
#AliasesToExport = '*'

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess
# PrivateData = ''

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

但是没有提供参数,我得到以下输出:

cmdlet Test at command pipeline position 1
Supply values for the following parameters:
Teststring:

因此,似乎复合资源确实有效,但是不会读取传递的参数。所以它挽救了并要求我手动输入参数。

复合资源是否已损坏?或者我做错了什么?

1 个答案:

答案 0 :(得分:1)

更新后,我可以成功重现您的问题,之后我无法重新启动原始配置,让我相信我在原始测试中犯了一些错误。在以下示例中,我将使用BaseConfig作为配置名称,使用MyParameter作为参数名称。

为了使用逐行说明创建更长的描述,我写了一篇关于这个主题的博客文章:http://robertwesterlund.net/post/2014/03/12/creating-a-composite-dsc-configuration-with-parameters

复合配置的参数是普通模块

当复合配置创建为普通模块时(例如,直接在C:\Program Files\WindowsPowerShell\Modules\MyModuleName中),它看起来像普通的Cmdlet。这意味着,要将参数传递给我必须执行的复合配置:

Import-DscResource -ModuleName BaseConfig 

Node localhost 
{ 
    BaseConfig Common -MyParameter "My Parameter Value" 
    { 
    } 
} 

我必须承认,我不知道这是否会像其他部分的DSC资源一样。

支持参数

的复合配置

我想编写配置的方式如下:

Import-DscResource -Name BaseConfig 

Node localhost 
{ 
    BaseConfig Common 
    { 
        MyParameter = "My Parameter Value" 
    } 
} 

(请注意,Import-DscResource也有所不同。)

为了使这个工作,我必须创建一个虚容器模块(当然,如果需要,它可以包含实现,但我只创建了一个空的psm1文件和一个psd1文件指向那个psm1文件。在没有实现容器模块的情况下,然后我为复合配置创建了一个DSC资源模块(位于MyContainerModule\DSCResources\MyCompositeConfiguration文件夹中)。之后它按预期工作。这可能是正确的方法创建复合配置。