2014-03-07 92 views
2

我正在使用DSC複合資源,但似乎無法使其工作。我的代碼: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: 

所以它看起來像複合資源的工作,但傳遞的參數不讀。所以它保釋出來並要求我手動輸入參數。

合成資源是否被破壞?或者我做錯了什麼?

+0

我無法在第一次嘗試時重現您的問題。它成功處理參數。你可以編輯你的答案,以確保你包含了三個文件(run.ps1,test.schema.psm1和test.psd1)的_exact_內容,以及它們的確切位置,我會看看我是否可以在本地重現你的問題,幫助你解決它。 –

回答

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文件和一個指向該psm1文件的psd1文件即在沒有實現容器模塊之後,我爲複合配置製作了DSC資源模塊(位於MyContainerModule\DSCResources\MyCompositeConfiguration文件夾中)。在此之後,它就像預期的那樣工作。這可能是創建組合配置的正確方法。

+0

嗨羅伯特,這確實有效。請注意,「Import-DSCResource -Name」內存密集。所以我使用「Import-DSCResource -ModuleName MyDSCResources」,它導入包含底層組合資源的完整虛擬模塊。這可以更好,更快速地工作:) –

+0

如果您使用新資源創建一個全新的模塊,它是否會立即生效?我發現如果我創建了複合DSC資源作爲它自己的模塊(不在DSCResources文件夾中),我必須手動執行'Import-Module' _once_,然後才能爲該模塊執行'Import-DscResource -ModuleName mymodule' 。當資源位於DscResources文件夾並使用'-ModuleName'參數時,我必須嘗試如何處理它。你試過了嗎? –

+0

1.空psm1是多餘的。 2.確保DscResources中psd1中的FunctionsToExport ='*'。 3.如果您不想調用Import-Module,請將目錄添加到$ env:PSModulePath。 –