Kaseya Community

Variable use

  • Hi guys,

    Im trying to set some basic procedures but im not able to finish them correctly. I hope you can help me:

    My procedure is simple, i want to execute  a CMD file in c:\ path giving 2 variables, example:

    c:\file.cmd 25 109

    This procedure run correctly if i execute it manually. So my kaseya procedure is:

    WriteFile("file.cmd","c:\file.cmd","all OS","halt on fail")

    GetVariable("prompt when procedure is scheduled","Enter variable1","variable1","all OS","halt on fail")

    GetVariable("prompt when procedure is scheduled","Enter variable2","variable2","all OS","halt on fail")

    a) ExecuteFile("c:\file.cmd","#variable1# #variable2#","execute as user and continue","all OS","HONF")

    ZipFiles...

    GetFile...

    I've tried also b) ExecuteShellCommand("c:\file.cmd #variable1# #variable2#","All OS","HONF") instead of ExecuteFile.

    Kaseya runs both successfully but the script is not recieving those 2 variables. I mean:

    Both cases, what i want to execute remotely:

    c:\file.cmd 25 102

    What is executing right now:

    a) c:\file.cmd

    b) C:\file.cmd #variable1# #variable# (literally)

    Any idea how to make those lines to execute correctly?



    more fixes
    [edited by: Zlatan at 1:53 AM (GMT -7) on Jun 2, 2015]
  • Zlatan,

    I call lots of BAT/CMD files, but have worked out a way that runs consistently. One rule in my org is that the first argument to any external script is the path to the KWorking folder. As such, here's a command that calls MyScript.BAT and passes 3 args:

    executeShellCommand("cmd.exe /c #vAgentConfiguration.AgentTempDir#\myscript.bat #vAgentConfiguration.AgentTempDir# #Var1# #Var2#", "Execute as System", "All Windows Operating Systems", "Continue on Fail")

    Note that it calls the command interpreter (CMD.EXE) with the /c argument, then the MyScript.BAT from the KWorking directory. The next arg is the KWorking directory path, followed by the two arguments used by the bat file.

    My bat files look like this:

    echo Off

    REM Response Utility Script - First Arg is required and must point to KWorking dir

    REM Insure that KWorkDir env var is defined

    If [%KWorkDir%]==[] Set KWorkDir=%1

    If [%KWorkDir%]==[] (

    Echo Can't run - KWorkDir variable is not set! >>C:\USP_ERR.log

    Goto :EOF

    )

    REM Discard the first arg

    Shift

    %KWorkDir%\command.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

    ------------------

    The BAT file sets the KWorkDir variable to the first arg passed to the script, then throws that positional arg away. I can now reference the KWorking folder using the same script no matter what it is defined to be.

    Note how the bat file uses the positional args - this is important, and I point it out because you didn't show your command file content.

    Regards,

    Glenn

  • Don't know about you guys but seeing random files left in the root of the C: Drive is one of my pet peeves.

    There are many good reasons not to do it.

    gbarnas is totally right in that you should specify the path as Kaseya by default uses the C:\Windows\system32 or the agent install path depending on the version of Kaseya that you are running.

  • @

    Don't worry that was just an example to explain myself as clear as possible.

      

    So, ExecuteShellCommand wasn't working cause c:\file.cmd is not a command.

    I've changed my procedure to ExecuteShellCommand("cmd.exe /c c:\file.cmd #var1# #var2# #var3#", "..........")

    and it works!. So simple, tyvm for your help, guys.



    fix replies
    [edited by: Zlatan at 1:15 AM (GMT -7) on Jun 3, 2015]
  • ZLatan

    Glad to hear that it worked!

    HardKnoX,

    I agree with you on the files in the root folder (cringe). I actually have a hierarchy in the KWorking folder for Logs, Scripts, and the like, and that's where all of the production scripts write their logs - %KWORKDIR%\Logs\mylog.log. :) Since this particular script checks for the KWorkDir value to either exist or be specified, it can't find that folder if the root working dir isn't known. My procedure looks for the error file in the root and removes it, and responds with an error if it was found.. It was kind of a Chicken-Egg issue. I suppose I could have used the TEMP folder to write the log, but where would the Kaseya procedure look for it? C:\Temp? C:\Temp\#? C:\Windows\Temp?  There's only one C:\

    (Don't say "The KWorking folder", since the fact that neither the Env Var nor first arg are defined, so we don't know where that is! I also didn't want to assume that the BAT file was written to the KWorking folder.) :D

    Glenn