2014 年 4 月 12 日更新
此答案中的原始说明可以正常工作,但它显示了创建复合资源的错误方式,这就是为什么需要显式导入模块才能在测试配置中使用复合配置的原因。
复合配置应作为普通 DSC 资源创建在容器模块的 DSCResources 子文件夹中。这在this StackOverflow question的回答中提到了,我还在creating composite DSC configurations which take parameters, which gives detailed instructions on the correct approach上写了一篇博文。
我保留了原来的答案,只是加上了一个删除线,因为我认为它仍然很有帮助。此外,鉴于以下信息,它也应该非常有用。
需要 Import-Module 调用的原因是只有普通的 DSC 资源能够自动加载。复合配置应在容器模块MyContainerModule\DSCResources\BaseConfig 的子文件夹中创建。容器模块的psm1 文件可以为空,复合配置可以与原始答案状态完全相同(Ìmport-DscResource 命令使用Name 参数而不是ModuleName 参数,如果你想保留那里的 BaseConfig 名称)。
罢工>
创建复合 DSC 配置的详细步骤
以下是我在创建和验证名为BaseConfig 的复合配置时执行的步骤。我希望这有助于为您提供指导。
对于以下所有步骤,我让 PowerShell ISE 以管理员身份运行。
创建存放模块的文件夹
PS C:\WINDOWS\system32> cd 'C:\Program Files\WindowsPowerShell\Modules'
PS C:\Program Files\WindowsPowerShell\Modules> md BaseConfig
Directory: C:\Program Files\WindowsPowerShell\Modules
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2014-03-04 23:45 BaseConfig
PS C:\Program Files\WindowsPowerShell\Modules> cd .\BaseConfig
PS C:\Program Files\WindowsPowerShell\Modules\BaseConfig>
创建复合配置文件
此文件的命名必须以“.schema.psm1”结尾,因为这是硬编码到 PowerShell DSC 模块中的,您可以在文件夹 C:\windows\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration 中找到其代码。
PS C:\Program Files\WindowsPowerShell\Modules\BaseConfig> notepad BaseConfig.schema.psm1
进入BaseConfig.schema.psm1文件,我输入如下配置:
Configuration BaseConfig
{
File TestFile1
{
DestinationPath = "C:\CompositeConfigurationCreatedTextFile1.txt";
Contents = "File1Content";
}
File TestFile2
{
DestinationPath = "C:\CompositeConfigurationCreatedTextFile2.txt";
Contents = "File2Content";
}
}
创建模块清单
创建模块清单很容易,只需让 PowerShell 为您完成。
PS C:\Program Files\WindowsPowerShell\Modules\BaseConfig> New-ModuleManifest -Path .\BaseConfig.psd1 -RootModule BaseConfig.schema.psm1
验证 PowerShell 是否找到复合配置
在这一步之后,我们应该完成了复合配置。要验证是否可以找到它,请尝试运行以下命令并验证输出:
PS C:\Program Files\WindowsPowerShell\Modules\BaseConfig> Import-Module BaseConfig
PS C:\Program Files\WindowsPowerShell\Modules\BaseConfig> Get-Command -CommandType Configuration
CommandType Name ModuleName
----------- ---- ----------
Configuration BaseConfig BaseConfig
重要提示:我发现如果我在没有先导入模块的情况下尝试执行测试配置(下),它会失败;它无法识别 BaseConfig。导入模块后,测试配置运行良好。但是,如果我稍后打开一个新的 PowerShell 会话或在当前 PowerShell 会话中运行 Remove-Module(两种方式都可以确保模块未在当前会话中加载),则配置将正常工作。因此,出于某种原因,它似乎在我至少导入了一次模块之前找不到我新创建的复合配置。
测试复合配置
要测试复合配置,请在节点 localhost 上创建配置配置,执行它并验证是否已进行预期的更改。我在下面详细说明了执行此操作的具体步骤。
创建测试配置
为了使用复合配置,我首先创建了一个配置定义文件,路径为 c:\temp\testconfiguration.ps1
PS C:\Program Files\WindowsPowerShell\Modules\BaseConfig> md c:\Temp
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 2014-03-04 23:47 Temp
PS C:\Program Files\WindowsPowerShell\Modules\BaseConfig> cd \temp
PS C:\temp> notepad testconfiguration.ps1
我给了它以下内容:
Configuration TestConfiguration
{
Import-DscResource -ModuleName BaseConfig
node localhost
{
BaseConfig Common
{
# The created configuration did not have any parameters, thus no properties
}
}
}
TestConfiguration
创建 mof 文件
然后,要创建 mof 文件,只需执行 ps1 文件
PS C:\temp> .\testconfiguration.ps1
Directory: C:\temp\TestConfiguration
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014-03-04 23:49 2266 localhost.mof
执行 DSC 配置
之后,我让 DSC 使用以下命令执行配置:
PS C:\temp> Start-DscConfiguration TestConfiguration
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
2 Job2 Configuratio... Running True localhost Start-DscConfiguration...
验证是否已进行预期的更改
最后,我通过验证应该创建的两个文件的存在来验证复合配置已经运行。
PS C:\temp> ls c:\ -Filter *.txt
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2014-03-04 23:51 15 CompositeConfigurationCreatedTextFile1.txt
-a--- 2014-03-04 23:51 15 CompositeConfigurationCreatedTextFile2.txt