Video Screencast Help

TECH144662 - Convert cleanup script from batch to vbscript

Created: 26 Aug 2013 | 2 comments

We went ahead and truncated our Task tables per TECH144662.  The only thing I don't like about the clean up script from the tech article is it runs a batch file that will open a command window while running. A colleague of mine who does vbscript attempted to convert it over. Here is the batch file from the tech article and the vb code. The batch file might have been flawed from the beginning because while it works outside of Altiris when run from inside of the console the job fails do to the agent being stopped. When the agent stops, it cancels all tasks it's currently running to restart, therefore, stopping and failing this task as well. The original batch script created a temp bat file and more or less 'queued' it to run after the completion of the task, so the script would be independent of the task itself. The same holds true for the vbscript or so I believe.

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

Cleanup Script - Batch

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

REM Get the Altiris Agent install path

FOR /F "tokens=2*" %%A IN ('REG.EXE QUERY "HKLM\Software\Altiris\Altiris Agent" /V "installdir"') DO SET AgentDir=%%B set tempbat=%temp%\AgentClean.bat"

 

REM Create temporary batch file to execute while the agent restarts echo "%AgentDir%\aexagentutil" /stop > %tempbat% echo rmdir "%AgentDir%\TaskManagement\cache" /s /q >> %tempbat% echo rmdir "%AgentDir%\TaskManagement\status" /s /q >> %tempbat% echo rmdir "%AgentDir%\TaskManagement\statusXml" /s /q >> %tempbat%

REM echo rmdir "%AgentDir%\TaskManagement\lti" /s /q >> %tempbat%      -- remove away the REM, from this line only if you are running on 7.1 and above

echo ping localhost -n 30 >> %tempbat%

echo "%AgentDir%\aexagentutil" /start >> %tempbat% echo exit >> %tempbat% REM Executes temporary batch file start "" /MIN %tempbat%

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

Cleanup Script - VB

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

 

ON ERROR RESUME NEXT

DIM SRCPath, WshShell, fso

Set fso = CreateObject("Scripting.FileSystemObject")

Set WshShell = WScript.CreateObject("WScript.Shell")

SRCPath = WshShell.RegRead("HKLM\Software\Altiris\Altiris Agent\installdir")

Return = WshShell.Run(CHR(34) & SRCPath & "\AeXAgentUtil.exe" & CHR(34) & " /stop", 0 , TRUE)

fso.DeleteFolder(SRCPath & "\TaskManagement\cache") fso.DeleteFolder(SRCPath & "\TaskManagement\status") fso.DeleteFolder(SRCPath & "\TaskManagement\status") fso.DeleteFolder(SRCPath & "\TaskManagement\statusXml") fso.DeleteFolder(SRCPath & "\TaskManagement\lti") WScript.Sleep 5000 Return = WshShell.Run(CHR(34) & SRCPath & "\AeXAgentUtil.exe" & CHR(34) & " /start", 0 , TRUE)

 

If someone could help me with (A) fix the batch file or (B) cleanup the converted code in the vbscript I would appreciate it. Thanks.

 

Operating Systems:

Comments 2 CommentsJump to latest comment

jason.f's picture

It looks like you just didn't have any line breaks in your folder delete section, you also have the 'Status' folder listed twice but that wasn't causing any issues. What helps when troubleshooting is to comment out the "ON ERROR RESUME NETXT" by putting a single quote (`) at the start of the line so it will throw the error and help you find where the problem is. Try this, it worked for me.

 

ON ERROR RESUME NEXT

DIM SRCPath, WshShell, fso

Set fso = CreateObject("Scripting.FileSystemObject")

Set WshShell = WScript.CreateObject("WScript.Shell")

SRCPath = WshShell.RegRead("HKLM\Software\Altiris\Altiris Agent\installdir")

Return = WshShell.Run(CHR(34) & SRCPath & "\AeXAgentUtil.exe" & CHR(34) & " /stop", 0 , TRUE)

fso.DeleteFolder(SRCPath & "\TaskManagement\cache")
fso.DeleteFolder(SRCPath & "\TaskManagement\status")
fso.DeleteFolder(SRCPath & "\TaskManagement\statusXml")
fso.DeleteFolder(SRCPath & "\TaskManagement\lti")
WScript.Sleep 5000
Return = WshShell.Run(CHR(34) & SRCPath & "\AeXAgentUtil.exe" & CHR(34) & " /start", 0 , TRUE)

Briandr88's picture

Hi,

So I packed a modified version of the batch file, a vb file (calls the batch silently), and a "dummy" file (will be used as part of future Managed Software Delivery policy detection rule) into a WinRAR self-extracting EXE. The WinRAR file does a few different things like specifying where the files get extracted to and some other stuff. What I am not sure of is if my If-Else statement below is correct. I realize in a sense I am doing a double check. One with the detection rule in Altiris and also within the script. Overkill yes, but for now I just want to leave it as is. So how does the batch code look? Do I need to change the placement of things?

If [%ERRORLEVEL%] == [0] (
   echo X86
   cd %systemroot%\system32
   If exist "CleanupCompleted.txt" exit
   call :CLEANUP
   call :FILEMOVE1
) ELSE (
   echo AMD64
   cd %systemroot%\sysnative
   If exist "CleanupCompleted.txt" exit
   call :CLEANUP
   call :FILEMOVE2
)

:CLEANUP
REM Get the Altiris Agent install path
FOR /F "tokens=2*" %%A IN ('REG.EXE QUERY "HKLM\Software\Altiris\Altiris Agent" /V "installdir"') DO SET AgentDir=%%B
set tempbat="%temp%\AgentClean.bat"

REM Create temporary batch file to execute while the agent restarts
(
   echo "%AgentDir%\aexagentutil" /stop
   echo rmdir "%AgentDir%\TaskManagement\cache" /s /q
   echo rmdir "%AgentDir%\TaskManagement\status" /s /q
   echo rmdir "%AgentDir%\TaskManagement\statusXml" /s /q
   REM echo rmdir "%AgentDir%\TaskManagement\lti" /s /q
   echo ping localhost -n 30
   echo "%AgentDir%\aexagentutil" /start
   echo exit /b
) > %tempbat%

REM Executes temporary batch file
Call %tempbat%

Set RegVar=HKLM\Hardware\Description\System\CentralProcessor\0

REG.exe Query %RegVar% 2>NUL | find /I /N "x86">NUL

exit /b

:FILEMOVE1
COPY /Y "%~dp0\CleanupCompleted.txt" "%systemroot%\system32\CleanupCompleted.txt"
exit /b

:FILEMOVE2
%systemroot%\sysnative\cmd.exe /c copy /y "%~dp0\CleanupCompleted.txt" "%systemroot%\system32\CleanupCompleted.txt"
exit /b

 

Thoughts or ideas guys? Let me know. Thanks.