Praneeth
Praneeth

Reputation: 2547

Detecting if windows service is already installed using puppet exec

I have below exec resource which is used to install a window service.

I want to it to execute only if the window service is not installed. So I use the onlyif command but the command is not working. It is running the install all the time:

exec { "install-${target_path}/${binary_name}":
    command   => "${installUtil_filepath} /i /servicename=\"${service_name}\" /displayname=\"${display_name}\" /description=\"${description}\" \"${target_path}\\${binary_name}\"",
    onlyif    => "if((Get-Service \"${service_name}\" -ErrorAction SilentContinue).DisplayName -eq  ${display_name}) { exit 1 } else { exit 0 }",
    logoutput => true,
    provider => powershell,
  } 

This worked

if(Get-Service \"${service_name}\") { exit 1 } else { exit 0 }

Upvotes: 4

Views: 1425

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

Why are you first retrive the service using the service name and then also compare the display name? You can probably omit that check...

Also, don't you want to exit with exit 0 when the service is installed and otherwise with 1?

Try this:

onlyif    => "if(Get-Service ${service_name}) { exit 0 } else { exit 1 }",

Upvotes: 2

Related Questions