Page 1 of 1 1
Topic Options
#140919 - 2005-06-02 10:00 PM Disable and Stop a service
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
I need to change the Startup Type for a service on a remote Server to Disable and Stop that service. I searched the KiXtart forum and found the fnWMIService UDF, which turned out to be exactly what I needed. Based on the examples I came up with this:

Code:

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

Dim $SetService, $Remote

$Remote = 'Citrix01' ; Name of the remote computer where service is running on
$SetService=fnWMIService('AventailAutoSOCKS','ChangeStartMode(Disabled)',$Remote)
? 'Change Startup Type Mode Error: ' + @ERROR
$SetService=fnWMIService('AventailAutoSOCKS','StopService',$Remote)
? 'Change Service Status 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



Since I need to run the script against several servers, I decided to add some logic to first find out if the service I need to Disable and Stop was even installed on that server. Again based on the examples, I tried to add that function, but when I re-ran the script I got the following error message:

C:\>kix32 Services.kix
Service Name:
ERROR : unknown command [ fnWMIService]!
Script: C:\Services.kix
Line : 8


I can’t figure out the error. It the same function name as in the rest of the script. I’ve already re-typed the script, in the event there was a non-visible character that was causing the problem. Can someone shed some light on this?

Second Script
Code:

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

Dim $SetService, $Remote

"Service Name: " fnWMIService('Aventail Connect','DisplayName',$Remote)
If @ERROR
@ERROR " | " @SERROR ?
Else
$Remote = 'Citrix01' ; Name of the remote computer where service is running on
$SetService=fnWMIService('AventailAutoSOCKS','ChangeStartMode(Disabled)',$Remote)
? 'Change Startup Type Mode Error: ' + @ERROR
$SetService=fnWMIService('AventailAutoSOCKS','StopService',$Remote)
? 'Change Service Status Error: ' + @ERROR
EndIf

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
#140920 - 2005-06-02 10:46 PM Re: Disable and Stop a service
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
sounds like a missing quote but there is no such.
looks good to my eye, both of them.
_________________________
!

download KiXnet

Top
#140921 - 2005-06-02 10:49 PM Re: Disable and Stop a service
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
What do you se when you run it in debug?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#140922 - 2005-06-02 11:52 PM Re: Disable and Stop a service
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
My luck, I can’t replicate the problem. But after looking at the code again, the $Remote variable was in the wrong place. Works when I moved it up a couple of lines.

I need a vacation, some time off, need to relax a little. Sorry, for the post.

Top
#140923 - 2005-06-02 11:56 PM Re: Disable and Stop a service
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Looks like you're calling the function with $Remote on line 8 but don't declare $remote till line 12

 $Remote = 'Citrix01'

Should be set before, or actually use the call like this
"Service Name: " + fnWMIService('Aventail Connect','DisplayName','Citrix01')

Notice the + sign. Since NoVarInStrings is on and you've placed a TEXT STRING before the call without the + sign it becomes all one string.

 

Top
#140924 - 2005-06-03 12:20 AM Re: Disable and Stop a service
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
actually, this looks like a bug.
a) the last parameter is optional
b) $remote is defined, thus no rules broken

conclusion: buggie boo
_________________________
!

download KiXnet

Top
#140925 - 2005-06-03 02:08 AM Re: Disable and Stop a service
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
You're right, there is a bug in the function, but it wasn't copied as posted. The following line needs to be corrected...

Code:

If Not $sComputer $sComputer="" EndIf


...to...
Code:

If Not $sComputer $sComputer=@WKSTA EndIf


Top
#140926 - 2005-06-03 02:11 AM Re: Disable and Stop a service
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
not talking about the function but kix.
_________________________
!

download KiXnet

Top
#140927 - 2005-06-03 02:22 AM Re: Disable and Stop a service
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
That should not cause it either Chris.

But the method for the WMI is not correct for a connection as a local non admin user.

I'll try to do more testing in general if I get time, but I think my ConfirmWMI does both methods. Not positive though if I updated that on the site.

 

