Tuesday, September 24, 2013

Chef: Using Powershell as conditional step

Often times, in Chef, when working with Windows, you'll want to check something on the machine before acting on it.

Chef gives us the handy only_if and not_if conditional's, where we can write ruby code to check things first.  In the linux world, this works out great almost every time.  In the Windows world, you'll probably want to run some Powershell instead.

Using the handy powershell_out mixin, we can extend the current provider to let us make a powershell call, get the return value, and act upon it.

The powershell script must use a return statement, and you can get the value from the stdout property of the result.  Just to keep life interesting, it adds a cr/lf, so you can use the handy ruby chop method.

The below example sets the service account of a windows service and restarts it only if it is not already configured.

No comments:

Post a Comment