Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Delete all Scheduled tasks except 1.

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

Avatar of sirbounty
sirbounty
Flag of United States of America image

what operating system?
were these scheduled with 'at' or 'schtasks'?
Avatar of bsharath

ASKER

Windows NT,Windows 2000 ,Windows XP and Windows 2003
That script was for a different purpose.
Its schtasks
Windows NT,Windows 2000 ,Windows XP and Windows 2003
That script was for a different purpose.
Its schtasks
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

Hmm - but I don't think WinNT or Win2k support schtasks - those would be AT jobs and only identified by the task ID...
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
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
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

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
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

The log does not show any as deleted but logs all the tasks in the machine
Can you upload the log file?
Sorry i was wrong it just says could not contact and does not create any log file
Avatar of AmazingTech
AmazingTech

%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

AT

 i get
Could not connect to machine name for all the machines in the computers.txt
Can you try this?

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

Open in new window

I get this

Value must be supplied for option -n.

Value must be supplied for option -n.
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

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
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
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

I will not wanted this scan to be deleted.

Infected scan only should not be deleted. I guess it still deletes them
I will not wanted this scan to be deleted.

Infected scan only should not be deleted. I guess it still deletes them
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.

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank U worked perfect
Thank U worked perfect
AT any help on the above please
AT any help on the above please