Top
#140928 - 2005-06-03 02:30 AM Re: Disable and Stop a service
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Yes I did update the UDF

That code works on a LOCAL system even if the user is not an Admin. Also works for remote systems if the user has Admin rights on the remote system.


ConfirmWMI() - Confirm access and version of WMI
 

Top
#140929 - 2005-06-03 03:06 AM Re: Disable and Stop a service
Chris S. Offline
MM club member
*****

Registered: 2002-03-18
Posts: 2368
Loc: Earth
@Jooel, I couldn't confirm a KiXtart bug anywhere. Methinks one of his attempts had a typo somewhere.

@Doc, it would cause a failure to connect to WMI as the namespace was incorrect.

At any rate, I think he got his problem fixed.

Quote:

My luck, I can’t replicate the problem. But after looking at the code again, the $Remote variable was in the wrong place. Works when I moved it up a couple of lines.

I need a vacation, some time off, need to relax a little. Sorry, for the post.




@operez, good idea about the vacation. I'm actually on vacation myself. Of course, that hasn't stopped me from working harder around the house than I ever do at work.

Top
#140930 - 2005-06-03 04:18 AM Re: Disable and Stop a service
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Chris,

Yeah looks like you were correct. Looks like he apparently used a combination of some of my code and some of your code which are not set the same, so he should have used your example instead of mixing them. Anyways... that said, if you would make a minor modification to your UDF then it would be able to handle the call better when a computer name is not supplied. Currently your method of the connection is using a UNC even on a local system which means a user must have Admin rights to access it. By making the call to the local namespace without the UNC a user without Admin rights can still run the script. It wouldn't complete the task if the user didn't have Admin rights (it would not be able to modify the service) but the WMI connection would work, so that if it was only a query it would complete.

Function fnWMIService($sService,$sMethod,Optional $sComputer)
Dim $objWMI,$objSrvc,$nul
If Not $sComputer $sComputer = ''
Else
$sComputer = '\\'+Join(Split($sComputer,'\'),'',3)+'\'
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


 

Here is his code re-worked that should work well. Not really sure what's up with checking a Display Name of one item and then setting another item. Don't have the product installed to confirm why.

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


Dim $Remote,$sService,$SetService
$Remote = 'Citrix01'
$sService = 'AventailAutoSOCKS'
"Service Name: " fnWMIService('Aventail Connect','DisplayName',$Remote)
If @ERROR
@ERROR " | " @SERROR ?
Else
$SetService=fnWMIService($sService,'ChangeStartMode(Disabled)',$Remote)
? 'Change Startup Type Mode Error: ' + @ERROR + ' : ' + @SERROR
$SetService=fnWMIService($sService,'StopService',$Remote)
? 'Change Service Status Error: ' + @ERROR + ' : ' + @SERROR
EndIf


Function fnWMIService($sService,$sMethod,Optional $sComputer)
Dim $objWMI,$objSrvc,$nul
If Not $sComputer $sComputer = ''
Else
$sComputer = '\\'+Join(Split($sComputer,'\'),'',3)+'\'
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
#140931 - 2005-06-03 09:02 PM Re: Disable and Stop a service
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
NTDOC I did use a combination of both yours and Chris’ code, assuming the fnWMIService function was the same. Also in reference to your comment about using the DisplayName, it was showing blank until I changed it to the service name, which is what the function is looking for.

Code:

from

"Service Name: " fnWMIService('Aventail Connect','DisplayName',$Remote)

to

"Service Name: " fnWMIService(‘AventailAutoSOCKS’,'DisplayName',$Remote)


Your code was a life saver, since I had to make the change to 100 Citrix servers. Thanks to both of you for all your help.
And everyone else who contributed to this thread.

Top
#140932 - 2005-06-03 09:18 PM Re: Disable and Stop a service
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
What's with the curly quotes?
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#140933 - 2005-06-03 09:20 PM Re: Disable and Stop a service
operez Offline
Getting the hang of it

Registered: 2003-08-08
Posts: 75
Typo, sorry.
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 293 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.066 seconds in which 0.023 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