Chef DK on Windows

[edit on GitHub]

This guide details some of the common considerations that come into play when using the Chef Development Kit on Windows.

Configure Environment

You can use the chef shell-init command with Windows PowerShell to add the appropriate variables to your environment.

To try it in your current session:

chef shell-init powershell | Invoke-Expression

To enable it permanently:

"chef shell-init powershell | Invoke-Expression" >> $PROFILE

If $PROFILE does not exist, continue to the next section.

Create a Profile

On new Windows machines, commands cannot be appended to $PROFILE if the folder does not exist, or if there is a new user profile. This will result in an error similar to the following:

PS C:\Users\Stuart> "chef shell-init powershell | Invoke-Expression" >> $PROFILE
out-file : Could not find a part of the path
'C:\Users\Stuart\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
At line:1 char:1
+ "chef shell-init powershell | Invoke-Expression" >> $PROFILE
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

In this situation, run the following chef shell-init command instead:

if(Test-Path $PROFILE){ chef shell-init powershell | Add-Content $PROFILE } else { New-Item -Force -ItemType File $PROFILE; chef shell-init powershell | Add-Content $PROFILE }

Set the Execution Policy

On new Windows machines, PowerShell scripts will not work until an administrator runs the following command:

Set-ExecutionPolicy RemoteSigned

Spaces and Directories

Directories that are used by Chef on Windows cannot have spaces. For example, C:\Users\Steven Danno will not work, but C:\Users\StevenDanno will. Because of this, the knife cookbook site install subcommand will fail if the directory contains a space.

Top-level Directory Names

Windows will throw errors when path name lengths are too long. For this reason, it’s often helpful to use a very short top-level directory, much like what is done in UNIX and Linux. For example, Chef uses /opt/ to install the Chef development kit on macOS. A similar approach can be done on Microsoft Windows, by creating a top-level directory with a short name. For example: C:\chef.