Page 1 of 1 1
Topic Options
#167633 - 2006-09-14 02:39 AM UDF : fADSIServiceRun() - How to enhance for service names with spaces
pearly Offline
Getting the hang of it
*****

Registered: 2004-02-04
Posts: 92
Hi, I have services whose names contain spaces. The space is causing an issue on line :

Code:

$objService = Getobject("WinNT://$Computer/$Service,Service")


Error code returned is -2147467259

Any idea how to make this work? I have services with names that do not contain spaces and the UDF was able to start those services. Only services whose names contain spaces failed to start.

Here is the UDF : http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=82528&an=0&page=23#82528

Thanks!

Top
#167634 - 2006-09-14 05:06 AM Re: UDF : fADSIServiceRun() - How to enhance for service names with spaces
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
Well my guess is this is not NoVarInStrings compliant.

Please give this UDF a try and I'm sure you'll be happy with it.

fnWMIService - Use the Win32_Service class of WMI to control services
http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=83246

Top
#167635 - 2006-09-14 05:17 AM Re: UDF : fADSIServiceRun() - How to enhance for service names with spaces
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Doc, you are right it is not NoVarsInStrings compliant, but there is another issue as pearly says.
Code:

? fADSIServiceRun ("", "Task Scheduler", 0)
? "Error " + @ERROR + ": " + @SERROR
? fADSIServiceRun ("", "Task Scheduler", 1)
? "Error " + @ERROR + ": " + @SERROR
? fADSIServiceRun ("", "WebClient", 0)
? "Error " + @ERROR + ": " + @SERROR
? fADSIServiceRun ("", "WebClient", 1)
? "Error " + @ERROR + ": " + @SERROR


returns
Code:

Error -2147023836: De opgegeven service is geen geïnstalleerde service.

Error -2147023836: De opgegeven service is geen geïnstalleerde service.

Error 0: De bewerking is voltooid.

Error 0: De bewerking is voltooid.


Top
#167636 - 2006-09-14 08:45 AM Re: UDF : fADSIServiceRun() - How to enhance for service names with spaces
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I have been working a bit in the fADSIServiceRun (Sorry Howard).
In this way, it will support services with spaces in their names
Code:

Function fADSIServiceRun ($Computer, $Service, $Run)
Dim $objService, $status, $StartUp, $objServices
; Validate input parameters
If VarType($Computer) <> 8
Exit(87)
EndIf
If VarType($Service) <> 8
Exit(87)
EndIf
If VarType($Run) <> 2 AND
VarType($Run) <> 3 AND
$Run >= 0 AND $Run <= 3
Exit(87)
EndIf

If $Computer = ""
$Computer = @wksta
EndIf

;$objService = Getobject('WinNT://'+$Computer+'/'+$Service+',Service')
$objServices = Getobject('WinNT://'+$Computer)
$objServices.Filter = 'Array("Service")'
For Each $objService In $objServices
If $objService.DisplayName = $Service

;If @error = 0
$status = $objService.status

Select
Case $Run = 0
Select
Case $status = 4 ;Running
$objService.stop
Case $status = 1 ;Stopped
Exit (2182)
Case $status = 7 ;Paused
$objService.stop
Case 1
? 'Unhandled service status(0):$status'
Exit (1)
EndSelect
Case $Run = 1
Select
Case $status = 1 ;Not Running
$StartUp = $objService.Get('StartType')
If $StartUp <> 4
$objService.start
Else
Exit (1058)
EndIf
Case $status = 7 ;Paused
$objService.continue
Case $status = 4 ;Running
Exit (3521)
Case 1
? 'Unhandled service status(1):$status'
Exit (1)
EndSelect
Case $Run = 2
Select
Case $status = 4 ;Running
$objService.pause
Case $status = 1 ;Stopped
$StartUp = $objService.Get('StartType')
If $StartUp <> 4
$objService.start
$objService.pause
Else
Exit (1058)
EndIf
Case 1
? 'Unhandled service status(2):$status'
Exit (1)
EndSelect
EndSelect
$objService = ''
;Else
; Exit @error
;EndIf
EndIf
Next
EndFunction


Top
#167637 - 2006-09-14 06:05 PM Re: UDF : fADSIServiceRun() - How to enhance for service names with spaces
pearly Offline
Getting the hang of it
*****

Registered: 2004-02-04
Posts: 92
Thank you Witto! It is working great. Kudos
Top
#167638 - 2006-09-14 08:43 PM Re: UDF : fADSIServiceRun() - How to enhance for service names with spaces
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11625
Loc: CA
Quote:

Doc, you are right it is not NoVarsInStrings compliant, but there is another issue as pearly says.





Yes, but I wasn't going to debug it like you did

The other UDF I linked to can do much more than this one.

Top
#167639 - 2006-09-14 09:23 PM Re: UDF : fADSIServiceRun() - How to enhance for service names with spaces
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
True, but if you just need to stop/start/pause a service, I have the impression this UDF is all you need.
Does anybody know maybe if a space in the service name can be supported in building this string?
Getobject('WinNT://'+$Computer+'/'+$Service+',Service')
I have been searching but all examples I found wisely show a service name without spaces...

Top
Page 1 of 1 1


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

Who's Online
0 registered and 323 anonymous users online.
Newest Members
Audio, Hoschi, Comet, rrosell, PatrickPinto
17880 Registered Users

Generated in 0.037 seconds in which 0.012 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