1

I am trying to run a Powershell command directly using Puppet exec resource instead of specifying path to the Powershell script.

exec { "Change status and start-up of Win service":
    command => 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NoLogo -NonInteractive -Command "& {set-service Spooler -Status Running -StartupType Automatic; exit (1 - [int]$?)}"',
    onlyif  => "if(Get-Service Spooler) { exit 0 } else { exit 1 }",
    logoutput => 'on_failure',
}

When i try running the above command, i get the following error:

Error: Failed to apply catalog: Parameter onlyif failed on Exec[Change status and start-up of Win service]: 'if(Get-Service Spooler
) { exit 0 } else { exit 1 }' is not qualified and no path was specified. Please qualify the command or specify a path. at /etc/puppetlabs/code/environments/production/modules/common/manifests/svc_auto_running.pp:17

Saw few links and all mentioned specifying the full path to binary, which in this case i think is path to PS executable and that's already specified.

Any help will be appreciated.

UPDATE: After i tried wrapping the onlyif statement in powershell command, i was able to successfully run the powershell command directly using exec. However, now the issue is that if i try checking for a non-existent Win service in onlyif statement, the command still passes successfully.

$powershell = 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NoLogo -NonInteractive'

exec { "Change status and start-up of Win service":
    command   => "$powershell -Command '& {set-service iDoNotExist -Status Running -StartupType Automatic; exit (1 - [int]$?)}'",
    #onlyif   => "$powershell -Command '& {if(Get-Service iDoNotExist); exit (1 - [int]$?)}'",
    onlyif    => "$powershell -Command '& {if(Get-Service iDoNotExist) { exit 0 } else { exit 1 }}'",
    logoutput => 'on_failure',
}
Technext
  • 7,887
  • 9
  • 48
  • 76
  • 4
    You probably need to also wrap the `onlyif` statement in a powershell command, I guess at the moment it's trying to run an executable. – arco444 Mar 01 '17 at 12:35
  • Thanks @arco444! That worked. :) You may post it as an answer and i will close this. BTW, is there any better alternative to this which i can employ? – Technext Mar 01 '17 at 17:33
  • Again stuck although with a different issue. Updated my post. – Technext Mar 01 '17 at 18:42

2 Answers2

1

Probably it is better to use powershell privider to run those command. You can install this module: puppetlabs/powershell.

It provides a powershell provider that can be used to execute Powershell commands. It will probably help you to avoid a lot of nasty edge cases when executing command during puppet run.

Cosaquee
  • 724
  • 1
  • 6
  • 22
  • Thanks for the suggestion. I was actually using the powershell provider earlier for all execs until i faced [this](http://stackoverflow.com/questions/42484043/capturing-output-error-when-invoking-powershell-script/42485152?noredirect=1#comment72112698_42485152) issue. So now i am avoiding it. – Technext Mar 07 '17 at 11:46
1

As suggested by @arco444, i tried wrapping the onlyif statement in a powershell command and it worked for me but later, as stated in my post's UPDATE section, i got another issue. Finally, the following worked:

$powershell = 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NoLogo -NonInteractive'

exec { "Change status and start-up of Win service":
    command   => "$powershell -Command \"& {set-service $service_name -Status Running -StartupType Automatic; exit (1 - [int]$?)}\"",
    onlyif    => "$powershell -Command '& {\"if(Get-Service ${service_name})\"; exit (1 - [int]$?)}'",
    logoutput => 'on_failure',
}
Technext
  • 7,887
  • 9
  • 48
  • 76