Kaseya Community

Writing multiline variable to text file

  • Hey,

    Sometime I have an eventlog entry which is a multiline event log entry. 

    I then wish to write the #ed# variable to a file. However if this is a multiline variable, Kaseya simple doesn't do anything.

    Using the variable in an email is however no problem.

    How can I get this variable to a file on my disk?

    Thijs

  • Write file, or try a simple "echo #ed# >> file.txt" in an execute command

  • Hi Dan,

    tried this, but this doesn't work for variables which have multiple lines in them.

    it does work however for single line variables.

    in the mean time I have found a workaround. I'm able to write the variable to the registry and then read it from a vbscript and then write it to the a file. Bit of a workaround though...

    Thijs

  • sounds interesting, can you submit your workaround, sounds like it might be useful

  • Hi,

    the workaround involves writing the variable to the a Mult String reg Value (a REG_MULTI_SZ) and afterwords you run a vbs script which read the reg key, and writes it to a file on the machine.

    I'll try to put the code later on, not at the office now.

    Thijs

  • Got an example attached here to try.

    Write Multi-Line Variable to File (Agent Procedure, Unzip and import XML)

     

     



    [edited by: Ben at 1:32 PM (GMT -8) on 12-5-2011] .
  • There is a blog post on my blog about this. If you have questions, just ask.

  • Forgot to provide the link: thijsdeschepper.be

  • The idea of using the REG-MULTI_SZ is fabulous, but there is no need for a vbscript:

    You can do everything you need from the command prompt.

    All you need is two Unix tools: sed.exe and wc.exe

    So here we go: I want from a list of patches recently approved to log ONE Agent Procedure Logn entry per patch. The tricky part is that you never know how many patch...

    The said list of patches recently approved is available on the "New Patch Available" alert under the #pl# variable

    1) Set your registry value. In my example " HKEY_LOCAL_MACHINE\SOFTWARE\HTG\PatchMgmt\NewPatchAvailable" will contain #pl#

    for instance:

    KB2817396 for Office 2010

    KB2687567 for Office 2010

    KB2775360 for Office 2010

    KB2760601 for Office 2010

    KB2843630 for Windows 7

    KB2837595 for Office 2010

    KB2919469 for Windows 7

    KB2830477 for Windows 7

    KB2817369 for Office 2010

    KB2837583 for Office 2010

    2) Execute Shell command as SYSTEM:

      reg query HKLM\Software\HTG\PatchMgmt /v NewPatchAvailable | #AgentTemp#\sed.exe -e "/^HKEY*/ d" -e "s/NewPatchAvailable//" -e "s/REG_MULTI_SZ//" -e "s/^[ ]*//" -e "/^$/ d" >> #AgentTemp#\NewPatchAvailable.txt

      a little explanation on the various sed.exe work:

         -e "/^HKEY*/ d" => will delete the line containing the key containing the value (don't need that)

         -e "s/NewPatchAvailable//" => will erase "NewPatchAvailable" from the line.

         -e "s/REG_MULTI_SZ//" => will erase "REG_MULTI_SZ" from the line.

         -e "s/^[ ]*//" => will remove any leading space

         -e "/^$/ d" => will delete any empty line

    you now have one file containing you list of patches: #AgentTemp#\NewPatchAvailable.txt

    But how do we log individually each patch ?

    3) count your patches

      Execute Shell command as SYSTEM:

      #AgentTemp#\wc.exe -l #AgentTemp#\NewPatchAvailable.txt | #AgentTemp#\sed.exe -e "s/^[ \t]*//" >> #AgentTemp#\NewPatchAvailable.Number.tmp

      this #AgentTemp#\NewPatchAvailable.Number.tmp file will contain in my example:

      10 C:\_HTG_Work\NewPatchAvailable.txt

      Execute Shell command as SYSTEM:

      FOR /F "tokens=1,*" %A in (#AgentTemp#\NewPatchAvailable.Number.tmp) do echo %A | #AgentTemp#\sed.exe -e "s/ //g" >> #AgentTemp#\NewPatchAvailable.Number.txt

      this #AgentTemp#\NewPatchAvailable.Number.txt file will contain only the number of patches: 10

    4) split your file containing the list of patches in on file per patch:

      Execute Shell command as SYSTEM:

      FOR /F "tokens=1* delims=:" %a IN ('FINDSTR /n "^" "#AgentTemp#\NewPatchAvailable.txt"') DO FOR /F "tokens=*" %c IN ("#GLOBAL:prefix# %~b") DO >>"#AgentTemp#\NewPatchAvailable.%a.txt" ECHO(%c

      and you will end up with 10 files: #AgentTemp#\NewPatchAvailable.1.txt through #AgentTemp#\NewPatchAvailable.10.txt

    5) finally call another procedure to log the files one by one... see attached screenshot.

  • Just to chime in, in case anyone from Kaseya is listening :).. If I could figure out a good way to do it in the agent procedure, I would but it would be much easier for Kaseya to modify their "writeTextToFile()" function.

    The "correct" way to do this without involving additional utilities, etc, would be to do a search and replace for any "newline" characters in the text you are trying to write with the proper escape sequence for echo to actually write the multiline.

    For an example  Say that you want to write the following two lines to a text file using echo...

    I like cheese
    No Really I REALLY like cheese

    I can do that from within a batch file using echo with the following syntax

    @echo I like cheese^

    No Really I REALLY like cheese > myfile.txt

    Basically what you are doing is using the dos escape character ^  followed by two newlines to get one actual newline in the results of echo... So a simple search and replace to replace every newline in your variable with a ^ and two newlines woudl produce the requested results.   And if Kaseya were to modify their writeTextToFile function to do this, then it would work as expected for both multiline and single line variables, as with a single line there would simply be no newlines to replace.