Saturday, January 12, 2013

Find and delete files on drives - SCCM Script


SCCM is often considered a delivery mechanism using which IT Teams can deliver their intelligence thru software, script etc to clients around, based on what is required by business. Recently our project demanded us to come up with a script that will be deployed thru SCCM to find and delete certain older versions of our LOB apps on each of the local drives on clients. Though I am not an expert at scripting, but do have passion for scripting /automation as they benefit in increased productivity. Ok, this passion drives me to find and build script (from net, sometimes) and, would like to share below what I used for above said requirement just in case if you will find it useful

 I do practice to log suffice info that helps us to troubleshoot in case of any issues, without needing to repro the issue,  including start and end times of script launch, computer name etc. In this case I have saved a copy on network share all actions, so I know in detail what happened on client, as SCCM execution status (mere success or failure)  doesn’t fully provide such info. Also I added few lines to script to force software scan on SCCM clients so that we get latest info on exe files

@ECHO OFF
Echo Starts now on %computername% >> c:\windows\temp\%computername%.log
date /t && time /t >> c:\windows\temp\%computername%.log
wmic path win32_logicaldisk where drivetype=3 get caption | findstr : > c:\windows\temp\drvs.txt
for /f %%v in (c:\windows\temp\drvs.txt) do call :DelFunction %%v
:DelFunction
%1
cd\ >> c:\windows\temp\%computername%.log
Echo Starts now on %computername% and %1 drive >> c:\windows\temp\%computername%.log
date /t && time /t >> c:\windows\temp\%computername%.log
del LOBApps1.exe /s /Q /f >> c:\windows\temp\%computername%.log
del LOBDepend.exe /s /Q /f >> c:\windows\temp\%computername%.log
Echo Ends now on %computername% and %1 drive >> c:\windows\temp\%computername%.log
date /t && time /t >> c:\windows\temp\%computername%.log
copy c:\windows\temp\%computername%.log \\SCCMDP\FilesDelete$ /Y
WMIC /namespace:\\root\ccm path sms_client CALL TriggerSchedule "{00000000-0000-0000-0000-000000000002}" /NOINTERACTIVE

PS: Innovate as much as possible, leveraging SCCM Capabilities to transform your IT environments and you  will rock !!!
 
Cheers, Vasu Miriyala

2 comments:

  1. Hi
    I find this script very useful and i want to use it just to find and report to SCCM if a file exists in any of the computer drives.
    So if I change the two lines where you delete your files by the following two:
    if exists file1.exe >> c:\windows\temp\%computername%.log
    if exists file2.exe >> c:\windows\temp\%computername%.log
    Will the script achieves want i need?
    Best regards,
    JLuz

    ReplyDelete
    Replies
    1. If you just want to find the details of file, like its location, name, size, version etc... then enable Software Inventory Agent which can fetch all details for you.

      Delete