如何应用多个 DSC 配置?

How do you apply multiple DSC configurations?

这是我的例子:

$Config = @{
    AllNodes = @(
        @{ NodeName = 'localhost'; PSDscAllowPlainTextPassword = $True }
    )
}

Configuration LocalAdmin
{
    Param([String[]]$Node='localhost',[PSCredential]$Cred)

    Import-DscResource -ModuleName 'PSDscResources'
    Node $Node
    {
        User 'LocalAdmin'
        {
            Username = 'Admin'
            Description = 'DSC configuration test'
            Ensure = 'Present'
            FullName = 'Administrator Extraordinaire'
            Password = $Cred
            PasswordChangeRequired = $False
            PasswordNeverExpires = $True
        }
        Group 'AddToAdmin'
        {
            GroupName = 'Administrators'
            DependsOn = '[User]LocalAdmin'
            Ensure = 'Present'
            MembersToInclude = 'Admin'
        }
    }
}
Configuration DisableLocalAccounts
{
    Param([String[]]$Node='localhost')

    Import-DscResource -ModuleName 'PSDscResources'
    Node $Node
    {
        User 'Administrator'
        {
            Username = 'Administrator'
            Disabled = $True
        }
        User 'Guest'
        {
            Username = 'Guest'
            Disabled = $True
        }
        User 'DefaultAccount'
        {
            Username = 'DefaultAccount'
            Disabled = $True
        }
    }
}


Set-Location $env:UserProfile
LocalAdmin -Cred (Get-Credential -UserName 'Admin') -ConfigurationData $Config
DisableLocalAccounts

Start-DscConfiguration -ComputerName 'localhost' -Wait -Force -Verbose -Path '.\LocalAdmin'
Start-DscConfiguration -ComputerName 'localhost' -Wait -Force -Verbose -Path '.\DisableLocalAccounts'

问题:
当我运行Get-DscConfiguration时,它只显示我运行最后一个配置的配置。

PS C:\> Get-DscConfiguration


ConfigurationName        : DisableLocalAccounts
DependsOn                :
ModuleName               : PSDscResources
ModuleVersion            : 2.8.0.0
PsDscRunAsCredential     :
ResourceId               : [User]Administrator
SourceInfo               :
Description              : Built-in account for administering the computer/domain
Disabled                 : True
Ensure                   : Present
FullName                 :
Password                 :
PasswordChangeNotAllowed : False
PasswordChangeRequired   :
PasswordNeverExpires     : True
UserName                 : Administrator
PSComputerName           :
CimClassName             : MSFT_UserResource

ConfigurationName        : DisableLocalAccounts
DependsOn                :
ModuleName               : PSDscResources
ModuleVersion            : 2.8.0.0
PsDscRunAsCredential     :
ResourceId               : [User]Guest
SourceInfo               :
Description              : Built-in account for guest access to the computer/domain
Disabled                 : True
Ensure                   : Present
FullName                 :
Password                 :
PasswordChangeNotAllowed : True
PasswordChangeRequired   :
PasswordNeverExpires     : True
UserName                 : Guest
PSComputerName           :
CimClassName             : MSFT_UserResource

ConfigurationName        : DisableLocalAccounts
DependsOn                :
ModuleName               : PSDscResources
ModuleVersion            : 2.8.0.0
PsDscRunAsCredential     :
ResourceId               : [User]DefaultAccount
SourceInfo               :
Description              : A user account managed by the system.
Disabled                 : True
Ensure                   : Present
FullName                 :
Password                 :
PasswordChangeNotAllowed : False
PasswordChangeRequired   :
PasswordNeverExpires     : True
UserName                 : DefaultAccount
PSComputerName           :
CimClassName             : MSFT_UserResource

如何应用多个配置?我找不到这方面的文档。

您不会在文档中找到它,因为您(基本上)不能那样做。

我说基本上,因为从某种意义上说,您可以使用 DSC Partial Configurations

