简短说明
提供了 PowerShell Desired State Configuration (DSC) 功能的简要介绍。
长说明
DSC 是 PowerShell 中的一个管理平台,可用于部署和管理软件服务的配置数据,并管理这些服务的运行环境。
DSC 提供了一组 PowerShell 语言扩展、新的 cmdlet 和资源,你可以使用它们以声明方式指定你希望如何配置软件环境的状态。 它还提供了维护和管理现有配置的方法。
DSC 是在 PowerShell 4.0 中引入的。
有关 DSC 的详细信息,请参阅 PowerShell Desired State Configuration 概述。
使用类开发 DSC 资源
从 PowerShell 5.0 开始,你可以使用类开发 DSC 资源。 有关详细信息,请参阅 about_Classes 以及使用 PowerShell 类编写自定义 DSC 资源。
使用 DSC
若要使用 DSC 来配置环境,请先使用 Configuration 关键字(后跟一个标识符)来定义一个 PowerShell 脚本块,然后使用一对大括号来为该脚本块定界。 在配置块内,你可以定义节点块,这些块指定环境中每个节点(计算机)的所需配置状态。 节点块以 Node 关键字开头,后跟目标计算机的名称,计算机名称可以是变量。 计算机名称后是用于界定节点块的大括号。 在节点块内,你可以定义资源块来配置特定资源。 资源块以资源的类型名称开头,后跟你想要为该块指定的标识符,然后是用来界定该块的大括号,如以下示例所示。
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。
注意
在低于 7.0 的 PowerShell 版本中,Get-DscResource
找不到基于类的 DSC 资源。
本主题中的示例演示了如何使用 File 和 WindowsFeature 资源。 若要查看可用于资源的所有属性,请在 PowerShell ISE 中在配置脚本中的资源关键字(例如 File)中插入光标,然后按下 CTRL+空格键
查找更多资源
你可以下载、安装和了解 PowerShell 和 DSC 用户社区以及 Microsoft 创建的许多其他可用 DSC 资源。 访问 PowerShell 库来浏览和了解可用的 DSC 资源。