配置数据中的凭据选项

适用于: Windows PowerShell 5.0

纯文本密码和域用户

包含未加密凭据的 DSC 配置将生成有关纯文本密码的错误消息。 此外,DSC 在使用域凭据时将生成警告。 若要禁止显示这些错误和警告消息,请使用 DSC 配置数据关键字:

  • PsDscAllowPlainTextPassword
  • PsDscAllowDomainUser

注释

存储/传输未加密的明文密码通常不安全。 建议使用本主题后面介绍的技术来保护凭据。 Azure 自动化 DSC 服务允许集中管理要在配置中编译并安全存储的凭据。 有关信息,请参阅: 编译 DSC 配置/凭据资产

在 DSC 中处理凭据

默认情况下,DSC 配置资源按其运行方式 Local System 运行。 但是,某些资源需要凭据,例如, Package 当资源需要在特定用户帐户下安装软件时。

早期的资源使用硬编码的 Credential 属性名称来处理此问题。 WMF 5.0 为所有资源添加了一个自动 PsDscRunAsCredential 属性。 有关使用 PsDscRunAsCredential的信息,请参阅 使用用户凭据运行 DSC。 较新的资源和自定义资源可以使用此自动属性,而不是为凭据创建自己的属性。

注释

某些资源的设计是出于特定原因使用多个凭据,并且它们将具有自己的凭据属性。

要查找资源上的可用凭据属性,请使用 Get-DscResource -Name ResourceName -Syntax ISE ()CTRL+SPACE 中的 Intellisense 或 Intellisense。

Get-DscResource -Name Group -Syntax
Group [String] #ResourceName
{
    GroupName = [string]
    [Credential = [PSCredential]]
    [DependsOn = [string[]]]
    [Description = [string]]
    [Ensure = [string]{ Absent | Present }]
    [Members = [string[]]]
    [MembersToExclude = [string[]]]
    [MembersToInclude = [string[]]]
    [PsDscRunAsCredential = [PSCredential]]
}

此示例使用内置 DSC 资源模块中的PSDesiredStateConfiguration资源。 它可以创建本地组并添加或删除成员。 它同时接受 Credential 属性和自动 PsDscRunAsCredential 属性。 但是,资源仅使用该 Credential 属性。

有关该 PsDscRunAsCredential 属性的详细信息,请参阅 使用用户凭据运行 DSC

示例:组资源凭据属性

DSC 在 下 Local System运行,因此它已具有更改本地用户和组的权限。 如果添加的成员是本地帐户,则不需要凭据。 如果资源将域帐户添加到本地组,则 Group 需要凭据。

不允许对 Active Directory 进行匿名查询。 Credential资源的Group属性是用于查询 Active Directory 的域帐户。 对于大多数目的,这可以是通用用户帐户,因为默认情况下用户可以 读取 Active Directory 中的大多数对象。

示例配置

以下示例代码使用 DSC 使用域用户填充本地组:

Configuration DomainCredentialExample
{
    param
    (
        [PSCredential] $DomainCredential
    )
    Import-DscResource -ModuleName PSDesiredStateConfiguration

    node localhost
    {
        Group DomainUserToLocalGroup
        {
            GroupName        = 'ApplicationAdmins'
            MembersToInclude = 'contoso\alice'
            Credential       = $DomainCredential
        }
    }
}

$cred = Get-Credential -UserName contoso\genericuser -Message "Password please"
DomainCredentialExample -DomainCredential $cred

此代码生成错误和警告消息:

ConvertTo-MOFInstance : System.InvalidOperationException error processing property 'Credential' OF
TYPE 'Group': Converting and storing encrypted passwords as plain text is not recommended.
For more information on securing credentials in MOF file, please refer to MSDN blog:
https://go.microsoft.com/fwlink/?LinkId=393729

At line:11 char:9
+   Group
At line:341 char:16
+     $aliasId = ConvertTo-MOFInstance $keywordName $canonicalizedValue
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Write-Error], InvalidOperationException
    + FullyQualifiedErrorId : FailToProcessProperty,ConvertTo-MOFInstance
WARNING: It is not recommended to use domain credential for node 'localhost'. In order to suppress
the warning, you can add a property named 'PSDscAllowDomainUser' with a value of $true to your DSC
configuration data for node 'localhost'.

Compilation errors occurred while processing configuration
'DomainCredentialExample'. Please review the errors reported in error stream and modify your
configuration code appropriately.
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:3917 char:5
+     throw $ErrorRecord
+     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (DomainCredentialExample:String) [], InvalidOperationException
    + FullyQualifiedErrorId : FailToProcessConfiguration

此示例有两个问题:

  1. 错误说明不建议使用纯文本密码
  2. 警告建议不要使用域凭据

标志 PSDSCAllowPlainTextPasswordPSDSCAllowDomainUser 禁止显示错误和警告,通知用户所涉及的风险。

PSDSCAllowPlainTextPassword(PSDSCAllow纯文本密码)

