about_DesiredStateConfiguration

简短说明

简要介绍 PowerShell Desired State Configuration (DSC) 功能。

详细说明

DSC 是 PowerShell 中的一个管理平台,可用于部署和管理软件服务的配置数据,以及管理运行这些服务的环境。

DSC 提供了一组 PowerShell 语言扩展、新的 cmdlet 和资源,你可以使用这些扩展以声明方式指定要如何配置软件环境的状态。 它还提供了维护和管理现有配置的方法。

DSC 在 PowerShell 4.0 中引入。

有关 DSC 的详细信息,请参阅 TechNet 库中的 PowerShell Desired State Configuration概述

使用类开发 DSC 资源

从 PowerShell 5.0 开始,可以使用类开发 DSC 资源。 有关详细信息,请参阅 microsoft TechNet 上的 about_Classes使用 PowerShell 类编写自定义 DSC 资源

USING DSC

若要使用 DSC 配置环境,请先使用配置关键字 (keyword) 定义 PowerShell 脚本块,后跟标识符,然后是分隔块的一对大括号。 在配置块中,可以定义节点块,这些节点块指定环境中每个节点 (计算机) 所需的配置状态。 节点块以 Node 关键字 (keyword) 开头,后跟目标计算机的名称,可以是变量。 在计算机名称之后,是分隔节点块的大括号。 在节点块中,可以定义资源块来配置特定资源。 资源块以资源的类型名称开头,后跟要为该块指定的标识符,后跟分隔块的大括号,如以下示例所示。

Configuration MyWebConfig {
    # Parameters are optional
    param ($MachineName, $WebsiteFilePath)
    # A Configuration block can have one or more Node blocks
    Node $MachineName
    {
        # Next, specify one or more resource blocks
        # WindowsFeature is one of the resources you can use in a Node block
        # This example ensures the Web Server (IIS) role is installed
        WindowsFeature IIS
        {
            # To ensure that the role is not installed, set Ensure to "Absent"
            Ensure = "Present"
            Name = "Web-Server" # Use the Name property from Get-WindowsFeature
        }

        # You can use the File resource to create files and folders
        # "WebDirectory" is the name you want to use to refer to this instance
        File WebDirectory
        {
            Ensure = "Present"  # You can also set Ensure to "Absent"
            Type = "Directory" # Default is "File"
            Recurse = $true
            SourcePath = $WebsiteFilePath
            DestinationPath = "C:\inetpub\wwwroot"

            # Ensure that the IIS block is successfully run first before
            # configuring this resource
            DependsOn = "[WindowsFeature]IIS"  # Use for dependencies
        }
    }
}

若要创建配置,请以调用 PowerShell 函数的方式调用 Configuration 块,传入上例 (两个) 定义的任何预期参数。 例如,在本例中:

MyWebConfig -MachineName "TestMachine" -WebsiteFilePath `
  "\\filesrv\WebFiles" -OutputPath "C:\Windows\system32\temp"
# OutputPath is optional

这会在指定的路径上为每个节点生成一个 MOF 文件。 这些 MOF 文件指定每个节点的所需配置。 接下来,使用以下 cmdlet 分析配置 MOF 文件,向每个节点发送相应的配置,并执行这些配置。 请注意,无需为基于类的 DSC 资源创建单独的 MOF 文件。

Start-DscConfiguration -Verbose -Wait -Path "C:\Windows\system32\temp"

使用 DSC 维护配置状态

使用 DSC 时,配置是幂等的。 这意味着,如果使用 DSC 多次执行相同的配置,则生成的配置状态将始终相同。 因此,如果怀疑环境中的任何节点可能偏离了所需的配置状态,则可以再次执行相同的 DSC 配置,使其恢复到所需状态。 无需修改配置脚本即可仅处理其状态偏离所需状态的资源。

以下示例演示如何验证给定节点上的配置的实际状态是否偏离了节点上执行的最后一个 DSC 配置。 在此示例中,我们将检查本地计算机的配置。

$session = New-CimSession -ComputerName "localhost"
Test-DscConfiguration -CimSession $session

内置 DSC 资源

可以在配置脚本中使用以下内置资源:

名称 属性
文件 {DestinationPath, Attributes, Checksum, Content...}
存档 {Destination, Path, Checksum, Credential...}
环境 {Name, DependsOn, Ensure, Path...}
分组 {GroupName, Credential, DependsOn, Description...}
日志 {Message, DependsOn, PsDscRunAsCredential}
{Name, Path, ProductId, Arguments...}
注册表 {Key、ValueName、DependsOn、Ensure...}
脚本 {GetScript、SetScript、TestScript、Credential...}
服务 {Name, BuiltInAccount, Credential, Dependencies...}
用户 {UserName, DependsOn, Description, Disabled...}
WaitForAll {NodeName, ResourceName, DependsOn, PsDscRunAsC...}
WaitForAny {NodeName, ResourceName, DependsOn, PsDscRunAsC...}
WaitForSome {NodeCount, NodeName, ResourceName, DependsOn...}
WindowsFeature {Name、Credential、DependsOn、Ensure...}
WindowsOptionalFeature {Name、DependsOn、Ensure、LogLevel...}
WindowsProcess {Arguments, Path, Credential, DependsOn...}

若要获取系统上可用 DSC 资源的列表,请运行 Get-DscResource cmdlet。

本主题中的示例演示如何使用 File 和 WindowsFeature 资源。 若要查看可与资源一起使用的所有属性,请将光标插入资源关键字 (keyword) (例如 PowerShell ISE 配置脚本中的“文件) ”,按住 CTRL,然后按空格键。

查找更多资源

可以下载、安装和了解 PowerShell 和 DSC 用户社区以及 Microsoft 创建的许多其他可用 DSC 资源。 访问PowerShell 库浏览和了解可用的 DSC 资源。

另请参阅

PowerShell Desired State Configuration 概述

内置 PowerShell Desired State Configuration资源

生成自定义 PowerShell Desired State Configuration资源