Use a certificate with PowerShell DSC to add a server to Active Directory without hard coding a password

A new Windows Server 2012 R2 machine has been brought online and needs to be joined to your Active Directory domain. All machines used in this demonstration are running either Windows Server 2012 R2 or Windows 8.1 with PowerShell version 4.

You’ve decided to use DSC (Desired State Configuration) to join this new server to the domain because it’s a prototype for many more servers to come. You plan to automate their deployment along with the majority of their configuration with DSC.

While this blog article doesn’t fully automate all of the process required to add the target node to the domain, it is meant to demonstrate the encryption and decryption of the password for the domain user account that is required to add it to the domain which would otherwise have to be stored in clear text.

The name of the target node has been added to the trusted host list on the machine that is being used to author and apply the DSC configuration:

dsc-cert6

The xComputerManagment module which is part of DSC Resource Kit Wave 8, contains the DSC resource that will be used to add our new server to the Active Directory domain. This module has been copied to “C:\Program Files\WindowsPowerShell\Modules” on the target node and the machine that will be used to author and apply the DSC configuration.

Fellow PowerShell MVP Vadims Podans has a PowerShell self-signed certificate generator that was used to create a self-signed certificate on the target node:

dsc-cert11

Export the public key:

dsc-cert10

The public key that was exported in the previous step has been copied to the machine that will be used to author and apply the DSC configuration. The Thumbprint of the certificate has been entered into the DSC configuration as shown in the following example.

I had previously read an article on MSDN titled “Deploying using PowerShell Desired State Configuration in Release Management” that demonstrates using an initialization script so I thought I would take advantage of that technique here:

dsc-cert1

The LCM (Local Configuration Manager) on the target node needs to be made aware of the certificate that will be used to decrypt the password. That portion of the configuration could be split out into a separate configuration. I also set the LCM on the target node to automatically reboot the machine if a restart is required by configuration changes:

dsc-cert2

When the configuration is run, two MOF files are created. One for the LCM and the other for the node configuration that you typically see created with DSC. The credential that is specified with this command is a domain account that has the necessary rights to add the machine to the domain. The password for this credential is what will be encrypted in the MOF file that is created:

dsc-cert3

The following example shows the contents of the WIN-U74LBSGF2CA.mof file:

Notice that the password is encrypted in the mof file as shown the previous example. That is the whole point of this blog article. Don’t store passwords in plain text <period>.

This example shows the contents of the WIN-U74LBSGF2CA.meta.mof file:

Now to apply the changes to the LCM on the target node. The credential that is specified with this command is a local userid and password that has the necessary rights to perform these configuration changes on the target node:

dsc-cert4

Why didn’t I use a domain account in the previous example? Because the machine isn’t yet a member of any Active Directory domain. It’s a brand new server that’s in a workgroup.

Now to apply the DSC configuration to the new server (our target node). Once again, we need to specify a local userid and password on the target node when prompted for credentials with this command. I could have previously stored the credentials in a variable and specified the variable since this is the second time I’ve had to type them in:

dsc-cert5

If you happen to be signed into the target node when the configuration is applied, you’ll receive the following message and about two seconds later the machine will restart:

dsc-cert7

Once the restart of the target node is complete, it will have been renamed and added to the domain just like magic and all without needing to store any passwords in plain text!

Troubleshooting notes:

If you receive this error, there’s a problem with the certificate you’re using. I ended up with this error when I used a certificate that was created with New-SelfSignedCertificate.

The private key could not be acquired.
+ CategoryInfo : NotSpecified: (root/Microsoft/…gurationManager:String) [], CimException
+ FullyQualifiedErrorId : MI RESULT 1
+ PSComputerName : WIN-U74LBSGF2CA

dsc-cert8

If you receive this error, you could have a certificate mismatch problem:

Access is denied.
+ CategoryInfo : PermissionDenied: (root/Microsoft/…gurationManager:String) [], CimException
+ FullyQualifiedErrorId : HRESULT 0x80070005
+ PSComputerName : WIN-U74LBSGF2CA

dsc-cert9

When I encountered the certificate mismatch problem, it was because I had initially used a certificate that wouldn’t work and then when I generated another certificate, I hadn’t reconfigured the LCM on the target node to use it so the encryption was occurring with one certificate and the decryption was attempting to use a different certificate.

µ

Leave a Reply

%d bloggers like this: