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',
}