Kaseya Community

Question on Write text to file in agent procedures

  • I'm working on a script where I need to write out a dynamic XML file with some of the contents based on a variable entered at procedure runtime...

    At first I was thinking it would be fairly easy using the "Write Text To File" functions to build my XML.  However in actual practice I can't seem to able to utilize the < or > characters in the text to write to the file, no matter what I do.  I understand that those are used by global variables, so you have to escape them when you want to use the "real" character by typing them twice, however even with doing that the resulting text file just seems to completely skip any lines where I attempted to write out any < or > characters.  I even modified the script to use &lt; and &gt; thinking I could then write some kind of script to find and replace, or convert from HTML entities, but it still skips those lines...

    Have I run across a bug, or is there something I'm missing.

     

  • You could declare both characters as global variables and use those instead?

    Or use the step "Get Variable - Universal - Create" and make "<" universal 1 and ">" universal 2.

  • I think if you want to out put one ">" then you have to wright TWO of them >> in kaseya and the result will be one in your txt file

  • I haven't had a chance to try SMason's suggestion yet, but Michael, as I stated in my original post, I knew about that issue, and by putting two of them I get rid of an error stating it can't find the variable, but it still writes *nothing* to the text file.  Thus why I believe there to be a bug there....

  • I did just test SMason's suggestions and that didn't work either, though it did lead me to what I think is the actual issue behind this.  I don't believe that it will ever be possible to use < or > in the write text to file because of the way that they are doing this... The problem is what they appear to be doing in order to write the text to the file is using the 'Echo' command line... So if you try to put > or >> or << or << that is actually telling "echo" where to get it's text from or where to write it to, and causing issues.  When I tested using the universal variables, I ended up with a line in my text file from where I tried to use those variables of "ECHO is on"  :)

  • Not sure if you can make this work for your specific example but this is the syntax for writing ascii characters using the actual ascii value

    i.e. if you write this line to a text file you get the following result in the actual file

    Test text sent to file  "This is the test text "

    Will appear in the text file as

    Test text sent to file  "This is the test text"

    i.e. it converts the to " to a Quote

    So may be able to play around with other ascii values and get all sorts of characters into your text files

  • Doh .. it did the actual conversion when I pasted the txt above

    My example was

    Test text sent to file  & # 3 4 ;  This is the test text & # 3 4 ;

    Try it but take the spaces out between the & # 3 4 ;

  • Another option to look into;

    Your XML should in theory have a specific structure so you could template the XML file and add place holders in your XML template where you want to put the data values in. Then you copy this XML template to the target machine in your Agent Procedure and use another script (Batch/VBS/Powershell) to replace the place holders with the values your agent procedure generates.

  • Paul,

    Tried your suggestion, but I get basically the exact same behaviour.  Like I said before, I believe the underlying issue is with how they are actually doing the "Write text to file" on the PC, it is my belief they are using the "Echo" command to actually output your text to the file, which interprets the > and < characters as "redirection" characters on the command prompt.

    HardKnoX... That's more or less what I'm doing now, though to avoid having to deal with VBS, or powershell, I'm basically uploading two files which I then combine with my variable in the middle into one big file.  I was just hoping to avoid that by utilizing the "Write text to file", since overall it's a fairly small amount of text that I'm having to dynamically build..

    Now then for the really good news, as I was typing this response, something finally occurred to me.  As I mentioned before, I beleive they are using the "Echo" command built into windows to create the text file, Given that I realized... Huh, how would I manage to put the > symbol into a file if I was using echo from the command line?... A simple google search and a couple of quick tests later, and I have the answer to my overall question..

    If you want to use > or <, or even for that matter & | ^ or \ characters in the "Write text to file" command you will have to prefix them with the ^ symbol, as that is the "Escape character" for the commandline such as "Echo" uses. 

    So for a quick replacment chart (taking into account Kaseya's escape as well as the commandline prompt...")

    Character to insert Actual text you use in Kaseya's proceedure
    > ^>>
    < ^<<
    \ ^\
    & ^&
    | ^|
    ^ ^^

    Thanks to everyone for the tips, and helping to lead me down the right path anyway.

  • Cool. Thanks for sharing that.

  • I assume your solution is better but there is also the SETX command... to actually create the environment variable on the machine and then just use %variable% and it should work.... you could then blank and remove it afterwards.  Never done this but looks like it could be done.

  • THANK YOU!!!!!