Powershell DSC:参数不起作用的复合资源 [英] Powershell DSC: Composite Resources with parameters not working

查看:34
本文介绍了Powershell DSC:参数不起作用的复合资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 DSC 复合资源,但似乎无法使其正常工作.我的代码:

E:\Dev\DSCResources\run.ps1:

Import-Module Test -Force配置运行{导入-DscResource -ModuleName 测试节点本地主机{测试通用{TestString = "blabla"}}}跑步

和复合资源:

E:\Dev\DSCResources\Test\test.schema.psm1:

配置测试{参数 ([参数(必填)][ValidateNotNullOrEmpty()][字符串]$测试字符串)日志示例 {消息 = $Teststring}}

E:\Dev\DSCResources\Test\test.psd1:

<预><代码>## 模块 'test' 的模块清单## 生成者:管理员## 生成于:3/12/2014#@{# 与此清单关联的脚本模块或二进制模块文件.RootModule = 'test.schema.psm1'# 该模块的版本号.模块版本 = '1.0'# 用于唯一标识此模块的 IDGUID = 'a0c8ed64-00bc-4112-997f-d65d567bc043'# 这个模块的作者作者 = '管理员'# 此模块的公司或供应商公司名称 = '未知'# 本模块版权声明版权所有 = '(c) 2014 管理员.版权所有.'# 本模块提供的功能说明# 说明 = ''# 此模块所需的 Windows PowerShell 引擎的最低版本# PowerShellVersion = ''# 此模块所需的 Windows PowerShell 主机的名称# PowerShellHostName = ''# 此模块所需的 Windows PowerShell 主机的最低版本# PowerShellHostVersion = ''# 此模块所需的 Microsoft .NET Framework 的最低版本# DotNetFrameworkVersion = ''# 此模块所需的公共语言运行时 (CLR) 的最低版本# CLRVersion = ''# 此模块所需的处理器架构(无、X86、Amd64)# 处理器架构 = ''# 导入本模块前必须导入全局环境的模块#RequiredModules = @()# 必须在导入此模块之前加载的程序集# RequiredAssemblies = @()# 在导入此模块之前在调用者的环境中运行的脚本文件 (.ps1).# ScriptsToProcess = @()# 导入这个模块时要加载的类型文件(.ps1xml)# TypesToProcess = @()# 导入这个模块时要加载的格式文件(.ps1xml)# FormatsToProcess = @()# 模块作为 RootModule/ModuleToProcess 中指定模块的嵌套模块导入# NestedModules = @("")# 要从此模块导出的函数#FunctionsToExport = '*'# 要从此模块导出的 Cmdlet#CmdletsToExport = '*'# 要从此模块导出的变量#VariablesToExport = '*'# 从此模块导出的别名#AliasesToExport = '*'# 该模块打包的所有模块列表# 模块列表 = @()# 该模块打包的所有文件列表# 文件列表 = @()# 传递给 RootModule/ModuleToProcess 中指定的模块的私有数据# 私有数据 = ''# 该模块的 HelpInfo URI# HelpInfoURI = ''# 从这个模块导出的命令的默认前缀.使用 Import-Module -Prefix 覆盖默认前缀.# DefaultCommandPrefix = ''}

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

cmdlet 在命令管道位置 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节点本地主机{BaseConfig Common -MyParameter "我的参数值"{}}

我必须承认,我不知道这对于其他部分的 DSC 资源是否会按预期运行.

支持参数的复合配置

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

Import-DscResource -Name BaseConfig节点本地主机{基本配置通用{MyParameter = "我的参数值"}}

(注意 Import-DscResource 也不同.)

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

I'm working on a DSC composite resource, but I can't seem to get it working. My code:

E:\Dev\DSCResources\run.ps1:

Import-Module Test -Force

Configuration Run
{
    Import-DscResource -ModuleName Test

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

Run

And the composite resource:

E:\Dev\DSCResources\Test\test.schema.psm1:

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

    Log Example {
        Message = $Teststring
    }
}

E:\Dev\DSCResources\Test\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 = ''

}

But the parameters don't get supplied, and I get the following output:

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

So it seems like the composite resource does work, but the passed parameters are not read. So it bails out and asks me to manually input the parameters.

Are composite resources broken? Or am I doing something wrong?

解决方案

After your update I could successfully reproduce your problem, after which I couldn't get my original configuration working again, making me believe I made some kind of error in my original test. In the following examples, I will use BaseConfig for config name and MyParameter for parameter name.

In order to create a longer description with line-by-line instructions, I wrote a blog post on the subject: http://robertwesterlund.net/post/2014/03/12/creating-a-composite-dsc-configuration-with-parameters

Parameters to a Composite Configuration which is an ordinary module

When a Composite Configuration is created as an ordinary module (directly in the C:\Program Files\WindowsPowerShell\Modules\MyModuleName, for example), it seems like it behaves like an ordinary Cmdlet. This means, to pass parameters to the composite configuration I had to do:

Import-DscResource -ModuleName BaseConfig 

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

I must admit that I don't know if this would behave as expected for a DSC resource for other parts.

A Composite Configuration which supports parameters

The way I wanted to write the configuration was the following:

Import-DscResource -Name BaseConfig 

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

(Notice that the Import-DscResource also differs.)

To get this working, I had to make a dummy container module (which, of course, could contain implementation if necessary, but I only created an empty psm1 file and a psd1 file pointing to that psm1 file. After making the container module without implementation, I then made a DSC Resource module (located in the MyContainerModule\DSCResources\MyCompositeConfiguration folder) for the composite configuration. After this it worked just as expected. This is probably the correct way to create a composite configuration.

这篇关于Powershell DSC:参数不起作用的复合资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