Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stopping and Disabling services

Status
Not open for further replies.

maxandmia

MIS
Sep 4, 2007
3
US
I am trying to use the following script to stop and disable a services that is not needed. (not the service in this script) My question is why to I get error:0x80041021, Code: 80041021 for line 13 when it runs. It will work through the list and stop the service but then kicks out the error. Also is there a way to add the disbale into this code (line 23 - commented out) I am not a programmer but I need to accomplish this task. Any and all help will be appreciated.
Thank you,
Maria

strCurrentDir = GetCurrentDirectory() &"\"
strServerList = strCurrentDir & "AGC_Servers.xls"
' Open spreadsheet for Servers
Set oXL = WScript.CreateObject("EXCEL.application")
oXL.Visible = True
oXL.workbooks.open strServerList
oXL.sheets("Sheet1").Activate
introw = 2

Do
strComputer = oXL.Cells(intRow, 2).Value

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery("Associators of " _
& "{Win32_Service.Name='Schedule'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )

For Each objService in colServiceList
If objService.Name = "Schedule" & objService.State = 4 Then
objService.StopService()
End If
' If objService.Name = "Schedule" & objService.StartType = "Auto" Then
' objService.ChangeStartType("Disabled")
' End If

Next

Set colServiceList2 = objWMIService.ExecQuery ("Select * from Win32_Service where Name='Schedule'")
'BackupExecAgentAccelerator
For Each objService in colServiceList2
errReturn = objService.StopService()
Wscript.Sleep 5000
If errReturn = 0 then
oXL.Cells(intRow,3).Value = "service is now stopped"
ElseIf errReturn = 5 then
oXL.Cells(intRow,3).Value = "Service was already stopped"
ElseIf errReturn <> 0 or errReturn <> 5 Then
oXL.Cells(intRow,3).Value = "Error stopping the service"
End If
Next
intRow = intRow + 1
Loop Until strComputer = ""


oXL.activeworkbook.save
oXL.workbooks.close
oXL.application.quit()


Function GetCurrentDirectory()
GetCurrentDirectory = Left(WScript.ScriptFullName,InstrRev(WScript.ScriptFullName,"\") -1)
End Function
 
What's line 13 for you and what service are you trying to stop?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
personally i prefer the LDAP/WinNT approach I find it easier to read
 
Here is a sample of how to disable all services set to start manually.

Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where StartMode = 'Manual'")

For Each objService in colServiceList
    errReturnCode = objService.Change( , , , , "Disabled")   
Next

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Line 13

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

I have verified that the code is correct from several different sources. I am not sure that it has anything to do with this line at all. The script successfully runs through the list of servers and writes out the results in the Excel spreadsheet and then throws this error.

Thank you
 
If it works on several servers and then errors out then it could be your passing an invalid computer name or a blank name or that WMI is corrupted on that particular computer.



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thank you. I figured it out how to stop it. I placed an "On Error Resume Next' before it. I don't know if it was the correct way to eliminate the error but I now can stop and disable the service along with updating my spreadsheet.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top