#135620 - 2005-03-15 05:01 PM
Removing Service... Can it be done?
|
dataspike
Getting the hang of it
Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
|
Below is code that I use to install a service. Now, is it possible to remove the same service via code?
Code:
WMICreateService (@WKSTA, "BigBrotherClient", "Big Brother SNM Client 1.08d", "C:\BB\BBNT\1.08d\bin\bbnt.exe","Automatic")
Function WMICreateService($Hostname,$ServiceName,$DisplayName,$PathName,$StartType,Optional $Username,Optional $Pass, Optional $verbose) Dim $WMIService,$WMIServiceCreateProperties,$GetError,$GotErr Dim $Text[25] $Text = "Service "+$ServiceName+" has been created successfully","Not Supported","Access Denied","Dependent Services Running", "Invalid Service Control","Service Cannot Accept Control","Service Not Active","Service Request Timeout","Unknown Failure", "Path Not Found","Service Already Running","Service Database Locked","Service Dependency Deleted", "Service Dependency Failure","Service Disabled","Service Logon Failure","Service Marked For Deletion","Service No Thread", "Status Circular Dependency","Status Duplicate Name","Status Invalid Name","Status Invalid Parameter", "Status Invalid Service Account","Status Service Exists","Service Paused" If $verbose $msg = MessageBox ("Function WMICreateService: Started",0) EndIf $WMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"+$Hostname+"/root/cimv2:Win32_Service") If @ERROR If $verbose $msg = MessageBox ("Function WMICreateService: WMI Failed",0) EndIf $WMICreateService="Function WMICreateService: WMI Failed" Exit -1 EndIf If $verbose $msg = MessageBox ("Function WMICreateService: WMI Connected",0) EndIf $WMIServiceCreateProperties = $WMIService.Methods_("Create") $Properties=$WMIServiceCreateProperties.inParameters.SpawnInstance_() $Properties.Name = $ServiceName $Properties.DisplayName = $DisplayName $Properties.PathName = $PathName $Properties.StartMode = $StartType $Properties.StartName = $UserName $Properties.StartPassword = $pass $GetError = $WMIService.ExecMethod_("Create", $Properties) $GotErr = $GetError.ReturnValue $WMICreateService = "Function WMICreateService: "+$Text[$GotErr] Exit $GotErr If $verbose $msg = MessageBox ("Function WMICreateService: Finished",0) EndIf EndFunction
|
|
Top
|
|
|
|
#135621 - 2005-03-15 05:21 PM
Re: Removing Service... Can it be done?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Here's a vbs snippet you could convert to Kixtart:
Code:
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colListOfServices = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Service WHERE Name = 'DbService'") For Each objService in colListOfServices objService.StopService() objService.Delete() Next
-Shawn
|
|
Top
|
|
|
|
#135622 - 2005-03-15 05:25 PM
Re: Removing Service... Can it be done?
|
dataspike
Getting the hang of it
Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
|
Quote:
Here's a vbs snippet you could convert to Kixtart:
Code:
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colListOfServices = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Service WHERE Name = 'DbService'") For Each objService in colListOfServices objService.StopService() objService.Delete() Next
-Shawn
Shawn,
Thanks for the point in the right direction, however I am a real newbie when it comes to scripting, both in Kix and VBS... any additional direction would be helpful. The script above was grabbed from one of the other sites. Thanks for your help and insight.
Chris
|
|
Top
|
|
|
|
#135623 - 2005-03-15 05:27 PM
Re: Removing Service... Can it be done?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Heres some interesting reading before you start, basically, Microsoft says there is more to removing a service than just "the service removal" thru WMI. (think we knew that already) ;0)
Installing and Removing Services
|
|
Top
|
|
|
|
#135624 - 2005-03-15 05:32 PM
Re: Removing Service... Can it be done?
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Here's a quick conversion, I commented-out the Delete part until your ready to try it ... try simply stopping the service first, then go for the big Delete.
Code:
break on
$strComputer = "."
$strService = "Alerter"
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2")
$colListOfServices = $objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Name = '$strService'")
For Each $objService in $colListOfServices
$= $objService.StopService()
;$= $objService.Delete()
Next
-Shawn
|
|
Top
|
|
|
|
#135630 - 2005-03-28 03:26 PM
Re: Removing Service... Can it be done?
|
supermanzdead
Getting the hang of it
Registered: 2005-03-09
Posts: 50
|
Instead of using WMI can't you use XNET to remove a service??
_________________________
~Mwah
|
|
Top
|
|
|
|
#135631 - 2005-03-28 09:23 PM
Re: Removing Service... Can it be done?
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Look at the XNET.txt and you will find the answer there.
Kent
|
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
1 registered
(Allen)
and 1172 anonymous users online.
|
|
|