PowerShell DSC 复合资源 [英] PowerShell DSC Composite Resources

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

问题描述

我已成功创建 DSC 配置并将它们部署到服务器上.作为下一步,我尝试将服务器中常见的配置块部分分解为可重用的部分,或者用 DSC 的说法是复合资源".经过大量研究,我仍然无法使用复合资源,运行Get-DSCResource"时未列出它们.

I have successfully create DSC Configurations and deployed them onto servers. As a next step, I tried to break apart the parts of the Configuration blocks that are common among servers into reusable sections, or in DSC parlance 'composite resources'. After a lot of researching I am still unable to use the composite resources, they are not listed when running 'Get-DSCResource'.

在查看Get-DSCResource"函数后,它似乎需要一个DSCResources"子文件夹,PowerShell.org 电子书没有说明,但 PowerShell 博客有.Get-DSCResources 函数使用资源名称(示例中的 SimpleConfig)调用 Get-Module.当根文件夹中没有 psd1 时,两者都不考虑 Get-Module 不看到"子文件夹(示例中的 BaseConfiguration).我调整了这些方法,产生了下面的例子,但也没有成功.

After looking through the 'Get-DSCResource' function, it seems to require a 'DSCResources' subfolder which the PowerShell.org ebook doesn't state but the PowerShell Blog does. The Get-DSCResources function calls Get-Module with the resource name (SimpleConfig in the example). Both don't account for Get-Module not 'seeing' the sub folders when there isn't a psd1 in the root folder (BaseConfiguration in the example). I adapted these approaches resulting in the example below, but that has not succeeded either.

我看过:

示例配置:

# $env:ProgramFiles\WindowsPowerShell\BaseConfiguration\BaseConfiguration.psd1
@{
    ModuleVersion = '1.0'
    GUID = '84f449fd-8f26-4bb9-908f-eb675c56b5d8'
    Author = 'James Pogran'
}

# $env:ProgramFiles\WindowsPowerShell\BaseConfiguration\DSCResoures\SimpleConfig\SimpleConfig.schema.psm1
Configuration SimpleConfig
{
    Log LogThis
    {
        Message = "Foo"
    }
}

# $env:ProgramFiles\WindowsPowerShell\BaseConfiguration\DSCResoures\SimpleConfig\SimpleConfig.psd1
RootModule = "SimpleConfig.schema.psm1"

推荐答案

更新 2014-04-12

此答案中的原始说明工作正常,但它显示了创建复合资源的错误方式,这就是为什么需要显式导入模块才能在测试配置中使用复合配置的原因.

Update 2014-04-12

The original instructions in this answers work correctly, but it shows an incorrect way of creating composite resources which is the reason why the module needs to be explicitly imported before being able to use the composite configuration in the test configuration.

复合配置应创建为容器模块的 DSCResources 子文件夹中的普通 DSC 资源.这个 StackOverflow 问题 我还写了一篇关于 创建采用参数的复合 DSC 配置,提供有关正确方法的详细说明.

Composite configurations should be created as ordinary DSC resources in the DSCResources subfolder of a container module. This is mentioned in the answer to this StackOverflow question and I've also written a blog post on creating composite DSC configurations which take parameters, which gives detailed instructions on the correct approach.

我保留了原来的答案,只是有一个删除线,因为我认为它仍然有帮助.此外,鉴于以下信息,它也应该非常有用.

I've kept the original answer, only with a strike-through, since I think it is still helpful. Also, given the below information, it should be pretty usuable as well.

需要调用Import-Module的原因是只有普通的DSC资源才能自动加载.复合配置应在容器模块 MyContainerModule\DSCResources\BaseConfig 的子文件夹中创建.容器模块的 psm1 文件可以为空,复合配置可以与原始答案完全一致(Ìmport-DscResource 命令使用 Namecode> 参数而不是 ModuleName 参数,如果您想在那里保留 BaseConfig 名称).

