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