Page 1 of 2 12>
Topic Options
#138511 - 2005-04-22 09:30 PM Services?
pat_smith1969 Offline
Fresh Scripter

Registered: 2005-04-19
Posts: 18
Has anyone done anything with checking weather or not a service is started, Disabled then starting, or enabeling it if it is not?

Is this beyond the scope of what KIX can do?

Top
#138512 - 2005-04-22 09:54 PM Re: Services?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
There are probably a dozen or so "service" UDFs and maybe few hundred topics posted. Did you try a search?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#138513 - 2005-04-22 10:57 PM Re: Services?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Please take a look here for many UDF scripts that will handle just about anything you want to do with a Service.

KiXhelp UDF Mirror

There are over 500 UDFs mirrored here already - so not much normal day to day Admin work that is not already in an easy to use UDF.

Good luck and if you need further assistance please let us know. Don't forget the FAQ section here on the board as well which answers a lot of basic questions about KiXtart.


Edited by NTDOC (2005-04-22 10:57 PM)

Top
#138514 - 2005-04-25 10:09 PM Re: Services?
pat_smith1969 Offline
Fresh Scripter

Registered: 2005-04-19
Posts: 18
I did several searches actually. I always TRY to find the information before bothering others, maybe my search strings were not good enough.

Thank You all for your assistnace, the UDF mirror was exactly what I needed.

Top
#138515 - 2005-04-25 10:15 PM Re: Services?
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
This would do it.
Change it to your needs.
Please not that this can not be done to all services. Eventlog for example can't be stopped in this like this because it is an essential service. Not that anyone should like to stop an essential service but he who knows what everyone wants.

Code:

;Checks if the requested service is running and changes the start-up type if wanted.
;In this example the Themes service is stopped and set to disabled.
;Status: 0 is boot, 1 is system, 2 is automatic, 3 is manual, 4 is disabled.
;
Break on
;
$Key = "HKLM\SYSTEM\CurrentControlSet\Services\"
$ServiceState = ReadValue ($key + "Themes", "Start")
;
If $ServiceState = "4"
?"Can't start a disabled service. Enable the service manually."
Else
Select
Case $ServiceState = "0"
$ServiceState = "Boot"
Case $ServiceState = "1"
$ServiceState = "System"
Case $ServiceState = "2"
$ServiceState = "Automatic"
Case $ServiceState = "3"
$ServiceState = "Manual"
Case $ServiceState = "4"
$ServiceState = "Disabled"
EndSelect
;
Shell '%comspec% /c net start | find /i "Themes"'
;
Select
Case @ERROR = "0"
?"Themes service is running and starts: " + $serviceState
Shell '%comspec% /c net stop "Themes"'
$rd = WriteValue ($key + "Themes", "Start", "4", "REG_DWORD")
?"Requested service was running and is set to disabled."
Case @ERROR <> "0" AND $ServiceState ="0"
WriteValue ($key + "Themes", "Start", "4", "REG_DWORD")
Case @ERROR <> "0" AND $ServiceState ="1"
WriteValue ($key + "Themes", "Start", "4", "REG_DWORD")
Case @ERROR <> "0" AND $ServiceState ="2"
WriteValue ($key + "Themes", "Start", "4", "REG_DWORD")
Case @ERROR <> "0" AND $ServiceState ="3"
WriteValue ($key + "Themes", "Start", "4", "REG_DWORD")
Case @ERROR <> "0" AND $ServiceState = "4"
?"Requested service is not running and has status disabled."
Case @ERROR <> "0" AND $ServiceState <> "4"
WriteValue ($key + "Themes", "Start", "4", "REG_DWORD")
?"Requested service is not running and is set to disabled."
EndSelect
EndIf
;
@CRLF
Do
?"Please check the messages above [Q to Quit] " Get $x
Until $x = "q"



