Delete a scheduled task on all machines in a txt file. Scheduled task called "AT01"

Hi,

Delete a scheduled task on all machines in a txt file. Scheduled task called "AT01"
Need to get the success & failure to just know on how many machines it was deleted.

And need to skip ping failure machines permission issu machines.

Regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?
 
yehudahaCommented:
hey

maybe i still don't control vbscript as i think i am but i don't know any sure way to pull the job name and use it in the script.

so in my script run by job id in you case i guess 1 .
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
 
bsharathAuthor Commented:
Does that mean that all jobs will be deleted. Or do i need to see something in the task and let you know.

Any help on the 2 posts

Add Administrator to each group....
0
 
bsharathAuthor Commented:
Does that mean that all jobs will be deleted. Or do i need to see something in the task and let you know.

Any help on the 2 posts

Add Administrator to each group....
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
yehudahaCommented:
no it mean only at1 will be deleted as you asked

i'm checking now the other posts
0
 
bsharathAuthor Commented:
Thanks i think its working.

What is the change i need to do in the script to just log them without deleting.

So i can see if the script is deleting the right one please...
0
 
bsharathAuthor Commented:
Thanks i think its working.

What is the change i need to do in the script to just log them without deleting.

So i can see if the script is deleting the right one please...
0
 
yehudahaCommented:
try this
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
objlog.WriteLine "the job deleted on " & strcomputer
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
 
bsharathAuthor Commented:
How and what does the script delete. why i ask is there are machines that show 10 + scheduled scans as

AT1
AT2
AT3
and so on

Can we set to delete anything with AT


0
 
yehudahaCommented:
sorry what i tryed with my knowledge it's only the job id

you can live the question open a new one and maybe one of ther other experts can help you
0
 
bsharathAuthor Commented:
Thank U
0
 
yehudahaCommented:
thanks you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.