Page 1 of 2 12>
Topic Options
#159914 - 2006-03-28 02:46 PM Kix to start a remote service.
Stephen Wintle Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 444
Loc: England
Hi,

Thanks to previous posts I can check using kix to see if service exists ie
Code:
 If GetObject("WinNT://" + $wrkid + "/cron,service")

Else

Endif


But can I get kix to start a given service on a remote machine if service hasn't been started?

Steve
_________________________
Dont worry because a rival imitates you. As long as they follow in your tracks they cant pass you!

Top
#159915 - 2006-03-28 02:58 PM Re: Kix to start a remote service.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
There are UDFs for this already written. Check out the UDF library...


Two service related UDFs I have submitted are: fADSIServiceRun and fADSIServiceStartup
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#159916 - 2006-03-28 03:54 PM Re: Kix to start a remote service.
Stephen Wintle Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 444
Loc: England
Hi Howard,

I can only get Chris's UDF I tried even following the links to the UDFs' on this page but they are no longer there..

Steve
_________________________
Dont worry because a rival imitates you. As long as they follow in your tracks they cant pass you!

Top
#159917 - 2006-03-28 03:59 PM Re: Kix to start a remote service.
Stephen Wintle Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 444
Loc: England
Ignore previous post I found the mirror site!
_________________________
Dont worry because a rival imitates you. As long as they follow in your tracks they cant pass you!

Top
#159918 - 2006-03-28 04:01 PM Re: Kix to start a remote service.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
they were submitted more than 3 years ago. When you do your search, change the number of years in the "Newer Than" section to 6. You will get the links then.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#159919 - 2006-03-28 04:03 PM Re: Kix to start a remote service.
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
The search only goes 3 years back by default. Changing this to 5 years show the two UFD's.

fADSIServiceRun() - ADSI routine that starts, stops ,pause, and continues a service

fADSIServiceStartup() - ADSI routine to change a service startup type
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#159920 - 2006-03-28 04:13 PM Re: Kix to start a remote service.
Stephen Wintle Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 444
Loc: England
Thanks for replies,

Still in the dark though. I need to ensure that a service is started on a series of machines. How does the UDF help me to do that? can I use the UDF with RemoteExec to achieve this?

Steve.
_________________________
Dont worry because a rival imitates you. As long as they follow in your tracks they cant pass you!

Top
#159921 - 2006-03-28 04:25 PM Re: Kix to start a remote service.
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Below is a some code that finds a given service if it is running and sets @error to something other then 0 if the service is not running.

Code:

Shell '%comspec% /c net start | find /i "Symantec Antivirus"'
If @ERROR = 0
?"Service running."
Else
?"Service not running."
EndIf
Sleep 5



