Powershell onlyif and exit code

1,073 views
Skip to first unread message

Leon Kyneur

unread,
Feb 5, 2014, 9:36:07 PM2/5/14
to puppet...@googlegroups.com
Hi,

I have a powershell script which I'm trying to implement within an exec/onlyif condition.

The script I'm working with is quite complex so I don't really want to put all of the logic inline and would prefer it stayed contained within the .ps1. I'm running into issues calling powershell and returning an exit code.

For example my .ps1 file has a statement like this:

If (condition) {
  # run exec command =>
  exit 1
} else {
  # don't run exec command =>
  exit 0
}

In my exec I have a command structured like so:

exec { "run-script":
  path =>  'C:\Windows\sysnative\WindowsPowershell\v1.0',
  command => "powershell.exe -File C:/myscript.ps1 arg1 arg2",
  onlyif => "powershell.exe -File C:/myscript.ps1 -runcheck",
}

Regardless of the condition / exit code within the script the command portion always runs always runs. I've tried to switch out -File for -Command without success.

Can anyone point out a way around this?

Regards,

Leon

Rob Reynolds

unread,
Feb 6, 2014, 12:52:18 AM2/6/14
to puppet...@googlegroups.com
This is a good resource for PowerShell and its interesting behaviors: http://zduck.com/2012/powershell-batch-files-exit-codes/



--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAPnT6nN51aUYOSBTYzD6M-X9768pG2WwE6vXzQnJGNNATR7cgw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Rob Reynolds
Developer, Puppet Labs

Join us at PuppetConf 2014September 23-24 in San Francisco - http://bit.ly/pupconf14

Leon Kyneur

unread,
Feb 6, 2014, 6:39:46 AM2/6/14
to puppet...@googlegroups.com

Thanks Rob. I'm well aware of both resources you mentioned but neither have provided me with a valid solution to my problem.

The powershell provider works well if I place my code inline within the manifest.

However the script I'm working with had a lot of logic. I don't see it being very maintainable all mashed up together inline in my onlyif statement.

Even following the all suggestions from the article on exit codes I cannot clearly see why puppet doesn't get the correct exit code when called from within the ps1 file.

Rob Reynolds

unread,
Feb 6, 2014, 4:45:30 PM2/6/14
to puppet...@googlegroups.com
On Thu, Feb 6, 2014 at 5:39 AM, Leon Kyneur <leon....@gmail.com> wrote:

Thanks Rob. I'm well aware of both resources you mentioned but neither have provided me with a valid solution to my problem.

The powershell provider works well if I place my code inline within the manifest.

However the script I'm working with had a lot of logic. I don't see it being very maintainable all mashed up together inline in my onlyif statement.

Even following the all suggestions from the article on exit codes I cannot clearly see why puppet doesn't get the correct exit code when called from within the ps1 file.

Taking Puppet out of the mix for a second, do you get the correct exit code when you call the ps1 file with a batch file?
 

For more options, visit https://groups.google.com/groups/opt_out.

Josh Cooper

unread,
Feb 11, 2014, 3:37:02 PM2/11/14
to puppet...@googlegroups.com
On Thu, Feb 6, 2014 at 3:39 AM, Leon Kyneur <leon....@gmail.com> wrote:

Thanks Rob. I'm well aware of both resources you mentioned but neither have provided me with a valid solution to my problem.

The powershell provider works well if I place my code inline within the manifest.

You are probably better off putting the powershell logic in an template, e.g. https://github.com/joshcooper/example-guest/blob/master/manifests/init.pp#L39-L44

If you need to interpolate values, then this approach means you don't have to escape $, as you would if it was inline with double quotes.

If you don't need to interpolate values, you should be able to drop your existing powershell code into the templates directory and point your onlyif/unless parameters at them, like the example-guest module above.

For more options, visit https://groups.google.com/groups/opt_out.



--
Josh Cooper
Reply all
Reply to author
Forward
0 new messages