不过,这些需要不同的工作流程和不同的本地配置管理器 (LCM) 设置。它们不会像您设想的那样工作,您可以在其中创建多个配置,然后一个接一个地应用它们。

这是设计使然;你想要做的并不是 DSC 的真正用途。这个想法是你应该提供你正在配置的节点的(期望的)状态。应用多个配置很容易导致应用冲突的设置。

即使使用分音,LCM 也会生成一个配置(解析您的分音),然后一次性应用所有配置。


改为做什么:

DSC 对工具的要求很低。关于您最终如何生成配置或处理公共数据、角色等,它并没有太多可说的。所以您已经必须自己动手做大部分。

应用多个单独的配置可能是您在自己的工作流程中应该注意的事情,最终会导致每个节点编译您的(单个)MOF。

什么是偏音?

我能想到有 2 个用例适合使用 Partials。

首先(这主要是 Microsoft 考虑的他们的角色)是针对更大和更隔离的组织,其中不同的团队对他们的知识领域负有唯一的责任和所有权,并且您希望这些团队能够编写和控制自己的配置。

因此,例如,OS 团队可能会编写各种基本 OS 配置项(设置时间 zone/NTP、许可证设置)的配置,并且他们可能会设置 LCM 设置从其余部分拉出。

DBA 团队编写了用于安装和配置 SQL 服务器的配置。

安全团队编写了用于设置密码策略、防火墙规则和强制执行等的配置

这些团队有自己的程序和规则以及自主权。他们可能有自己的拉取服务器来发布这些内容。

第二个用例,通常与第一个用例相关,是当您有多个拉取服务器,或者您想要组合推和拉取时。我相信这只有使用部分才有可能。


未来

请注意,Windows PowerShell 不太可能再更新。 PowerShell Core(基于 .Net Core 并在 Windows、Linux 和 MacOS 上运行)是目前 PowerShell 开发的主要方向。

这样,DSC will be changing too and getting a completely new edition going forward that works better cross-platform

如果您要编写大量工具和工作流代码来支持 DSC,请牢记这一点。

您不必总是使用部分配置来完成您想要完成的任务。您可以使用复合配置来实现相同的效果,具体取决于您如何使用这些配置。这是使用复合配置的示例的翻译。

    $Config = @{
    AllNodes = @(
        @{ NodeName = 'localhost'; PSDscAllowPlainTextPassword = $True }
    )
}
Configuration LocalAdmin
{
    Param([String[]]$Node='localhost',[PSCredential]$Cred)

    Import-DscResource -ModuleName 'PSDscResources'
    Node $Node
    {
        User 'LocalAdmin'
        {
            Username = 'Admin'
            Description = 'DSC configuration test'
            Ensure = 'Present'
            FullName = 'Administrator Extraordinaire'
            Password = $Cred
            PasswordChangeRequired = $False
            PasswordNeverExpires = $True
        }
        Group 'AddToAdmin'
        {
            GroupName = 'Administrators'
            DependsOn = '[User]LocalAdmin'
            Ensure = 'Present'
            MembersToInclude = 'Admin'
        }
    }
}
Configuration DisableLocalAccounts
{
    Param([String[]]$Node='localhost')

    Import-DscResource -ModuleName 'PSDscResources'
    Node $Node
    {
        User 'Administrator'
        {
            Username = 'Administrator'
            Disabled = $True
        }
        User 'Guest'
        {
            Username = 'Guest'
            Disabled = $True
        }
        User 'DefaultAccount'
        {
            Username = 'DefaultAccount'
            Disabled = $True
        }
    }
}
Configuration AllAccounts
{
    Param([String[]]$Node='localhost',[PSCredential]$Cred)
    DisableLocalAccounts localAccount
    {
       Node = $Node
    }
    LocalAdmin localAdmin
    {
        Node = $Node
        Cred = $Cred
    }
}
Set-Location $env:UserProfile
AllAccounts -Cred (Get-Credential -UserName 'Admin') -ConfigurationData $Config
Start-DscConfiguration -ComputerName 'localhost' -Wait -Force -Verbose -Path '.\AllAccounts'