Kaseya Community

Variables created inside IF statement not available outside of it?

This question is answered

After furiously testing a procedure over the course of several hours with absolutely no sense being made, I whittled it down to the most basic sense. All I can figure is that variables created inside of an IF statement are not available outside of it. Can anyone confirm this? Here is a sample (with wrong formatting) so you see what I mean:

IF user logged in
  -  Get variable #filecontent# from C:\temp\file.txt
  -  Write Procedure Log Entry: File Content in IF: #filecontent#
Write Procedure Log Entry: File Content outside IF = #filecontent#

The 3rd line will work and the 4th line will fail. If I remove the 3rd line, the 4th line still fails.

Now, don't crucify me because it is easy to say with the example above to "just put the 4th line under the IF statement". The end procedure is far more complex than that, and I simply cannot do that and maintain a clean-working script. All I really want some confirmation on is if I'm delusional or if variables created under IF statements are available outside them. It seems they are not, so my next question is "why"?

Thanks,
Matt

 

Verified Answer
  • This is by design. Nested conditional statements are basically separate scripts under the covers. You will need to use the Global Variable steps instead. Or just use temporary text files in the agent working directory (which is all global variables are anyway).

  • So did you work out how to use the global variables, matts?

    If not, all you need to do is change the variable above from filecontent to global:filecontent and then refer to it as #global:filecontent# later on in your script.

All Replies
  • This is by design. Nested conditional statements are basically separate scripts under the covers. You will need to use the Global Variable steps instead. Or just use temporary text files in the agent working directory (which is all global variables are anyway).

  • Well, it isn't what I wanted to hear (the "by design" part), but now I know the limitation I just need to work around it.

  • So did you work out how to use the global variables, matts?

    If not, all you need to do is change the variable above from filecontent to global:filecontent and then refer to it as #global:filecontent# later on in your script.

  • Also, I am sure you worked out that in the get variable step you don't use the hashes.

  • It can actually be quite useful to have variables that does not work in sibling/peer if statements, I make use of this feature all of the time.

    Since you are not familiar with global variables something to note is that they don't work outside the procedure either so if you use the execute procedure step and want to pass global variables to it from the parent/main procedure you need to read the global variables in to the normal procedure variables first and then use these new procedure variables in your child/sub procedure.

  • Well this bit me in the behind again today. I only ended up here because I came to the forum searching for the same thing I asked before. Spent two hours yelling at my monitor in jest. I guess I'll try the global variables...

  • Tested using the variable as a "global" one by simply prefixing with "global:" and it worked. Thanks ghettomaster.

  • Basically if you want to use a variable throughout the script in and out of IF simply rename your variable "global:<VARIABLE NAME>"