title: Running Powershell command directly using Puppet exec resource
body: <p>I am trying to run a Powershell command directly using Puppet <code>exec</code> resource instead of specifying path to the Powershell script.</p>
  
  <pre><code>exec { "Change status and start-up of Win service":
      command =&gt; 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NoLogo -NonInteractive -Command "&amp; {set-service Spooler -Status Running -StartupType Automatic; exit (1 - [int]$?)}"',
      onlyif  =&gt; "if(Get-Service Spooler) { exit 0 } else { exit 1 }",
      logoutput =&gt; 'on_failure',
  }
  </code></pre>
  
  <p>When i try running the above command, i get the following error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Saw few links and all mentioned specifying the full path to binary, which in this case <em>i think</em> is path to PS executable and that's already specified.</p>
  
  <p>Any help will be appreciated.</p>
  
  <p>UPDATE: After i tried wrapping the <code>onlyif</code> 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 <em>non-existent</em> Win service in <code>onlyif</code> statement, the command <em>still passes successfully</em>.</p>
  
  <pre><code>$powershell = 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NoLogo -NonInteractive'
  
  exec { "Change status and start-up of Win service":
      command   =&gt; "$powershell -Command '&amp; {set-service iDoNotExist -Status Running -StartupType Automatic; exit (1 - [int]$?)}'",
      #onlyif   =&gt; "$powershell -Command '&amp; {if(Get-Service iDoNotExist); exit (1 - [int]$?)}'",
      onlyif    =&gt; "$powershell -Command '&amp; {if(Get-Service iDoNotExist) { exit 0 } else { exit 1 }}'",
      logoutput =&gt; 'on_failure',
  }
  </code></pre>