Page 1 of 1 1
Topic Options
#135620 - 2005-03-15 05:01 PM Removing Service... Can it be done?
dataspike Offline
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 Offline
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 Offline
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 Offline
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 Offline
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
#135625 - 2005-03-15 07:18 PM Re: Removing Service... Can it be done?
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Quick an dirty.

Quote from here: http://www.cryer.co.uk/brian/windows/howto_nt_das.htm

Quote:


Normally it should not be necessary to manually delete a service. Uninstalling an application should remove its associated service (if any).

However, should it be necessary to manually remove a service:

Run Regedit or regedt32.

Find the registry entry:

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services

Find the service there and delete it.
You may wish to look at the keys and see what files the service was using and perhaps delete them also.
Note: You will have to reboot before the list gets updated in server manager.





So stopping the service via net stop and then removing the reg key would be easy doable using (kix) code.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#135626 - 2005-03-15 07:19 PM Re: Removing Service... Can it be done?
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Will obviously require admin privs

Edited by R2D2 (2005-03-15 07:20 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#135627 - 2005-03-15 08:17 PM Re: Removing Service... Can it be done?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
For the most part yes, but for some that have a legacy entry the local admin often toes not have rights to remove it until you give yourself rights.
Top
#135628 - 2005-03-16 12:27 AM Re: Removing Service... Can it be done?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
And if you want to remove the service, don't you also want to remove the related files as well?
_________________________
There are two types of vessels, submarines and targets.

Top
#135629 - 2005-03-16 09:42 AM Re: Removing Service... Can it be done?
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Sure.
There are however some software programmers that compile crappie stuff and do not remove everything when you remove the app.

And if he installed a custom service with the script in the first post the file(s) will be known so the removal of the service and the file(s) can be wrapped in the same script.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#135630 - 2005-03-28 03:26 PM Re: Removing Service... Can it be done?
supermanzdead Offline
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 Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Look at the XNET.txt and you will find the answer there.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#135632 - 2005-03-29 01:29 AM Re: Removing Service... Can it be done?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Quote:

Instead of using WMI can't you use XNET to remove a service??



is irrelevant as we're focusing on COM-Automation in this forum. A lot of the COM tasks can be performed via command-line tools as well. Some people just don't like SHELLing out if one can do it solely with inline KiXtart code.
_________________________
There are two types of vessels, submarines and targets.

Top
#135633 - 2005-04-03 07:07 PM Re: Removing Service... Can it be done?
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
I apologize! Shawn's WMI example looks like it works to resolve the issue...I am still new with the WMI stuff
_________________________
~Mwah

Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1172 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.07 seconds in which 0.031 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org