Kaseya Community

Command line defrag batch file that won't execute properly from a Kaseya procedure

This question has suggested answer(s)

Hi All,

I've got a windows batch file that runs fine from the command line, but when I try to execute it from a Kaseya procedure, it doesn't do anything except spawn some conhost.exe processes.  I've tried making it both a .CMD and a .BAT file.  I've tried impersonateuser and setcredential.  I've added pauses to the procedure without success.  I've used similar scripts to perform a variety of functions, but this one just won't work under Windows 7 (I thought I had it working earlier in the month on XP and 2003, but haven't bothered testing those in the last day or two as it is Windows 7 I'm primarily concerned with).

Anyone have any ideas what is going wrong?

Batch file is shown below:

time /t > defrag.log

for /f "tokens=* delims=Drives: " %%a in ('fsutil fsinfo drives') do set str=%%a
FOR /f "tokens=1 delims=\ " %%g in ("%str%") do defrag %%g -a >> defrag.log
FOR /f "tokens=2 delims=\ " %%g in ("%str%") do defrag %%g -a >> defrag.log
FOR /f "tokens=3 delims=\ " %%g in ("%str%") do defrag %%g -a >> defrag.log
FOR /f "tokens=4 delims=\ " %%g in ("%str%") do defrag %%g -a >> defrag.log
FOR /f "tokens=5 delims=\ " %%g in ("%str%") do defrag %%g -a >> defrag.log
FOR /f "tokens=6 delims=\ " %%g in ("%str%") do defrag %%g -a >> defrag.log
FOR /f "tokens=7 delims=\ " %%g in ("%str%") do defrag %%g -a >> defrag.log
FOR /f "tokens=8 delims=\ " %%g in ("%str%") do defrag %%g -a >> defrag.log
FOR /f "tokens=9 delims=\ " %%g in ("%str%") do defrag %%g -a >> defrag.log

Abnd the XML for the procedure is here:  2086.Procedure_Defrag_All_Drives_P1_-_analyse.xml

By the way, if anyone wants to use this, feel free.  Assuming you can make it work!

All Replies
  • Are you using Kaseya Version 5 or 6?

    Try using ExecuteShellCommand to execute the batch file instead. Use a Test File If Statement to check if the log file exists and write procedure log entries for if the log file does not exist.

    If you do use the ExecuteShellCommand you will need to use it twice, once for 32bit OS's and once for 64Bit OS's, using the getOS If Statement to detect the OS type. Make sure to set correct Execute as System for each ExecuteShellCommand.

    If you really want to use the ExecuteFile command then use %COMSPEC% as the file name to execute and set the arguments "/C #ktemp#\defrag1.bat" (no quotes). I would also make it wait to complete before it continues, but you might get a  black shell window appear on each machine you do this on which is why I recommend using the ExecuteShellCommand instead.



    typo
    [edited by: HardKnoX at 2:24 PM (GMT -8) on Nov 24, 2013]
  • Thanks HardKnoX.  I tried your suggestions but didn't have much luck.  In the end, I decided to do away with the .CMD file and just make a more complicated Kaseya script.  Seems to work OK, but still haven't fully tested it in all environments.  Feel free to use it if you like!

    Procedure Defrag All Drives P1 - analyse.xml