;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       SvcCtl() - DEPRECATED in ITCG CODING - SEE WMISvcMgr() 
;; 
;;ACTION         Starts/Stops service and controls service start type 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0 - 2003/02/11 
;; 
;;HISTORY        1.0 - 2003/02/11 - Initial Release 
;; 
;;SYNTAX         SvcCtl(Cmd, Service, [Server], MonitorMode) 
;; 
;;PARAMETERS     Cmd - Required; one of START, STOP, AUTOMATIC, MANUAL, DISABLE 
;; 
;;               Service - Required; name of a specific service to control 
;; 
;;               Server	- Optional; name of a remote server to manage 
;; 
;;		 MonitorMode - Optional - boolean 
;; 
;;REMARKS        Starts or stops services and controls service start type.  
;; 
;;               If "MonitorMode" is set, this function will change the start-type 
;;               services to Disabled before stopping them, and return them to the prior 
;;               setting when starting them. This is necessary to prevent certain 
;;               Network Monitor systemms from restarting these services that it detects 
;;               as "down" when their service Auto-Restart functionality is enabled. 
;;                
;;               START will start the named service 
;; 
;;               STOP will stop the named service 
;; 
;;               AUTOMATIC will change the start type to Automatic 
;; 
;;               MANUAL will change the start type to Manual 
;; 
;;               DISABLE will change the start type to Disabled 
;; 
;;RETURNS        1 if success, 0 if fail 
;;               @ERROR is set to 0 on success, 1 on failure 
;; 
;;DEPENDENCIES   xnet.exe - Kixtart component - must be available in the PATH, or have 
;;               it's location defined by %S_BIN% or @SCRIPTDIR 
;; 
;;TESTED WITH    NT4, W2K, WXP 
;;               Explicit, NoVarsInStrings 
;; 
;;EXAMPLES       $RC = SvcCtl('STOP', 'service') 
;;               $RC = SvcCtl('START', 'service') 
; 
Function SvcCtl($_fCmd, $_fService, Optional $_Target, Optional $_PatrolMode)
 
  Dim $_, $_Cmd, $_Err, $_Quote, $_RegKey, $_StartType, $_CPath
 
 
  ; insure proper format of server name, if specified 
  $_Target =  IIf(CStr($_Target) <> '', '\\' + Join(Split(CStr($_Target), '\'), '', 3) + '\', '')
 
  $_Quote  = '"'
  $_RegKey = $_Target + 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\' + $_fService
 
  ; Locate the XNET.EXE command 
  $_CPath = IIf(Exist('%S_BIN%\xnet.exe'),'%S_BIN%\', '')	; default to PATH, look in %S_BIN% 
  $_CPath = IIf($_CPath = '' And Exist(@SCRIPTDIR + '\xnet.exe'), @SCRIPTDIR + '\', $_CPath)
 
 
  Select
    Case $_fCmd = 'START'	; ========================================= START 
 
      ; reset the start type if service was stopped with PatrolMode active 
      If $_PatrolMode
        $_StartType = ReadValue($_RegKey, 'PMStart')
        ; restore previous setting if PMStart exists 
        $_ = DelValue($_RegKey, 'PMStart')				; Remove PMStart value 
        $_ = WriteValue($_RegKey, 'Start', $_StartType, 'REG_DWORD')	; previous start type 
      EndIf	; PatrolMode 
      If @ERROR
        $SvcCtl = Not @ERROR
        Exit @ERROR
      EndIf
 
      ; start the service 
      $_Cmd = '%ComSpec% /c ' + $_CPath + 'XNet Start ' + $_Quote + $_Target + $_fService + $_Quote + ' >NUL:'
      Shell $_Cmd
      $SvcCtl = Not @ERROR
      Exit @ERROR
 
 
    Case $_fCmd = 'STOP'		; ========================================= STOP 
      ; get the start type if PatrolMode is true 
      If $_PatrolMode
        $_StartType = ReadValue($_RegKey, 'Start')
        ; Set to Disable and save original start type if not disabled 
        If Val($_StartType) <> 4
          $_ = WriteValue($_RegKey, 'PMStart', $_StartType, 'REG_DWORD')	; Save Current 
          $_ = WriteValue($_RegKey, 'Start', 4, 'REG_DWORD')	; Set Disable 
        EndIf
      EndIf	; PatrolMode 
 
      If @ERROR
        $SvcCtl = Not @ERROR
        Exit @ERROR
      EndIf
 
      ; stop the service 
      $_Cmd = '%ComSpec% /c ' + $_CPath + 'XNet Stop ' + $_Quote + $_Target + $_fService + $_Quote + ' >NUL:'
      Shell $_Cmd
      $SvcCtl = Not @ERROR
      Exit @ERROR
 
    Case $_fCmd = 'AUTOMATIC'	; ========================================= AUTOMATIC START 
      $_ = WriteValue($_RegKey, 'Start', 2, 'REG_DWORD')
      $SvcCtl = Not @ERROR
      Exit @ERROR
 
    Case $_fCmd = 'MANUAL'	; ========================================= MANUAL START 
      $_ = WriteValue($_RegKey, 'Start', 3, 'REG_DWORD')
      $SvcCtl = Not @ERROR
      Exit @ERROR
 
    Case $_fCmd = 'DISABLE'	; ========================================= DISABLED 
      $_ = WriteValue($_RegKey, 'Start', 4, 'REG_DWORD')
      $SvcCtl = Not @ERROR
      Exit @ERROR
 
    Case 1			; ========================================= INVALID COMMAND 
      Exit 87	; invalid argument 
 
  EndSelect
 
EndFunction