bsharath
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
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
ASKER
Windows NT,Windows 2000 ,Windows XP and Windows 2003
That script was for a different purpose.
Its schtasks
That script was for a different purpose.
Its schtasks
ASKER
Windows NT,Windows 2000 ,Windows XP and Windows 2003
That script was for a different purpose.
Its schtasks
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:
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"
)
)
Hmm - but I don't think WinNT or Win2k support schtasks - those would be AT jobs and only identified by the task ID...
ASKER
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
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
ASKER
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
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
)
)
ASKER
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
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:
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
)
ASKER
The log does not show any as deleted but logs all the tasks in the machine
Can you upload the log file?
ASKER
Sorry i was wrong it just says could not contact and does not create any log file
%Errorlevel% 0 is not good to use.
Try it with errorlevel 1
And I think you want tokens=1,*
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
)
ASKER
AT
i get
Could not connect to machine name for all the machines in the computers.txt
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
)
ASKER
I get this
Value must be supplied for option -n.
Value must be supplied for option -n.
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
)
ASKER
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 want all scheduled items to be deleted except the "Infected scan"
But now even this scan is deleted
ASKER
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 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
)
ASKER
I will not wanted this scan to be deleted.
Infected scan only should not be deleted. I guess it still deletes them
Infected scan only should not be deleted. I guess it still deletes them
ASKER
I will not wanted this scan to be deleted.
Infected scan only should not be deleted. I guess it still deletes them
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.
if /i [%%t]==[%TaskNOTToDelete%]
Before without the /i upper and lower case needed to match. It should skip it now.
ASKER
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.
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
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank U worked perfect
ASKER
Thank U worked perfect
ASKER
Help on this please
https://www.experts-exchange.com/questions/24181003/Need-to-query-5-DHCP-servers-and-get-all-the-leased-ip's-and-machinenames-and-any-possible-data-to-an-excel-or-a-csv.html
https://www.experts-exchange.com/questions/24180842/Batch-script-that-can-get-to-each-machine-and-search-all-drives-for-Songs.html
https://www.experts-exchange.com/questions/24180822/Scan-every-machine-in-the-list-and-find-all-machines-that-has-shares-of-any-kind-which-has-songs-in-them-Only-those-names-should-be-logged-in-them.html
https://www.experts-exchange.com/questions/24181003/Need-to-query-5-DHCP-servers-and-get-all-the-leased-ip's-and-machinenames-and-any-possible-data-to-an-excel-or-a-csv.html
https://www.experts-exchange.com/questions/24180842/Batch-script-that-can-get-to-each-machine-and-search-all-drives-for-Songs.html
https://www.experts-exchange.com/questions/24180822/Scan-every-machine-in-the-list-and-find-all-machines-that-has-shares-of-any-kind-which-has-songs-in-them-Only-those-names-should-be-logged-in-them.html
ASKER
AT any help on the above please
ASKER
AT any help on the above please
were these scheduled with 'at' or 'schtasks'?