适用于:Windows PowerShell 4.0、Windows PowerShell 5.0
资源设计器工具是 xDscResourceDesigner 模块公开的一组 cmdlet,用于更轻松地创建 Windows PowerShell 所需状态配置 (DSC) 资源。 此资源中的 cmdlet 有助于为新资源创建 MOF 架构、脚本模块和目录结构。 有关 DSC 资源的详细信息,请参阅 生成自定义 Windows PowerShell 所需状态配置资源。 在本文中,我们将创建一个管理 Active Directory 用户的 DSC 资源。 使用 Install-Module cmdlet 安装 xDscResourceDesigner 模块。
创建资源属性
我们需要做的第一件事是确定资源将公开的属性。 对于此示例,我们将定义具有以下属性的 Active Directory 用户。
参数名称 说明
- UserName:唯一标识用户的键属性。
- 确保:指定用户帐户应为“存在”还是“不存在”。 此参数将只有两个可能的值。
- DomainCredential:用户的域密码。
- 密码:用户所需的密码,以允许配置在必要时更改用户密码。
若要创建属性,我们使用 New-xDscResourceProperty cmdlet。 以下 PowerShell 命令创建上述属性。
$UserName = New-xDscResourceProperty –Name UserName -Type String -Attribute Key
$Ensure = New-xDscResourceProperty –Name Ensure -Type String -Attribute Write –ValidateSet "Present", "Absent"
$DomainCredential = New-xDscResourceProperty –Name DomainCredential -Type PSCredential -Attribute Write
$Password = New-xDscResourceProperty –Name Password -Type PSCredential -Attribute Write
创建资源
创建资源属性后,我们可以调用 New-xDscResource cmdlet 来创建资源。 cmdlet 将 New-xDscResource 属性列表作为参数。 它还采用应创建模块的路径、新资源的名称以及包含该模块的模块的名称。 以下 PowerShell 命令创建资源。
New-xDscResource –Name Demo_ADUser –Property $UserName, $Ensure, $DomainCredential, $Password –Path 'C:\Program Files\WindowsPowerShell\Modules' –ModuleName Demo_DSCModule
该 New-xDscResource cmdlet 创建 MOF 架构、骨架资源脚本、新资源所需的目录结构,以及公开新资源的模块的清单。
MOF架构文件位于 C:\Program Files\WindowsPowerShell\Modules\Demo_DSCModule\DSCResources\Demo_ADUser\Demo_ADUser.schema.mof,其内容如下。
[ClassVersion("1.0.0.0"), FriendlyName("Demo_ADUser")]
class Demo_ADUser : OMI_BaseResource
{
[Key] string UserName;
[Write, ValueMap{"Present","Absent"}, Values{"Present","Absent"}] string Ensure;
[Write, EmbeddedInstance("MSFT_Credential")] String DomainCredential;
[Write, EmbeddedInstance("MSFT_Credential")] String Password;
};
资源脚本位于 C:\Program Files\WindowsPowerShell\Modules\Demo_DSCModule\DSCResources\Demo_ADUser\Demo_ADUser.psm1。
它不包括实现资源的实际逻辑,您必须自己添加。 骨架脚本的内容如下。
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$UserName
)
#Write-Verbose "Use this cmdlet to deliver information about command processing."
#Write-Debug "Use this cmdlet to write debug information while troubleshooting."
<#
$returnValue = @{
UserName = [System.String]
Ensure = [System.String]
DomainAdminCredential = [System.Management.Automation.PSCredential]
Password = [System.Management.Automation.PSCredential]
}
$returnValue
#>
}
function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.String]
$UserName,
[ValidateSet("Present","Absent")]
[System.String]
$Ensure,
[System.Management.Automation.PSCredential]
$DomainAdminCredential,
[System.Management.Automation.PSCredential]
$Password
)
#Write-Verbose "Use this cmdlet to deliver information about command processing."
#Write-Debug "Use this cmdlet to write debug information while troubleshooting."
#Include this line if the resource requires a system reboot.
#$global:DSCMachineStatus = 1
}
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$UserName,
[ValidateSet("Present","Absent")]
[System.String]
$Ensure,
[System.Management.Automation.PSCredential]
$DomainAdminCredential,
[System.Management.Automation.PSCredential]
$Password
)
#Write-Verbose "Use this cmdlet to deliver information about command processing."
#Write-Debug "Use this cmdlet to write debug information while troubleshooting."
<#
$result = [System.Boolean]
$result
#>
}
Export-ModuleMember -Function *-TargetResource
更新资源
如果需要添加或修改资源的参数列表,可以调用 Update-xDscResource cmdlet。 cmdlet 使用新的参数列表更新资源。 如果已在资源脚本中添加了逻辑,则该逻辑将保持不变。
例如,假设您想在资源中包含用户的上次登录时间。 可以调用 创建 New-xDscResourceProperty 新属性,然后调用 Update-xDscResource 新属性并将其添加到属性列表中,而不是再次完全写入资源。
$lastLogon = New-xDscResourceProperty –Name LastLogon –Type Hashtable –Attribute Write –Description "For mapping users to their last log on time"
Update-xDscResource –Name 'Demo_ADUser' –Property $UserName, $Ensure, $DomainCredential, $Password, $lastLogon -Force
测试资源架构
资源设计器工具公开了另一个 cmdlet,可用于测试手动编写的 MOF 架构的有效性。 调用 Test-xDscSchema cmdlet,将 MOF 资源架构的路径作为参数传递。 cmdlet 将输出架构中的任何错误。
另请参阅
概念
生成自定义 Windows PowerShell 所需状态配置资源