第一个错误消息有一个带有文档的 URL。 此链接介绍如何使用 ConfigurationData 结构和证书加密密码。 有关证书和 DSC 的更多信息, 请阅读这篇文章

要强制使用纯文本密码,资源需要 PsDscAllowPlainTextPassword 配置数据部分中的关键字,如下所示:

$password = "ThisIsAPlaintextPassword" | ConvertTo-SecureString -asPlainText -Force
$username = "contoso\Administrator"
[PSCredential] $credential = New-Object System.Management.Automation.PSCredential($username,$password)

Configuration DomainCredentialExample
{
    Import-DscResource -ModuleName PSDesiredStateConfiguration

    node localhost
    {
        Group DomainUserToLocalGroup
        {
            GroupName        = 'ApplicationAdmins'
            MembersToInclude = 'contoso\alice'
            Credential       = $credential
        }
    }
}

$cd = @{
    AllNodes = @(
        @{
            NodeName = 'localhost'
            PSDscAllowPlainTextPassword = $true
        }
    )
}

DomainCredentialExample -ConfigurationData $cd

本地主机.mof

PSDSCAllowPlainTextPassword 标志要求用户承认在 MOF 文件中存储纯文本密码的风险。 在生成的 MOF 文件中,即使使用了包含 SecureStringPSCredential 对象,密码仍显示为纯文本。 这是唯一一次暴露凭据。 获得对此 MOF 文件的访问权限使任何人都可以访问管理员帐户。

/*
@TargetNode='localhost'
@GeneratedBy=Administrator
@GenerationDate=01/31/2019 06:43:13
@GenerationHost=Server01
*/

instance of MSFT_Credential as $MSFT_Credential1ref
{
Password = "ThisIsAPlaintextPassword";
 UserName = "Administrator";

};

instance of MSFT_GroupResource as $MSFT_GroupResource1ref
{
ResourceID = "[Group]DomainUserToLocalGroup";
 MembersToInclude = {
    "contoso\\alice"
};
 Credential = $MSFT_Credential1ref;
 SourceInfo = "::11::9::Group";
 GroupName = "ApplicationAdmins";
 ModuleName = "PSDesiredStateConfiguration";

ModuleVersion = "1.0";

 ConfigurationName = "DomainCredentialExample";

};

传输中和静态凭据

  • PSDscAllowPlainTextPassword 标志允许编译包含明文密码的 MOF 文件。 存储包含明文密码的 MOF 文件时,请采取预防措施。
  • 当 MOF 文件以 推送 模式传递到节点时,WinRM 会加密通信以保护明文密码,除非使用 AllowUnencrypted 参数替代默认值。
    • 使用证书加密 MOF 可在将 MOF 文件应用于节点之前保护静态 MOF。
  • 拉取 模式下,可以将 Windows 拉取服务器配置为使用 HTTPS 使用 Internet Information Server 中指定的协议对流量进行加密。 有关详细信息,请参阅设置 DSC 拉取客户端使用证书保护 MOF 文件一文。
  • 在节点上,从 PowerShell 5.0 开始,MOF 文件在静态时加密。
    • 在 PowerShell 4.0 中,MOF 文件在静态时未加密,除非它们在推送到或拉取到节点时使用证书进行加密。

由于存在重大安全风险,Microsoft 建议避免使用纯文本密码。

域凭据

再次运行示例配置脚本(使用或不加密)仍会生成警告,提示不建议使用域帐户作为凭据。 使用本地帐户可以消除可能暴露可在其他服务器上使用的域凭据。

将凭据与 DSC 资源一起使用时,请尽可能首选本地帐户而不是域帐户。

如果凭据的属性中有 Username “\”或“@”,则 DSC 会将其视为域帐户。 用户名的域部分中的“localhost”、“127.0.0.1”和“::1”存在例外。

PSDscAllowDomain用户

在上面的 DSC Group 资源示例中,查询 Active Directory 域 需要 域帐户。 在这种情况下,将属性添加到 PSDscAllowDomainUser 块中 ConfigurationData ,如下所示:

$password = "ThisIsAPlaintextPassword" | ConvertTo-SecureString -asPlainText -Force
$username = "contoso\Administrator"
[PSCredential] $credential = New-Object System.Management.Automation.PSCredential($username,$password)

Configuration DomainCredentialExample
{
    Import-DscResource -ModuleName PSDesiredStateConfiguration

    node localhost
    {
        Group DomainUserToLocalGroup
        {
            GroupName        = 'ApplicationAdmins'
            MembersToInclude = 'contoso\alice'
            Credential       = $credential
        }
    }
}

$cd = @{
    AllNodes = @(
        @{
            NodeName = 'localhost'
            PSDscAllowDomainUser = $true
            PSDscAllowPlainTextPassword = $true
        }
    )
}

DomainCredentialExample -ConfigurationData $cd

现在,配置脚本将生成没有错误或警告的 MOF 文件。