0

I am trying to invoke a PowerShell command from Puppet. The issue is even if the PowerShell command is incorrectly supplied (in this case, iisres instead of iisreset), Puppet shows successful run as shown below:

Notice: /Stage[main]/Common::Iis::Iis_reset/Utils::Powershell_test[Restart IIS service]/Exec[Running command invoke-command -scriptblock {iisres} -verbose on agent]/returns: executed successfully

Few days ago, i had asked a similar question and the suggestion made in that post was working fine till i came across this issue. Here's the script:

[root@pe-server] cat iis_reset.pp
class common::iis::iis_reset {
    utils::powershell_test { 'Restart IIS service':
        command => 'invoke-command -scriptblock {iisres} -verbose',
    }
}


[root@pe-server] powershell_test.pp
define utils::powershell_test (String $command) {
    $powershell = 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoProfile -NoLogo -NonInteractive'

    exec { "Running command $command on agent":
        command => "$powershell -Command \"& {$command; exit (1 - [int]$?)}\"",
        logoutput => 'on_failure',
    }
}

How to fix such issues for good?

Note: I was actually using the Powershell provider by Puppet earlier for all exec commands until i faced this issue.

Community
  • 1
  • 1
Technext
  • 7,887
  • 9
  • 48
  • 76
  • try/catch and exit code approach should help you out I believe. I am not sure if that is exactly what you are looking for or not – Ranadip Dutta Mar 16 '17 at 15:01
  • I'm open to suggestions but i would try to avoid try/catch unless there's no other option left. – Technext Mar 16 '17 at 15:09
  • Could you please tell the reason for the same? Is there any context on the above point? because catching the exceptions is the best way I felt in your case. – Ranadip Dutta Mar 16 '17 at 15:13
  • There are many ways to approach a given problem. I'm not averse to using what you're suggesting but who knows, may be just a tweak in my syntax might help me fix the issue, for good. – Technext Mar 16 '17 at 15:21
  • 1
    I don't know anything about Puppet but why are you potentially returning a negative exit code? Also I think your issue is that Invoke-Command is running properly so the last command result is ok. Looking to see how you properly pass that to the calling computer. – Matt Mar 16 '17 at 15:43
  • http://stackoverflow.com/questions/1210942/catching-return-code-of-a-command-with-invoke-command-powershell-2 and http://stackoverflow.com/questions/8549184/how-to-capture-the-return-value-of-a-scriptblock-invoked-with-powershells-invok – Matt Mar 16 '17 at 15:44
  • @Matt: I'm using `exit (1 - [int]$?)` from the [link](http://stackoverflow.com/questions/42484043/capturing-output-error-when-invoking-powershell-script) i posted above in my question. Based on the [explanation](http://stackoverflow.com/questions/42484043/capturing-output-error-when-invoking-powershell-script#comment72194445_42485152), i understood that if the command will fail, it will return FALSE, which when casted to integer will return 0, and thus, to get the right exit code, we will be subtracting it from 1. Please correct me if i misunderstood that explanation. – Technext Mar 16 '17 at 16:20
  • @Technext the explanation is correct. Just never done that before so it struck me as odd at first. – Matt Mar 16 '17 at 16:26
  • @Matt: BTW, i tried using just `exit ([int]$?)` and there's an interesting observation: Puppet _correctly_ _mentions_ about the non-existent command `iisres`. However, if i use the correct command i.e., `iisreset`, it fails and that it seems is because it worked properly and returned TRUE, which when got converted to integer and exited with failure. – Technext Mar 16 '17 at 16:29

0 Answers0