Deployment Solution

 View Only
Expand all | Collapse all

Script Returns Errorlevel 1 but runs fine locally

  • 1.  Script Returns Errorlevel 1 but runs fine locally

    Posted May 08, 2017 12:57 AM

    Hi everyone,

     

    i have a little Problem with our deployment solution.

    I wrote a little batch script which should delete temp files and delete a registry entry if it exists.

    Unfortunately it returns errorlevel 1 and therefore scripts which should run after that script dont start since it fails.

    If i run the script right on my computer with the same user used for Altiris it returns errorlevel 0

    If i run it as Script from the Altiris Console i receive Errorlevel 1.

     

    Any Idea why this could be happening?

     

    Kind Regards,

     

    Patrick

     

    @echo off & setlocal enabledelayedexpansion
    
    	reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer\Logging"
    	if %errorlevel%==1 ( 
    	goto end
    	) else (
    	reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Installer\Logging" /f
    	goto end
    	)
    
    	:end
    	DEL "C:\drv\*" /F /S /Q
    	DEL "C:\xpinst\*" /F /S /Q
    	DEL "C:\Windows\Temp\*" /F /S /Q
    	DEL "C:\temp\*" /F /S /Q
    	DEL "C:\ProgramData\Symantec\Patch\*" /F /S /Q
    	FOR /D %%p IN ("C:\drv\*.*") DO rmdir "%%p" /s /q
    	FOR /D %%p IN ("C:\xpinst\*.*") DO rmdir "%%p" /s /q
    	FOR /D %%p IN ("C:\Windows\Temp\*.*") DO rmdir "%%p" /s /q
    	FOR /D %%p IN ("C:\temp\*.*") DO rmdir "%%p" /s /q
    	FOR /D %%p IN ("C:\ProgramData\Symantec\Patch\*.*") DO rmdir "%%p" /s /q
    	exit

     



  • 2.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 08, 2017 12:34 PM

    How are you getting the script to run as a user?



  • 3.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 09, 2017 03:33 AM

    Hi Andy,

    The script is run from the cmd via call with the AD User used for Altiris.

     

    Kind Regards,

     

    Patrick



  • 4.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 09, 2017 05:52 AM

    When you say "the AD User used for Altiris" do you mean the account that the Services run under? Because that account is not used to run scripts, by default scripts are run by the local Symantec Management Agent in the local System Account context.



  • 5.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 09, 2017 06:24 AM

    Hi Andy,

    ah i see. Is there a way i could get logs or something so that i could see what exactly caused the script to fail?

    I tried different settings even setting the errorlevel manually to 0 with VERIFY > nul or set %errorlevel% = 0 but the script still fails.

    The only Status i can see in the Altiris Console is Status: Failed Return Code: 1

     

    Kind Regards,

    Patrick



  • 6.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 09, 2017 07:08 AM

    Try to remove the "exit" in the end.

    I remember that this was killing the wrapping script once upon a time - making the agent think the task failed.



  • 7.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 09, 2017 07:58 AM

    Is any of the script actually running and deleting anything?



  • 8.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 09, 2017 10:14 AM

    Hi Mistral,

     

    tried that. Unfortunately with no success.



  • 9.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 09, 2017 10:16 AM

    At the moment through Altiris not.

    As soon as i run it from cmd the script works though.

    So i assume either the System Account cant execute it or Altiris does not execute the code written that way.



  • 10.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 09, 2017 10:39 AM

    Is Altiris running other scripts OK? Try dir > dir.txt or similar.



  • 11.  RE: Script Returns Errorlevel 1 but runs fine locally
    Best Answer

    Trusted Advisor
    Posted May 10, 2017 05:19 AM

    To confirm the context which the script is running under, you could create a simple Run Script task as below:

    Create a Run Script task, select the Script Type as Command Script and use the command "whoami". 

    Click Advanced and tick the option for "Save script output with task status". This will provide you with the output of the "whoami" command. 

    WhoAmI.png

    After running it, you should then see something like the below:

    TaskResults.png

    If you haven't already, it might be worth using that tick box for saving the output on the task you created the post about, it could provide more information that might be valuable in resolving the issue.

    Thanks



  • 12.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 10, 2017 05:30 AM

    Hi Andy,

     

    yes other scripts are running fine.

    Thanks to Sam`s Answer i could narrow it down a bit.

    After i checked "Save script output with task status" i rerun the script.

    It seems like the script deletes a few files in C:\Windows\Temp\ and after that it cant find itself anymore.

    So it seems the Script deletes itself.

    Im gonna try to work around that. Thanks for your help anyone. Gonna keep you updated.



  • 13.  RE: Script Returns Errorlevel 1 but runs fine locally
    Best Answer

    Posted May 10, 2017 05:34 AM

    Just put the c:\windows\temp line last. Using "Start" before the last del may give you a '0' exit code too.



  • 14.  RE: Script Returns Errorlevel 1 but runs fine locally

    Trusted Advisor
    Posted May 10, 2017 05:51 AM

    Perfect, glad you were able to narrow it down. 

    You could also add: "echo %cd%" to see the current directory your script is running from. I think it would be C:\Windows\System32\
    That way you can match up that location and the location you are deleting files from to see if that's your issue.

    Thanks

     



  • 15.  RE: Script Returns Errorlevel 1 but runs fine locally

    Posted May 10, 2017 06:01 AM

    The working directory is not important .. it's the scripts directory (even if they might be the same)

    echo %~dp0

    shows the path of the script.

    I assume its C:\Windows\Temp and this is where you delete yourself.



  • 16.  RE: Script Returns Errorlevel 1 but runs fine locally
    Best Answer

    Posted May 10, 2017 06:48 AM

    Hi Mistral,

    yes you were right.

    The Script itself was located in C:\Windows\Temp\

     

    I fixed the script now and it works flawlessly.

    Thanks for your input guys.

     

    Here is the updated Script.

    @echo off & setlocal enabledelayedexpansion
    
    	DEL "C:\drv\*" /F /S /Q
    	DEL "C:\xpinst\*" /F /S /Q
    	DEL "C:\temp\*" /F /S /Q
    	DEL "C:\ProgramData\Symantec\Patch\*" /F /S /Q
            FOR /D %%p IN ("C:\Windows\Temp\*") DO if not %%p == "AltirisScript*.cmd" DEL %%p /F /S /Q
    	FOR /D %%p IN ("C:\drv\*.*") DO rmdir "%%p" /s /q
    	FOR /D %%p IN ("C:\xpinst\*.*") DO rmdir "%%p" /s /q
    	FOR /D %%p IN ("C:\temp\*.*") DO rmdir "%%p" /s /q
    	FOR /D %%p IN ("C:\ProgramData\Symantec\Patch\*.*") DO rmdir "%%p" /s /q
    	FOR /D %%p IN ("C:\Windows\Temp\*.*") DO rmdir "%%p" /s /q