?
Solved

Delete all Scheduled tasks except 1.

Posted on 2009-02-23
32
Medium Priority
?
964 Views
Last Modified: 2012-05-06
Hi,

Delete all Scheduled tasks except 1.

I want to delete any no of scheduled scans in the machines that are in a txt file. the machine anmes are in a txt file. When run scan each machine and delete them.

Exclude one task ca;;ed 'Infected scan" and delete all the others.

Log all thats been deleted.

Regards
Sharath


Const ForReading = 1
Const ForWriting = 2
  
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
Set objlog = objfso.CreateTextFile("c:\log.txt", ForWriting)
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
If Reachable(strcomputer) Then
  If per(strcomputer) then
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
On Error Resume Next    
Set objInstance = objWMIService.Get("Win32_ScheduledJob.JobID=1")
If Err.Number = 0 Then
Err.Clear
On Error Goto 0
err = objInstance.Delete
If InStr(Err,"0") Then
objlog.WriteLine "the job deleted on " & strcomputer
Else
objlog.WriteLine "failed to delete job on " & strcomputer
End If
Else
objlog.WriteLine "the job didn't find on " & strcomputer
End if
Else
objlog.WriteLine "you don't have permission on " & strcomputer
End If
Else
objlog.WriteLine strcomputer & " isn't reachable"
End if
Loop
 
Function Reachable(strComputer)
 
 strCmd = "ping -n 1 " & strComputer
 
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)
 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "REPLY FROM") Then
 Reachable = True 
 Else
 Reachable = False
 End If
End Function
 
Function per(computer)
	strcomputer = computer
	On Error Resume Next
	Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	If err.number <> 0 Then
		err.Clear
		per = False
		On Error goto 0
	Else
		per = True
		On Error goto 0
	End If
End Function

Open in new window

0
Comment
Question by:bsharath
  • 20
  • 6
  • 6
32 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 23713742
what operating system?
were these scheduled with 'at' or 'schtasks'?
0
 
LVL 11

Author Comment

by:bsharath
ID: 23713791
Windows NT,Windows 2000 ,Windows XP and Windows 2003
That script was for a different purpose.
Its schtasks
0
 
LVL 11

Author Comment

by:bsharath
ID: 23713792
Windows NT,Windows 2000 ,Windows XP and Windows 2003
That script was for a different purpose.
Its schtasks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 67

Expert Comment

by:sirbounty
ID: 23714071
If I recall, the problem with using WMI is that it can only access jobs explicitly created via that method...
Untested, but try this on a test machine:
@echo off
set rpt=c:\TaskReport.txt
set TaskToDelete=Infected scan
for /f %%a in (c:\computers.txt) do call :process %%a
goto :eof
 
:process
set pc=%1
echo Checking %pc%>>%rpt%
for /f "tokens=1*" %%s in ('schtasks /query /s \\%pc% /fo list^|find /i "TaskName:"') do (
  if not [%%t]==[%TaskToDelete%] (
    echo.     Removing %%t from %pc%>>%rpt%
    schtasks /delete /s \\%pc% /tn "%%t"
  )
)

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 23714077
Hmm - but I don't think WinNT or Win2k support schtasks - those would be AT jobs and only identified by the task ID...
0
 
LVL 11

Author Comment

by:bsharath
ID: 23714250
I get this

ERROR: The network path was not found.
'scan]' is not recognized as an internal or external command,
operable program or batch file.
WARNING: Are you sure you want to remove the task "Infected Scan" (Y/N )?

I want to skip just that task and delete all the rest
0
 
LVL 11

Author Comment

by:bsharath
ID: 23714251
I get this

ERROR: The network path was not found.
'scan]' is not recognized as an internal or external command,
operable program or batch file.
WARNING: Are you sure you want to remove the task "Infected Scan" (Y/N )?

I want to skip just that task and delete all the rest
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 23714415
Try this - if it still fails, please upload the output report file...
@echo off
set rpt=c:\TaskReport.txt
set TaskToDelete=Infected scan
for /f %%a in (c:\computers.txt) do call :process %%a
goto :eof
 
