I’ve previously written a blog article titled “ Use a certificate with PowerShell DSC to add a server to Active Directory without hard coding a password ” where I had created a certificate that was used to encrypt the password in a PowerShell version 4 DSC (Desired State Configuration) MOF file.

The same procedure in PowerShell v5 generates an error stating the certificate cannot be used for encryption:


Create a Certificate to Encrypt MOF Passwords that’s Compatible with DSC in Pow ...

ConvertTo-MOFInstance : System.ArgumentException error processing property ‘Password’ OF TYPE ‘MSFT_Credential’:

Certificate ‘6EBFB5C88AB4B8C9E3B8E30E88A5D071D6735464’ cannot be used for encryption. Encryption certificates must

contain the Data Encipherment or Key Encipherment key usage, and include the Document Encryption Enhanced Key Usage

(1.3.6.1.4.1.311.80.1).

At C:\windows\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:303

char:13

+ ConvertTo-MOFInstance MSFT_Credential $newValue

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (:) [Write-Error], InvalidOperationException

+ FullyQualifiedErrorId : FailToProcessProperty,ConvertTo-MOFInstance

Write-NodeMOFFile : Invalid MOF definition for node ‘SQL01’: Exception calling “ValidateInstanceText” with “1”

argument(s): “Syntax error:

At line:11, char:1

Buffer:

T_Credential1ref

{

}^;

in

At

C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2275

char:21

+ … Write-NodeMOFFile $Name $mofNode $Script:NodeInstanceAlia …

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (:) [Write-Error], InvalidOperationException

+ FullyQualifiedErrorId : InvalidMOFDefinition,Write-NodeMOFFile

Errors occurred while processing configuration ‘test’.

At

C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:3705

char:5

+ throw $ErrorRecord

+ ~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (test:String) [], InvalidOperationException

+ FullyQualifiedErrorId : FailToProcessConfiguration

The solution to this problem is contained in the error message. “ Encryption certificates must contain the Data Encipherment or Key Encipherment key usage, and include the Document Encryption Enhanced Key Usage “.

As used in the previously referenced blog article, the PowerShell self-signed certificate generator function that Fellow Microsoft MVP Vadims Podans wrote is used to create acertificate on the target node:

New-SelfSignedCertificateEx -Subject 'CN=SQL01' -StoreLocation LocalMachine -StoreName My -KeyUsage DataEncipherment, KeyEncipherment -EnhancedKeyUsage 'Document Encryption'
Create a Certificate to Encrypt MOF Passwords that’s Compatible with DSC in Pow ...

Notice in the previous example that the KeyUsage and EnhancedKeyUsage parameters are specified along with the values from the previously referenced error message so this certificate is generated with the necessary requirements to be used for encryption with DSC in PowerShell version 5.0.

Determine the thumbprint of the certificate (it’s the only one that’s been created in the past two days):

Get-ChildItem -PathCert:\LocalMachine\My\ | Where-Object NotBefore -gt (Get-Date).AddDays(-2)
Create a Certificate to Encrypt MOF Passwords that’s Compatible with DSC in Pow ...

Export the certificate to a file:

Export-Certificate -Cert Cert:\LocalMachine\My\3652E4B2C9C885B4A93E81A07407008780D48356 -FilePath C:\tmp\sql01cert.cer
Create a Certificate to Encrypt MOF Passwords that’s Compatible with DSC in Pow ...

The exported certificate has been copied to the computer used for authoring the configuration.

Both the MOF and meta.MOF files now generate without a problem:

test -ConfigurationData $ConfigData -Credential (Get-Credential)
Create a Certificate to Encrypt MOF Passwords that’s Compatible with DSC in Pow ...

The password is indeed encrypted:

Get-Content -Path C:\test\SQL01.mof | Select-String -Pattern 'Password'
Create a Certificate to Encrypt MOF Passwords that’s Compatible with DSC in Pow ...

If you’ve found an easier way to accomplish this task, I’d love to hear about it via a comment to this blog article.

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: PowerShellSQLWindowsB2C
分页:12
转载请注明
本文标题:Create a Certificate to Encrypt MOF Passwords that’s Compatible with DSC in Pow ...
本站链接:http://www.codesec.net/view/482438.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(47)