Edited by Mart (2006-03-28 04:26 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#159922 - 2006-03-28 05:03 PM Re: Kix to start a remote service.
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
If you use http://www.kixtart.org/udf you don't need to worry about date range.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#159923 - 2006-03-28 05:48 PM Re: Kix to start a remote service.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
If you read fADSIServiceRun, you will see that it can connect to a remote computer and then uses the "status" method to get the current current state of the service. below is a code segment from the UDF which you can tune to your own needs.

Code:

$objService = Getobject("WinNT://$Computer/$Service,Service")
if @error = 0
$status = $objService.status
select
case $status = 4 ;Running
; do something
case $status = 1 ;Stopped
; do something
case $status = 7 ;Paused
; do something
case 1
? "Unhandled service status(0):$status"
exit (1)
endselect
endif

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#159924 - 2006-03-29 11:48 AM Re: Kix to start a remote service.
Stephen Wintle Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 444
Loc: England
Howard,

Thanks for your reply tried the following but to no avail. Could you please give me some pointers?

Code:
 
Function fADSIServiceRun ($Computer, $Service, $Run)
Dim $objService, $status, $StartUp
; 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")
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
EndFunction


$Computer = ReadFile('\\curricsvr1\netlogon\scripts\computers.txt')
$Service = 'BeyondLogic RmtExec Server'


$objService = Getobject("WinNT://$Computer/$Service,Service")
if @error = 0
$status = $objService.status
select
case $status = 4 ;Running
; do something

Run "Beyondexecv2 \\" + $Computer + " -d LockWorkstation -l 0"

case $status = 1 ;Stopped
; do something

ServiceRun ($Computer, "BeyondLogic RmtExec Server", 1)

case $status = 7 ;Paused
; do something

ServiceRun ($Computer, "BeyondLogic RmtExec Server", 1)

case 1
? "Unhandled service status(0):$status"
exit (1)
endselect
endif


Function ReadFile($file)
Dim $lf, $f, $_, $t
$lf=CHR(10)
$f=FreeFileHandle
$_=Open($f,$file)
If @ERROR Exit @ERROR EndIf
Do $t=$t+$lf+ReadLine($f) Until @ERROR
$_=Close($f)
$ReadFile=Split(SubStr($t,2),$lf)
EndFunction



Many thanks

Steve
_________________________
Dont worry because a rival imitates you. As long as they follow in your tracks they cant pass you!

Top
#159925 - 2006-03-29 05:57 PM Re: Kix to start a remote service.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You do not say what results/errors your code generated.

$Service = 'BeyondLogic RmtExec Server' <- I would recommend that you use the short name (name of the key for the service) in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

ServiceRun ($Computer, "BeyondLogic RmtExec Server", 1) <- this is not the UDF name. It is fADSIServiceRun.

If you provide more details, I could provide a better answer.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#159926 - 2006-03-29 05:59 PM Re: Kix to start a remote service.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Also add an ELSE section to the fllowing if..endif.
Code:

$objService = Getobject("WinNT://$Computer/$Service,Service")
if @error = 0
...
else
? $Computer
? "Error: " + @error
? " " + @serror
endif



This will let you know if the remote service could not be contacted.


Edited by Howard Bullock (2006-03-29 06:00 PM)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#159927 - 2006-03-30 01:47 PM Re: Kix to start a remote service.
Stephen Wintle Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 444
Loc: England
Hi Howard,

Many thanks for your help. I have tried your suggestions but the scripts does not return anything!

Steve..
_________________________
Dont worry because a rival imitates you. As long as they follow in your tracks they cant pass you!

Top
#159928 - 2006-03-30 01:52 PM Re: Kix to start a remote service.
Stephen Wintle Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 444
Loc: England
Sorry Howard also not sure what you meant by 'using shortname' are you refering to exe or not?

Code:
 ;FUNCTION         fADSIServiceRun()
;AUTHOR Howard A. Bullock (hbullock@tycoelectronics.com)
;ACTION Starts or stops a service.
;SYNTAX fADSIServiceRun(Computer, Service, $Run)
;PARAMETERS $Computer (Required) - String value
; $Service (Required) - String value
; $Run (Required) (0=stop or 1=start|continue or 2=Pause)
;REMARKS This function starts,stops, pauses, or continues a service on
; the local or a remote computer. This function sets @ERROR on exit.
; when "pausing" a service this function will start a stopped service then
; pause it.
;RETURNS Nothing
;DEPENDENCIES ADSI
;EXAMPLES ServiceRun ("", "Alerter", 0)
; ServiceRun ("SomeComputer", "Alerter", 1)

Function fADSIServiceRun ($Computer, $Service, $Run)
Dim $objService, $status, $StartUp
; 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")
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
? $Computer
? "Error: " + @error
? " " + @serror

endif
EndFunction


$Computer = ReadFile('\\curricsvr1\netlogon\scripts\computers.txt')
$Service = 'BeyondLogic RmtExec Server'


$objService = Getobject("WinNT://$Computer/$Service,Service")
if @error = 0
$status = $objService.status
select
case $status = 4 ;Running
; do something

Run "Beyondexecv2 \\" + $Computer + " -d LockWorkstation -l 0"

case $status = 1 ;Stopped
; do something

ServiceRun ($Computer, "BeyondLogic RmtExec Server", 1)

case $status = 7 ;Paused
; do something

ServiceRun ($Computer, "BeyondLogic RmtExec Server", 1)

case 1
? "Unhandled service status(0):$status"
exit (1)
endselect
endif


Function ReadFile($file)
Dim $lf, $f, $_, $t
$lf=CHR(10)
$f=FreeFileHandle
$_=Open($f,$file)
If @ERROR Exit @ERROR EndIf
Do $t=$t+$lf+ReadLine($f) Until @ERROR
$_=Close($f)
$ReadFile=Split(SubStr($t,2),$lf)
EndFunction




Edited by Stephen Wintle (2006-03-30 02:02 PM)
_________________________
Dont worry because a rival imitates you. As long as they follow in your tracks they cant pass you!

Top
#159929 - 2006-03-31 04:20 PM Re: Kix to start a remote service.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Look in the registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

All the keys listed here are services. The key name is the "Short Name".


Again you are calling: ServiceRun ($Computer, "BeyondLogic RmtExec Server", 1) what is this? It is NOT the name of the UDF fADSIServiceRun.


Edited by Howard Bullock (2006-03-31 04:22 PM)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#159930 - 2006-03-31 04:37 PM Re: Kix to start a remote service.
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Quote:

Again you are calling: ServiceRun ($Computer, "BeyondLogic RmtExec Server", 1) what is this? It is NOT the name of the UDF fADSIServiceRun.




...but it is in your examples.
Code:
;EXAMPLES         ServiceRun ("", "Alerter", 0)
; ServiceRun ("SomeComputer", "Alerter", 1)

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#159931 - 2006-04-05 10:40 AM Re: Kix to start a remote service.
Stephen Wintle Offline
Seasoned Scripter

Registered: 2001-04-10
Posts: 444
Loc: England
Hi,

Finally the penny dropped and I did (once) get the script to lock my lockstation. However despite not changing the script in anyway im now getting the following error Code:
 ERROR : unexpected command!
Script: \\curricsvr1\netlogon\scripts\lock2.kix
Line : 0



Ive tried stopping the server to see if it throws up any different errors but no joy..

Code:
;FUNCTION         fADSIServiceRun()
;AUTHOR Howard A. Bullock (hbullock@tycoelectronics.com)
;ACTION Starts or stops a service.
;SYNTAX fADSIServiceRun(Computer, Service, $Run)
;PARAMETERS $Computer (Required) - String value
; $Service (Required) - String value
; $Run (Required) (0=stop or 1=start|continue or 2=Pause)
;REMARKS This function starts,stops, pauses, or continues a service on
; the local or a remote computer. This function sets @ERROR on exit.
; when "pausing" a service this function will start a stopped service then
; pause it.
;RETURNS Nothing
;DEPENDENCIES ADSI
;EXAMPLES ServiceRun ("", "Alerter", 0)
; ServiceRun ("SomeComputer", "Alerter", 1)

Function fADSIServiceRun ($Computer, $Service, $Run)
Dim $objService, $status, $StartUp
; 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")
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
? $Computer
? "Error: " + @error
? " " + @serror

endif
EndFunction



$Computer = 'servxp1' ;ReadFile('\\curricsvr1\netlogon\scripts\computers.txt')
$Service = 'rexesvr'


$objService = Getobject("WinNT://$Computer/$Service,Service")
if @error = 0
$status = $objService.status
select
case $status = 4 ;Running
; do something

Run "Beyondexecv2 \\" + $Computer + " -d LockWorkstation -l 0"

case $status = 1 ;Stopped
; do something

fADSIServiceRun ($Computer, $Service, 1)

case $status = 7 ;Paused
; do something

fADSIServiceRun ($Computer, $Service, 1)

case 1
? "Unhandled service status(0):$status"
exit (1)
endselect
else
? $Computer
? "Error: " + @error
? " " + @serror
endif

EndFunction

Function ReadFile($file)
Dim $lf, $f, $_, $t
$lf=CHR(10)
$f=FreeFileHandle
$_=Open($f,$file)
If @ERROR Exit @ERROR EndIf
Do $t=$t+$lf+ReadLine($f) Until @ERROR
$_=Close($f)
$ReadFile=Split(SubStr($t,2),$lf)
EndFunction



Edited by Stephen Wintle (2006-04-05 10:40 AM)
_________________________
Dont worry because a rival imitates you. As long as they follow in your tracks they cant pass you!

Top
#159932 - 2006-04-05 01:06 PM Re: Kix to start a remote service.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Quote:

...but it is in your examples




Well that just sucks... All these years I nor anyone else seemed to never notice.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#159933 - 2006-04-05 01:12 PM Re: Kix to start a remote service.
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Do you have a pre-4.x version of KixTart somehow executing? Try putting...
Code:

@kix ?


as the first line of your script.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 202 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.076 seconds in which 0.026 seconds were spent on a total of 13 queries. Zlib compression enabled.

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