Search code examples
chef-infrachef-recipecookbookrecipe

how do i create a new windows service with chef?


So chef doesn't have any builtin support for creating new services. I create a new windows service in one of my recipes:

 powershell_script "Derp" do
  code <<-EOH
  new-service \
  -name derp "C:/path/myservice.exe" \
  -displayName "derp" \
  -StartupType Automatic \
  -Description "derp Service"
  EOH

So this runs fine on first run, but then it generates errors for subsequent runs because the service already exists.

Is the only solution here to do my own guarding? I like to avoid doing my own conditional in my recipes where ever possible. I can't be the only one with this problem, but I can't find a lot of examples online of how to solve this the chef way.


Solution

  • Not sure why you think Chef doesn't support creating services, the :enable action of the service resource works on Windows just like everywhere else.