Ad Widget

Collapse

How to get exit code from powershell script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • QuentinJ
    Junior Member
    • Aug 2018
    • 2

    #1

    How to get exit code from powershell script

    Hello,

    I am new to Zabbix and so far I have not encountered any major problem.
    However, I now want to use powershell script to get, for example, the number of sessions of people logged on to a certain server.

    The script works great and I get the value that I need, but I would like to get the $lastexitcode to know if my script worked as planned.

    I tried using UserParameter, and it does work but it means that for every server I want to monitor I have to edit the confiig file and that when I will have to change any script I will have to edit every single config file manually. I work on a big infrastructure with close to 100 servers total.

    My question is: Is there a way to get exit codes without using UserParameters?
    Thank you for your time.

    (Ps: sorry for my english)
  • Linwood
    Senior Member
    • Dec 2013
    • 398

    #2
    What is it you plan to do with the error?

    I take a very simple approach -- I return the error instead of the expected value, and the make sure that error triggers something (e.g. a different trigger like "bad config"). In some cases where the return should be numeric, I return the error message as a string, and just let the trigger to go an unsupported state.

    It's not elegant but it's simple.

    Comment

    • QuentinJ
      Junior Member
      • Aug 2018
      • 2

      #3
      Thank you for your answer Linwood,

      I plan on creating a trigger on anything else than exit code "0".
      I get what you are doing, but I can't really understand how you actually use the usefull value, for exemple, the number of sessions logged on a server, the actual information that I want to use in a graph.

      So if my scipt returns a number of people (integer) between 0 and x, how can I return an error message (String) without Zabbix to be unhappy about what I am sending to it?

      Thank you for your time,
      Quentin

      Comment

      • Linwood
        Senior Member
        • Dec 2013
        • 398

        #4
        That's why I said mine was not elegant -- if I'm returning a number and get an error, I return the string. Zabbix is unhappy, it puts the trigger in an unsupported state, which can trigger an alert or you can just periodically check for them.

        Alternative #2, return a number out of the domain -- return -1 (or different negatives for different meanings) and trigger on those differently.

        Alternative #3 (and I have not done this with agent returns so take it as conceptual) is when I want to return two things from one external check, I let the check return one (e.g. with printed output as normal) and send the other(s) back as a zabbix trap with zabbix send. For example, I wrote my own ping check, and it returns min, max, average and status. One of those comes back as an external check, the other three are pushed to zabbix trapper items, so the one check returns four items. You could do the same, let the agent return the return status, and push (via a zend) the other item into a trapper item, effectively separating the status return from the data item. Again, haven't tried that with agent, not sure how a send works from an agent, but I think it can.

        But in my mind largely getting an error on a powershell script implies a programming error. Should not happen at all, if it does it is rare, and I just let it generate a zabbix error and unsupported item and I go fix it. That's ugly and not very user friendly, but since I think of it as a programming error users should not see it anyway, as it is not really actionable for them.

        Comment

        Working...