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
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.
#140929 - 2005-06-0303:06 AMRe: Disable and Stop a service
Chris S.Chris S.
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.
#140930 - 2005-06-0304:18 AMRe: Disable and Stop a service
NTDOCNTDOC 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.
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.
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.
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.