Kaseya Community

Batch File Won't Create Files When Executed Via Kaseya

  • Hello...

    Recently our company wrote a script which copies & then executes a batch file to machines.

    There is a place in the batch file where a text file is created to log some info:

    i.e. Output info > SomeFile.txt

    The odd thing is that when we run the script from Kaseya using either the "Execute File" or simply calling it to run from a "Execute Command Shell Statement" it won't create the files. No error message appears, the batch file does execute but hangs. But if you simply run the file from your machine, bypassing Kaseya, it works fine.

    Now, we've tried running it as a user & as System & it fails either way. It seems like it would be a permissions issue in that it fails to have the ability to create a file in said directory. But why would this be considering running the file "as user" via Kaseya fails but then running it as the same user locally succeeds.

    Legacy Forum Name: Batch File Won't Create Files When Executed Via Kaseya,
    Legacy Posted By Username: JohnS
  • I've had similar issues with running DSQuery from a command line recently. Can we have a bit more information on the command that is meant to pipe the information out?

    Thanks

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: LANWorx
  • To test this I usually wrap whatever I'm trying to do in quotes and then echo "quoted text" >> #tempdir#\test.txt

    See what's in test.txt when you run it and you'll see where Kaseya's hanging up. Unless the problem is you can't echo "test" >> #tempdir#\test.txt

    Do you have quotation marks or double quotes in your shell command? There's some interesting things that happen when you do that. Export your script and I'll see if I can help.

    edit: And upon rereading it looks like you're doing this from a batch file you pushed and not from the execute shell command. If you use runas to run it as the user on the workstation does the batch file work?

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: Intech-Jason
  • ::Checks all drives, tested and works on flash drives and hard disks.
    ::Network drives to do get checked or create errors, chkdsk cannot check network drives, only physical
    ::Works on OS's that have fsutil, including windows xp and server 2003 and above


    SETLOCAL ENABLEEXTENSIONS
    SETLOCAL ENABLEDELAYEDEXPANSION
    ::Makes a list of all the drive letters used on the system
    SET Drives=

    FOR /F "skip=1 tokens=1,2 delims=:\ " %%A IN ('FSUTIL FSINFO DRIVES ^| MORE
    /E /T0') DO (
    IF "%%B"=="" (
    SET Drives=!Drives! %%A
    ) ELSE (
    SET Drives=%%B
    )
    )

    ::Checks every drive through normal chkdsk and makes report of output from chkdsk
    FOR %%A in (%Drives%) DO chkdsk %%A: > ChkDskReport%%A.txt
    ::echo CHKDSK cannot continue in read-only mode. > ChkDskReportC.txt
    ::For all the drives, check to see if any errors occured / needs to chkdsk /f
    FOR %%A in (%Drives%) DO CALL :Check %%A
    ::Final Report, condenses any error reports into one
    FOR %%A IN (%Drives%) DO IF EXIST ChkDskReport%%A.txt CALL :Report %%A

    GOTO end

    ::checks to see if errors occured, if so acts on it
    :Check

    ::Searches for string concerning errors in report
    FOR /f "tokens=*" %%a IN ('find "CHKDSK cannot continue in read-only mode." ChkDskReport%1.txt') DO SET test=%%a
    ::if file has the string concerning errors, report on it or do other action, right now it echo's
    IF ("%test%") neq ("---------- CHKDSKREPORT%1.TXT") (
    ECHO run %test% Drive %1
    ) ELSE (
    ::if report has no errors, or is about drive that is not physical, delete report
    DEL ChkDskReport%1.txt
    )

    ::is there a better way, so that GOTO's don't have to be used?
    GOTO end

    ::Makes final report file, with Date, Drive Letter, and chkdsk report, ends with newline.
    :Report

    DATE /t >> ChkDskReport.txt
    ECHO Drive %1 >> ChkDskReport.txt
    TYPE ChkDskReport%1.txt >> ChkDskReport.txt
    echo. >> ChkDskReport.txt
    DEL ChkDskReport%1.txt


    ::end of file
    :end


    Here's the script. It was still in a testing phase, not necessarily ready for production.

    When I run the batch file from my system it works fine.


    Running it through Kaseya via "Execute Shell Command" as System: - Script executes but fails to create .txt files

    Running it through Kaseya via "Execute Shell Command" as User: - Script executes but fails to create .txt files

    Running it through Kaseya via "Execute File" as System : - Script executes but fails to create .txt files


    Running it through Kaseya via "Execute File" as User : - Script executes but fails to create .txt files

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: JohnS
  • This is why I stopped doing batch files ...

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: Lmhansen
  • Hey John, did you ever get this working? Looks like it could be handy and I've not found anything like it yet. Would love to get your feedback.

    RJ

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: rbjsea
  • were having the same issue with a dsquerie script, can anyone make a suggestion as to what might be the issue?

    thanks

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: steve b
  • for what its worth, if you remove the 2 "DEL ChkDskReport%1.txt" the text files are created on the desktop...

    I ran the batch file as :
    Execute File/Execute as user & continue immediately...

    I'd like to see the rest of the script that you've created.

    Or, instead of deleting the files that are created by calling DEL in your batch file, have a set path to where you want the text files written, then use the script to delete them.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: thirteentwenty
  • Try running a search on the computer for the files your expecting. I was trying to dump some files in the Kaseya temp folder, and they ended up in the users temp folder.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: epetro@autobase.net
  • I was able to get the batch file to work correctly by running the file under execute shell command. This was the last step - copied the file to the destination, then ran the file under shell command and the information was written to a file in a shared location. I hope this helps.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: RichC
  • RichC
    I was able to get the batch file to work correctly by running the file under execute shell command. This was the last step - copied the file to the destination, then ran the file under shell command and the information was written to a file in a shared location. I hope this helps.


    Could someone enlighten me on this? I am trying to execute a batch file that simply writes the current date to a text file. But when executed from a Kaseya script, it doesn't write to the text file, only if you run the Batch file from the local machine. Our Batch File:
    echo %time% %date%>test.txt

    That's it, it writes the date to "test.txt" within the same directory as the batch file. Thank you.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: brian.mock
  • I just answered my own question by placing
    cd c:\the batch file location

    in front of the part that writes the date. Now it writes to the file.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: brian.mock