Skip to content

Setup Windows 10 For Chef and PowerShell DSC Development

· 6 min

I am in the process of writing up some blog posts about working with PowerShell Desired State Configuration (DSC) and OpsCode Chef from a Windows Workstation / Windows Server perspective.

This first article will cover the steps required to setup a development environment for someone that is doing work with PowerShell Desired State Configuration (DSC) or OpsCode Chef.

We will be covering the following:

Once everything is installed, we will be customizing and setting up the following:

Let’s get started.

Set Execution Policy and Install Applications#

The first step is to install the tools and applications we need. The easiest way to do this is with a combination of Chocolatey and OneGet which is now built into Windows 10.

Terminal window
# Configure PowerShell Execution Policy
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
# Install Chocolatey
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
# Install the required apps
choco install git.install -y
choco install virtualbox -y
choco install vagrant -y
choco install chefdk -y
choco install atom -y
# Install Posh-Git
Install-Module posh-git
# Optional - install tabbed Explorer
choco install clover -y
# Optional - free git GUI
choco install sourcetree -y
# OR you could try
choco install gitextensions -y
# Update Pester - https://github.com/pester/Pester
Install-Module -Name Pester -Confirm:$true
# Install the PSScriptAnalyzer - https://github.com/PowerShell/PSScriptAnalyzer
Install-Module -Name PSScriptAnalyzer -Confirm:$true

Customize Your PowerShell Profile#

Now that you have installed PoshGit, it is a good idea to take a look at your PowerShell Profile.

You can make any sort of customization you like, but here are two suggestions for things to do in your profile:

I have posted my PowerShell profile on GitHub as an example here.

My profile contains some useful functions for setting and reloading environment variables which I recommend you use in your own profile.

Path Environment Variable#

With many of the tools used for developing with Chef on Windows, they will require a correctly configured Path environment variable.

If one of your tools is not working or you cannot run it from the command line, there is a good chance something is wrong with your Path variable. For example, to use Ruby from the command line after you install the ChefDK, you need to add ruby to the path variable.

If you are using the functions from my PowerShell profile from above, it is very easy to add Path environment variables:

Terminal window
# Requires the Add-PathVariable function from my PowerShell Profile
Add-PathVariable -Path 'C:/opscode/chefdk/embedded/bin'

Move PowerShell Profile to a Synced Drive (Optional)#

If you move around to different machines, it is a good idea to move your your PowerShell Profile into a synced directory like Dropbox or OneDrive. From there you can create a symlink from the profile .ps1 in the synced path to the $PROFILE path.

Terminal window
# Create a symlink to the profile in your shared drive
cmd /c mklink $PROFILE D:\DataHodge\Dropbox\PSProfile\Microsoft.PowerShell_profile.ps1
# Load the profile into the current session
. $PROFILE

Symlink to PowerShell Profile

Symlink to PowerShell Profile

Configure Atom#

Atom is a great text editor which I like to use for everything but working on PowerShell scripts. It is my editor of choice for modifying Chef recipes.

We can make it more powerful with some additional packages.

Terminal window
# Linter to validate the code as you are typing
apm install linter
# Install rubocop gem
gem install rubocop
# Linter for ruby
apm install linter-rubocop
# Rubocop auto corrector
apm install rubocop-auto-correct
# Create a rubocop.yml configuration file to ignore warnings for line endings. Details here https://github.com/bbatsov/rubocop/blob/master/README.md
Set-Content -Path ~/.rubocop.yml -Value 'Metrics/LineLength:',' Enabled: false'
# Useful for removing Windows line endings
apm install line-ending-converter
# Gives a view of your entire document when it is open in atom
apm install minimap
# monokai theme for atom
apm install monokai

Setup Git SSH Keys#

You should be using source control for your Chef recipes and PowerShell scripts. Warren Frame has an excellent blog on the topic specific to PowerShell here.

Update 26/08/2016 - I created a more detailed guide on setting up Git SSH Keys on Windows. Check it out here: Ultimate PowerShell Prompt Customization and Git Setup Guide

Download Vagrant Boxes#

Vagrant is an excellent way to test your DSC scripts or Chef recipes.

I like to use 2 boxes for my Windows 2012 R2 and Ubuntu testing with Chef/DSC. There are also several plugins that make using Vagrant even nicer.

We will install some plugins and pre-load the Vagrant boxes:

Terminal window
# Install vagrant plugins
vagrant plugin install 'vagrant-berkshelf'
vagrant plugin install 'vagrant-dsc'
vagrant plugin install 'vagrant-omnibus'
vagrant plugin install 'vagrant-reload'
vagrant plugin install 'vagrant-vbguest'
vagrant plugin install 'vagrant-vbox-snapshot'
vagrant plugin install 'vagrant-winrm'
vagrant plugin install 'winrm-fs'
# Install vagrant boxes
vagrant box add ubuntu/trusty64
vagrant box add kensykora/windows_2012_r2_standard
# Install the test-kitchen plugins
gem install kitchen-pester

Configure Chef and Berkshelf#

Next step is to get your chef user.pem file sorted out. Chef has a how-to guide for this here.

Once you have your .pem file, we will setup the knife.rb and the berkshelf config.json.

Terminal window
# Create knife.rb config - more details here https://docs.chef.io/config_rb_knife.html
atom ~/.chef/knife.rb
# Create Berksfile config - more details here http://berkshelf.com/
atom ~/.berkshelf/config.json
# Verify you can communicate with the chef server
knife user list

Customize the PowerShell ISE Theme (Optional)#

The default theme for the PowerShell ISE is boring, lets spice it up with a theme. There is a great repository with PowerShell ISE themes located on GitHub.

To import the themes into the PowerShell ISE, go to Tools > Options > Manage Themes > Import.

PowerShell ISE Themes

PowerShell ISE Themes

Again, I would drop the theme into a synced folder so you can use it on all your machines.

Conclusion#

With that, you should have your Windows machine setup PowerShell DSC and Chef development. Did I miss anything? Send me a tweet @matthodge and let me know!