On Thu, Aug 27, 2015 at 9:26 AM, Peter Kristolaitis <alte...@alter3d.ca>
wrote:

> We solve this issue by doing (kind of ugly) stuff like this in our modules:
>
> unless  =>  'if ( ! ( Get-Service mcollectived ) ) { exit 1 }',
>
> If you don't like that syntax, you may be able to use the $? or
> $LastExitCode variables that get set by PowerShell (I haven't tested this,
> however).  Both of those have non-obvious gotchas.  A good writeup on error
> handling in PS (not Puppet-specific) is here:
> http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/12/powershell-error-handling-and-why-you-should-care.aspx
>

Another great writeup for PowerShell confusion is
http://joshua.poehls.me/2012/powershell-script-module-boilerplate/

The tl;dr of PowerShell scripts is that parser errors (incorrect
PowerShell) will always exit with 0, so test your scripts with PowerShell
and look at the last exit code.

Another is that even setting $ErrorActionPreference = 'Stop' (to catch
non-terminating errors) doesn't appear to catch external command errors
(versus powershell function calls):

[image: Inline image 1]

PowerShell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass
-Command "$ErrorActionPreference = 'Stop';reg /query bob; Write-Host 'yo'"


>
>
> - Peter
>
>
>
> On 08/27/2015 08:38 AM, Thomas Bartlett wrote:
>
> Hi Guys,
>
> So I've been working with puppet on windows and I think my approach is all
> wrong. I've been using the puppetlabs/powershell module to run commands,
> however I'm having difficulty with exit codes. Primarily puppet expects
> exit codes to denote success/failure, whereas powershell is returning
> objects (and giving a 0 exit code regardless of result).
>
> I'm automating the install of old bits of software, so getting meaningful
> answers out of the installers is pretty difficult, this means that the
> scripts are a bit ugly and not very idempotent. Typically I have to check a
> log file to find out if the install actually worked.
>
> Are there any examples out there of windows puppet automation that makes
> heavy use of the powershell module?
>
> Cheers,
>
> Tom
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/7b4f140b-05fd-4477-b8bc-62e14f54da70%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/7b4f140b-05fd-4477-b8bc-62e14f54da70%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/55DF1E29.40409%40alter3d.ca
> <https://groups.google.com/d/msgid/puppet-users/55DF1E29.40409%40alter3d.ca?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Rob Reynolds
Developer, Puppet Labs

*PuppetConf 2015 <http://2015.puppetconf.com/> is coming to Portland,
Oregon! Join us October 5-9.*
*Register now to take advantage of the Final Countdown discount
<https://www.eventbrite.com/e/puppetconf-2015-october-5-9-register-now-its-the-final-countdown-tickets-13115894995?discount=FinalCountdown>
*
*—**save $149!*

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CAMJiBK7WtTGh5HHoCU4rBNLrVWvqsignfdzhooe52mQ_VH_-xg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to