:process
set pc=%1
ping %pc% -n 1>nul
if not %errorlevel%==0 (
  echo Could not connect to %pc%
  goto :eof
)
echo Checking %pc%>>%rpt%
for /f "tokens=1*" %%s in ('schtasks /query /s \\%pc% /fo list^|find /i "TaskName:"') do (
  echo.     Evaluating %%s %%t>>%rpt%
  if not [%%t]==[%TaskToDelete%] (
    echo.     Removing %%t from %pc%>>%rpt%
    schtasks /delete /s \\%pc% /tn "%%t" /f
  )
)

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 23717901
Just to confirm
Infected scan is the only one that i dont want to delete and want to delete all the other tasks.
Is the script doing the same...
set TaskToDelete=Infected scan
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 23717958
Sorry - it should work as you've asked, I just didn't type it correctly:
Slight changes here though:
@echo off
set rpt=c:\TaskReport.txt
set TaskNOTToDelete=Infected scan
for /f %%a in (c:\computers.txt) do call :process %%a
goto :eof
 
:process
set pc=%1
ping %pc% -n 1>nul
if not %errorlevel%==0 (
  echo Could not connect to %pc%
  goto :eof
)
echo Checking %pc%>>%rpt%
for /f "tokens=1*" %%s in ('schtasks /query /s \\%pc% /fo list^|find /i "TaskName:"') do (
  echo.     Evaluating %%s %%t>>%rpt%
  if [%%t]==[%TaskNOTToDelete%] goto :eof
  echo.     Removing %%t from %pc%>>%rpt%
  schtasks /delete /s \\%pc% /tn "%%t" /f
)

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 23718009
The log does not show any as deleted but logs all the tasks in the machine
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 23718452
Can you upload the log file?
0
 
LVL 11

Author Comment

by:bsharath
ID: 23722710
Sorry i was wrong it just says could not contact and does not create any log file
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23736472
%Errorlevel% 0 is not good to use.

Try it with errorlevel 1

And I think you want tokens=1,*
@echo off
set rpt=c:\TaskReport.txt
set TaskNOTToDelete=Infected scan
for /f %%a in (c:\computers.txt) do call :process %%a
goto :eof
 
:process
set pc=%1
ping %pc% -n 1>nul
if errorlevel 1 (
  echo Could not connect to %pc%
  goto :eof
)
echo Checking %pc%>>%rpt%
for /f "tokens=1,*" %%s in ('schtasks /query /s \\%pc% /fo list^|find /i "TaskName:"') do (
  echo.     Evaluating %%s %%t>>%rpt%
  if [%%t]==[%TaskNOTToDelete%] goto :eof
  echo.     Removing %%t from %pc%>>%rpt%
  schtasks /delete /s \\%pc% /tn "%%t" /f
)

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 23736558
AT

 i get
Could not connect to machine name for all the machines in the computers.txt
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23736734
Can you try this?

for /f %%a in (c:\computers.txt) do (
    ping %%a -n 1>>Pinglog.txt
)

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 23736765
I get this

Value must be supplied for option -n.

Value must be supplied for option -n.
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23737293
Oh. Now try it.
@echo off
set rpt=c:\TaskReport.txt
set TaskNOTToDelete=Infected scan
for /f %%a in (c:\computers.txt) do call :process %%a
goto :eof
 
:process
set pc=%1
ping %pc% -n 1 >nul
if errorlevel 1 (
  echo Could not connect to %pc%
  goto :eof
)
echo Checking %pc%>>%rpt%
for /f "tokens=1,*" %%s in ('schtasks /query /s \\%pc% /fo list^|find /i "TaskName:"') do (
  echo.     Evaluating %%s %%t>>%rpt%
  if [%%t]==[%TaskNOTToDelete%] goto :eof
  echo.     Removing %%t from %pc%>>%rpt%
  schtasks /delete /s \\%pc% /tn "%%t" /f
)

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 23741219
I checked the script it works but removes the Infected scan also.