Edited by R2D2 (2005-04-25 10:19 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#138516 - 2005-04-25 10:17 PM Re: Services?
pat_smith1969 Offline
Fresh Scripter

Registered: 2005-04-19
Posts: 18
Another question...
I started using the SERVICEMODE() udf but am a LITTLE confused about the syntax...

The readme says
;SYNTAX SERVICEMODE(SERVICENAME [,STARTUPMODE])
and
;EXAMPLE $mode=SERVICEMODE('service','automatic')

I tried this...
Code:
 IF $mode=SERVICEMODE('Altiris Agent','Disabled')
SERVICEMODE('Altiris Agent','Automatic')
EndIF


But it did nothing, I am probably using the syntax and examples wrong... I also tried just a simple
Code:
 SERVICEMODE('Altiris Agent','Automatic')


without any other logic but this did nothing either...
What I am aiming at here is to simple set the service to Automatic if it is set to disabled. I did check my registry to make sure I have the exact right name of the service.

Top
#138517 - 2005-04-25 10:30 PM Re: Services?
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
OK! The SERVICEMODE() udf is a much better way to control the services then my example
Had this wondering around for some time, it works great though.

Doing a $mode=SERVICEMODE('Themes','Automatic') should set the start-up mode to automatic. It does for me.
First doing a check like in the example I posted above and then set the state using SERVICEMODE() udf should work.
Off course you should start the service after it has been set to automatic. It does not start by itself. And you will need the proper privileges to change service start-up mode.


Edited by R2D2 (2005-04-25 10:31 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#138518 - 2005-04-25 10:53 PM Re: Services?
pat_smith1969 Offline
Fresh Scripter

Registered: 2005-04-19
Posts: 18
I changed the test service to Themes(as in $mode=SERVICEMODE('Themes','Automatic') ) and it worked GREAT...
I tried The Altiris Agent again and it did not work. Which tells me my problem is either that I do not have the exact correct service name OR I am having issues with the space in the service name.. so any ideas on the correct syntax for a service with a space in the name? Im sure I need to throw a "" in somewhere.

Top
#138519 - 2005-04-25 11:01 PM Re: Services?
pat_smith1969 Offline
Fresh Scripter

Registered: 2005-04-19
Posts: 18
FYI I got it working...
The problem was not the space in the name of the service I was using... but rather I was using the name that was in the registry under "Display Name". I found that I must use the name of the registry KEY!!!

Thanks for all your help.

Top
#138520 - 2005-04-26 03:47 PM Re: Services?
pat_smith1969 Offline
Fresh Scripter

Registered: 2005-04-19
Posts: 18
OK guess I am not totally done as yet. Like I said above I got the ServiceMODE function to change the service from "Disabled" to "Automatic". I even when through the help that R2D2 provided and created the below script and that changes the type from "Disabled" to "Automativ (see code). I have tried this on a couple different non-essential services and I get the same problem (see below).
Code:
Code:
$Key = "HKLM\SYSTEM\CurrentControlSet\Services\"
$ServiceState = ReadValue ($key + "Themes", "Start")
?" Altiris Agent Service State: " + $ServiceState
If $ServiceState <> "2"
WriteValue ($key + "Themes", "Start", "2", "REG_DWORD")
EndIF



The problem is that even though it changes the service from one start type to another I cannot start the service. I get an error saying the service is still disabled. Both the SERVICEMODE function and the above script does this.

If I use the GUI to change the service type, I do not have the problem. If I change the start type in the REGISTRY directly (using regedit) I do not have the problem. It is only when I use any script to change the value in the registry that I have this issue. To fix the service I simple open the service GUI and set it to disabled and then re-set it to automatic. That fixes it.

I have watched the registry as the script runs and can see it change the "Start" field from 4 to 2. I can see no difference between what the script does and what I do when I manually change the registry value (which works).

Anyone ever have this issue, can anyone offer any advice?

Top
#138521 - 2005-04-26 05:02 PM Re: Services?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Hi Pat,

I know what you are trying to do as we run Altiris Too.. Maybe do a remote Admin Script with XNET? That might be the trick you are looking for. The big problem with Altiris is that we have seen when we try to connect in using CarbonCopy, is the service has stopped and we need to start it remotely, before connecting in.

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

Top
#138522 - 2005-04-26 05:10 PM Re: Services?
pat_smith1969 Offline
Fresh Scripter

Registered: 2005-04-19
Posts: 18
heheh nice to hear others share my misery.
Altiris is a great product in most ways but the users HATE it (big brother phobia). We consider ourselves a "scholastic Campus" and it has been decreed (by powers far greater than mine) that all users will have full admin rights on their PCs. They frequently go in and delete all the altiris files in program files\altiris directory and stop and disable the services.

I am simply looking for a way to counter this issue at logon by re-installing and re-enabeling it.

Top
#138523 - 2005-04-26 05:17 PM Re: Services?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
From what I understand (I don't Adminster it), but you are supposed to be able to "re-push" the application to the clients.

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

Top
#138524 - 2005-04-26 05:39 PM Re: Services?
pat_smith1969 Offline
Fresh Scripter

Registered: 2005-04-19
Posts: 18
you can re-push the app butt...
If the service is disabled and stopped there is not way for the app to recieve the repair type push. It will not re-install because it will see the PC as already having the client installed on it and will ignore any subsequent installs.

My logon script so far works as I want, in that it will check to make sure the client files are not deleted, if they are it will force a re-install of the client. So half my battle is done.

Now I just need to re-enable the service. I am working on doing a snapshot of the PC before/after the service is enabled/disabled to see if I can use that instead.

Top
#138525 - 2005-04-26 06:14 PM Re: Services?
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Maybe a very user unfriendly option.....

How about checking the service state or if it is present at all, check if the files still exist if not remove the service using Xnet. After removing the service re-push the client app. Maybe one or more reboots are necessary, don't know not using Altiris.

Like I said not so user friendly but he, they remove the files and disable the service. This way they will get burned by the things they do but are not supposed to do. Maybe they will learn something this way and maybe this a a way to show the higher powers that admin privs to everyone is not a good option because the systems get f$#@cked up by the users.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#138526 - 2005-04-26 09:00 PM Re: Services?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Pat,

You can't start a service that is in the Disabled mode so you need to first set the Startup mode to AUTOMATIC, then actually Start the service. However, in your case if they have removed the files or enough of the files to prevent the service from starting then you need to at least set it to Automatic, then push out the Altiris Agent again.

Personally I like using the fnWMIService UDF from Chris S.
fnWMIService - Use the Win32_Service class of WMI to control services

NOTE! Chris has not updated the code yet though to support the NoVarsInStrings option. I have updated and posted an example here though for you to try.

Break On
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')


Dim $SetService, $Remote
$Remote = 'computername' ; Name of the remote computer to start service on
$SetService=fnWMIService('Altiris Agent','ChangeStartMode(Automatic)',$Remote)
? 'Change startmode error: ' + @ERROR
$SetService=fnWMIService('Altiris Agent','StartService',$Remote)
? 'Starting service error: ' + @ERROR


Function fnWMIService($sService,$sMethod,Optional $sComputer)
Dim $objWMI,$objSrvc,$nul
If Not $sComputer $sComputer="" EndIf
$objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"+$sComputer+"\root\cimv2")
If @ERROR<0 Exit Val("&"+Right(DecToHex(@ERROR),4)) EndIf
$objSrvc = $objWMI.ExecQuery('Select * from Win32_Service WHERE Name = "'+$sService+'"')
For Each $objSrvc In $objSrvc
$nul=Execute("$"+"fnWMIService = $"+"objSrvc."+$sMethod)
Next
EndFunction


Top
#138527 - 2005-04-26 09:38 PM Re: Services?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Doc,

Quote:

You can't start a service that is in the Disabled mode so you need to first set the Startup mode to AUTOMATIC, then actually Start the service.




Not completely true.. You can start a service if it is set to MANUAL..

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

Top
#138528 - 2005-04-27 01:23 AM Re: Services?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Kent

Quote:

Not completely true.. You can start a service if it is set to MANUAL




I said nothing about MANUAL I said you can't start one set to DISABLED until you set it to Automatic (or if what you mean is that it can also be set to MANUAL, then yes, true either MANUAL or AUTOMATIC will work, but not DISABLED.)

Top
#138529 - 2005-04-27 04:05 PM Re: Services?
pat_smith1969 Offline
Fresh Scripter

Registered: 2005-04-19
Posts: 18
Thank you again for your help guys.

I will try the new function and see if that works any better. I will post back my results good or bad.

Just to be clear though on my issue. The scripts I have used so far ONLY set the service to automatic. THen when I go and manually start the service it says it is still disabled (1058 error pops up on the screen, and a 10005 DCOM error in the event log), even though the service tool claims it is set to automatic... weird.

The service will not start after that until I use the service tool to re-set the service type to any other mode and back again.

Talk to you guys in a little bit...

Top
#138530 - 2005-04-27 07:26 PM Re: Services?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Well if you're having DCOM issues then you're going to have a lot of issues with all types of remote connections and activity.

Microsoft COM (Component Object Model) technology in the Microsoft Windows-family of Operating Systems enables software components to communicate.

COM Automation allows users to build scripts in their applications to perform repetitive tasks or control one application from another.


COM: Component Object Model Technologies

However that specific error message sound like you attempted to START the service, not just modify it's state.
 
net helpmsg 1058

The service cannot be started, either because it is disabled or because it has no enabled devices associated with it.


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 1003 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.074 seconds in which 0.025 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