The reason that the Import-Module call was needed is that only ordinary DSC resources are able to be loaded automatically. The composite configuration should be created in a subfolder of a container module MyContainerModule\DSCResources\BaseConfig. The psm1 file of the container module can be empty and the composite configuration can be exactly as the original answer states (The Ìmport-DscResource command use the Name parameter instead of the ModuleName parameter though, if you want to keep the BaseConfig name there).

<打击>

以下是我在创建和验证名为 BaseConfig 的复合配置时执行的步骤.我希望这有助于为您提供指导.

Below are the steps I performed when I created and verified a composite configuration called BaseConfig. I hope this helps serve as a guide for you.

对于以下所有步骤,我以管理员身份运行 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文件,我输入如下配置:

Into the BaseConfig.schema.psm1 file, I entered the following configuration:

Configuration BaseConfig
{
    File TestFile1
    {
        DestinationPath = "C:\CompositeConfigurationCreatedTextFile1.txt";
        Contents = "File1Content";
    }
    File TestFile2
    {
        DestinationPath = "C:\CompositeConfigurationCreatedTextFile2.txt";
        Contents = "File2Content";
    }
}

创建模块清单

创建模块清单很容易,让 PowerShell 为您完成.

Create the module manifest

Creating the module manifest is easy, just let PowerShell do it for you.

PS C:\Program Files\WindowsPowerShell\Modules\BaseConfig> New-ModuleManifest -Path .\BaseConfig.psd1 -RootModule BaseConfig.schema.psm1

验证 PowerShell 是否找到了复合配置

在这一步之后,我们应该已经完成​​了复合配置.要验证是否可以找到,请尝试运行以下命令并验证输出:

Verify that the composite configuration is found by PowerShell

After this step, we should have the composite configuration finished. To verify that it can be found, try running the following command and verify the output:

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(这两种方式都确保模块未在当前会话中加载),配置将正常工作.因此,出于某种原因,在我至少导入一次模块之前,它似乎不会找到我新创建的复合配置.

Important Note: I found that if I tried to execute the test configuration (below) without importing the module first, it failed; it wasn't able to recognize BaseConfig. After importing the module, the test configuration worked perfectly fine. However, if I later open a new PowerShell session or run Remove-Module in the current PowerShell session (both ways ensures that the module isn't loaded in the current session) the configuration will work just fine. So it seems, for some reason, that it wouldn't find my newly created composite configuration until I had imported the module at least once.

要测试复合配置,请创建一个配置来配置节点 localhost 上的配置,执行它并验证是否已进行预期的更改.我在下面详细说明了执行此操作的确切步骤.

To test the composite configuration, create a configuration which configures the configuration on the node localhost, execute it and verify that the expected changes have been made. I have detailed my exact steps in doing this below.

为了使用复合配置,我首先创建了一个路径为 c:\temp\testconfiguration.ps1 的配置定义文件

To use the composite configuration I first created a configuration definition file with the path 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 文件

Create the mof file(s)

Then, to create the mof-files, just execute the ps1-file

PS C:\temp> .\testconfiguration.ps1

    Directory: C:\temp\TestConfiguration

Mode                LastWriteTime     Length Name                                                                         
----                -------------     ------ ----                                                                         
-a---        2014-03-04     23:49       2266 localhost.mof  

执行 DSC 配置

此后,我让 DSC 使用以下命令执行配置:

Execute the DSC configuration

After this, I let DSC execute the configuration using the following command:

PS C:\temp> Start-DscConfiguration TestConfiguration

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
2      Job2            Configuratio... Running       True            localhost            Start-DscConfiguration...

验证是否进行了预期的更改

最后,我通过验证应该创建的两个文件是否存在来验证复合配置已经运行.

Verify that the expected changes have been made

Finally, I verified that the composite configuration has been run by verifying the existence of the two files which is should create.

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    

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

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