- Added xNetworking and removed requirement for DHCP.
- Please NOTE that this method does not currently work with WMF5 as the Host when targeting a WMF4 WM.
Invoke-DSCBuild -Target PKI | Test-Lab
Before I can get into the really fun stuff with DSC I need the prerequisites for a secure DSC environment. This involves a few things. The minimum is a Certificate Authority, the other is a HTTPS pull server. For my Lab I’m also going to also have a Domain Controller, because most enterprises will have a domain, and because it simplifies permissions.
- Domain Controller
- Enterprise CA
- Pull Server
Initial server build and Self Signed Certificate
I’m going to use my patched image as a baseline. Now because xAdcsDeployment and File resources require Credentials, I will have to create a self signed certificate to secure the passwords. I don’t want to use plaintext in a .MOF file.
Now I created a helper function that does the following.
- Copy the patch VHDX
- Mount to a temporary folder
- copy in files
- unattnd.xml (same one I used for the Patched Image creation)
- FirstRun.ps1 script
- AtStartup.ps1 script (SCC-Init.ps1)
- any other supporting scripts (New-SelfSignedCertificateEx)
- Save VHDX
- Start VM
- Wait for VM to stop
- Create the AtStartup job to run AtStartup.ps1
- Start a transcript to SCC-Init.log
- check if pfx was copied in, (and I did not)
- if PFX present install that.
- if not, use New-SelfSignedCertificateEx (from Script Center)to create a certificate
- Export the certificate to a CER file
- shutdown the VM
Extracting the Certificate
Once we have a self signed certificate for encrypting passwords we need to get if off the VM and back to the host.
Again I created a helper function for this.
- Verify the VM is off
- if on, run stop-vm
- Mount the VHDX to a temporary folder
- copy the file from the relative path inside the VHDX to the supplied path on the host
- Dis-Mount the VHDX discarding changes changes.
Creating the MOF
Now due to how the ISE reacts to the having a “Configuration” in a file I separated out the configuration to a .ps1(Intelisence tends to timeout in scripts with a configuration in the ISE in WMF 4)
- Get the Certificate thumbprint from the CER file.
- Import Credentials from clixml for:
- Local Administrator (used to change the built-in Administrator and for setting up a Certificate Authority)
- Remote user needed to access the source.wim for adding features
- Create a hash table with AllNodes to pass the certificate thumbprint and CER file path with
- Configuration Block
- Set the IP Address
- Set Computer name
- Copy the source.wim from a remote share (my win8 workstation on the lab network)
- Now this point was something of a pain. In a production environment you don’t want to copy this file over. However due to how DISM (what WindowsFeature and install-WindowsFeatuere are wrappers for) Cant access a network share (even when guest in enabled) on WMF4. (WMF5 adds the ability to run each resource in specified user accounts and may fix this) The LCM runs as the local System account, In a Domain environment you can grant permissions via “Domain Computers” to a share and point the WindowsFeature resorce at the UNC to Source.WIM
- Install the Certificate Authority WindowsFeature
- Initialize the Certficate Authority useing xAdcsCertificationAuthority
- Set the location for the Certificate Revocation list and re-export
- This is done via a script resource, as xAdcsCertificationAuthority does not currently have a way to do this
- Create the LocalHost.Meta.MOF and LocalHost.MOF files
I had planed to set the IP address, but xNetworking was causing the DSC to hang in my tests.
So I had to turn on DHCP on my Voys router used in the Lab. I hope to track this down and submit a fix.
This issue was resolved and noted below
Armed with the required .MOF files I need to get them copied into the VM and and start DSC. Once again a helper function, (pardon my poor naming conventions)
- Stop the VM if it’s running
- Mount the VHDX to a temp folder
- Copy in a new AtStartup.ps1 (DSC-Init.ps1)
- If LocalHostMofFolder set
- Copy in LocalHost.mof and LocalHost.Meta.Mof
- Else copy
- CofnigMof as Pending.mof
- Create metaconfig.mof from supplied string
- Dismount VHDX saving changes
I initially was going to go with copy in Pending and set Metaconfig.mof directly then use Invoke-CimMethod, as shown by the PowerShell team blog However after fighting with a failing config I went to Start-DscConfiguration. This did not fix my issue with xNetworking but I have had better luck with this method.
This issue actually had to do having WMF5 on the host. Removing WMF5 preview and returning to WMF4 and my problems were resolved. so I have added xNetworking back in.
However I’m staying with the current use of start-DSCConfiguration rather than a WMI call. The debugging is simpler with -verbose.
- Starts a transcript to DSC-Init.log
- Unregister the AtStartup scheduled task
- Remove c:\Unattend.xml
- The plain text password contained inside will be changed by DSC but not point in leaving a mess.
- Enable the Analytic and Debug logs for DSC
- Run Set-DSCLocalConfigurationManager
- Run Start-DscConfiguration with -Wait -Verbose
Enabling the log channels and using -wait -verbose with a transcript helps a lot with debugging
Putting it all together
I put all the functions into a quickly made module to simplify importing them.
I created a wrapper script that will put all this together and build my Root-CA
- Set the a number of variables
- Path to DSC Config Sript
- Start-Deploy parameters for splating
- Get-VmCert parameters for splating
- Set-VmMof Parameters fro splatting
- Create the clixml files containing the Passwords
- Import-Module on the sub folder holding all my functions
- Dot Source the DSC Config Script
- Start the VM
- Remove-Module -Name Functions
The reason for saving the passwords to XML avoids the need to enter credentials on subsequent runs of the script. I want to be able to tare down and rebuild the lab from scratch at any time.
One other file you will need is the unattend.xml
The folder structure for all of this is
G:\BuildLab │ Build-DC.ps1 │ Build-RootCA.ps1 │ Root-CA.LocalAdminCred.xml │ Root-CA.RemoteUserCred.xml │ RootCA_Config.ps1 │ Unattend.xml │ ├───Functions │ Functions.psd1 │ Functions.psm1 │ Get-VmCert.ps1 │ Set-VmLcm.ps1 │ Start-Deploy.ps1 │ Unattend.xml │ ├───Helpers │ New-SelfSignedCertificateEx.ps1 │ ├───Resorces_RootCA │ ├───xAdcsDeployment │ ├───xComputerManagement │ ├───xDscDiagnostics │ └───xNetworking │ ├───RootCA │ localhost.meta.mof │ localhost.mof │ └───VMScripts DSC-Init.ps1 FirstRun.ps1 SSC-Init.ps1
This script took a lot longer then planed and I got to use xDscDiagnostics a lot to track errors. That is outside of the scope of this post but I plan to write about it at some time. I did have to enable DHCP on the router, and that is something I hope to address and update this post. I want to use a DSC configured DHCP. but in this case I have a clasic chicken and egg issue. I need the CA, DC and Pull server first.