I want all scheduled items to be deleted except the "Infected scan"
But now even this scan is deleted
0
 
LVL 11

Author Comment

by:bsharath
ID: 23741220
I checked the script it works but removes the Infected scan also.

I want all scheduled items to be deleted except the "Infected scan"
But now even this scan is deleted
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23742097
Oh. We'd better put in /i to ignore case sensitivity.
@echo off
set rpt=c:\TaskReport.txt
set TaskNOTToDelete=Infected scan
for /f %%a in (c:\computers.txt) do call :process %%a
goto :eof
 
:process
set pc=%1
ping %pc% -n 1 >nul
if errorlevel 1 (
  echo Could not connect to %pc%
  goto :eof
)
echo Checking %pc%>>%rpt%
for /f "tokens=1,*" %%s in ('schtasks /query /s \\%pc% /fo list^|find /i "TaskName:"') do (
  echo.     Evaluating %%s %%t>>%rpt%
  if /i [%%t]==[%TaskNOTToDelete%] goto :eof
  echo.     Removing %%t from %pc%>>%rpt%
  schtasks /delete /s \\%pc% /tn "%%t" /f
)

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 23742131
I will not wanted this scan to be deleted.

Infected scan only should not be deleted. I guess it still deletes them
0
 
LVL 11

Author Comment

by:bsharath
ID: 23742132
I will not wanted this scan to be deleted.

Infected scan only should not be deleted. I guess it still deletes them
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 23742309
This line checks for the task named "Infected scan" if it does then skip.

if /i [%%t]==[%TaskNOTToDelete%] goto :eof

Before without the /i upper and lower case needed to match. It should skip it now.

0
 
LVL 11

Author Comment

by:bsharath
ID: 23742388
Here is the results
C:\>"Find Scheduled task.bat"
ERROR: No network provider accepted the given network path.
Could not connect to Dev-pc2165
SUCCESS: The scheduled task "Infected Scan" was successfully deleted.
ERROR: The specified task name "Infected Scan" does not exist in the system.
SUCCESS: The scheduled task "New scan" was successfully deleted.
0
 
LVL 11

Author Comment

by:bsharath
ID: 23742389
Here is the results
C:\>"Find Scheduled task.bat"
ERROR: No network provider accepted the given network path.
Could not connect to Dev-pc2165
SUCCESS: The scheduled task "Infected Scan" was successfully deleted.
ERROR: The specified task name "Infected Scan" does not exist in the system.
SUCCESS: The scheduled task "New scan" was successfully deleted.
0
 
LVL 21

Accepted Solution

by:
AmazingTech earned 2000 total points
ID: 23742457
Sorry the /i didn't work.  It needs to be in quotes for /i to work or atleast now it works.
@echo off
set rpt=c:\TaskReport.txt
set TaskNOTToDelete=Infected scan
for /f %%a in (c:\computers.txt) do call :process %%a
goto :eof
 
:process
set pc=%1
ping %pc% -n 1 >nul
if errorlevel 1 (
  echo Could not connect to %pc%
  goto :eof
)
echo Checking %pc%>>%rpt%
for /f "tokens=1,*" %%s in ('schtasks /query /s \\%pc% /fo list^|find /i "TaskName:"') do (
  echo.     Evaluating %%s %%t>>%rpt%
  if /i "%%t" == "%TaskNOTToDelete%" goto :eof
  echo.     Removing %%t from %pc%>>%rpt%
  schtasks /delete /s \\%pc% /tn "%%t" /f
)

Open in new window

0
 
LVL 11

Author Comment

by:bsharath
ID: 23745107
Thank U worked perfect
0
 
LVL 11

Author Comment

by:bsharath
ID: 23745108
Thank U worked perfect
0
 
LVL 11

Author Comment

by:bsharath
ID: 23753465
AT any help on the above please
0
 
LVL 11

Author Comment

by:bsharath
ID: 23753466
AT any help on the above please
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question