Commit 3785ee0fc8b7399c08114f7a5dc275dbf3e406e2

Authored by unknown
1 parent 704fefb0

Detect if UAC is enabled and elevate process if installer not run as admin

PT:1732247

Updated: dmsctl.vbs can now install, uninstall, start and stop services

Committed by: Charl Joseph Mert
Showing 1 changed file with 361 additions and 13 deletions
dmsctl.vbs
1   -' This script should only be invoked for Vista and Windows 7
  1 +'
  2 +' KnowledgeTree
  3 +'
  4 +'
  5 +'
  6 +
  7 +' Service Name Consts
  8 +Const KTOFFICE = "KTOpenOffice"
  9 +Const KTSCHEDULER = "KTScheduler"
  10 +Const KTLUCENE = "KTLucene"
  11 +
  12 +' Service Control Manager Error Code Consts
  13 +Const SVC_SUCCESS = 0 ' Success
  14 +Const SVC_NOT_SUPPORTED = 1 ' Not Supported
  15 +Const SVC_ACCESS_DENIED = 2 ' Access Denied
  16 +Const SVC_DEPENDENT_SERVICES_RUNNING = 3 ' Dependent Services Running
  17 +Const SVC_INVALID_SERVICE_CONTROL = 4 ' Invalid Service Control
  18 +Const SVC_SERVICE_CANNOT_ACCEPT_CONTROL = 5 ' Service Cannot Accept Control
  19 +Const SVC_SERVICE_NOT_ACTIVE = 6 ' Service Not Active
  20 +Const SVC_SERVICE_REQUEST_TIMEOUT = 7 ' Service Request Timeout
  21 +Const SVC_UNKNOWN_FAILURE = 8 ' Unknown Failure
  22 +Const SVC_PATH_NOT_FOUND = 9 ' Path Not Found
  23 +Const SVC_SERVICE_ALREADY_RUNNING = 10 ' Service Already Running
  24 +Const SVC_SERVICE_DATABASE_LOCKED = 11 ' Service Database Locked
  25 +Const SVC_SERVICE_DEPENDENCY_DELETED = 12 ' Service Dependency Deleted
  26 +Const SVC_SERVICE_DEPENDENCY_FAILURE = 13 ' Service Dependency Failure
  27 +Const SVC_SERVICE_DISABLED = 14 ' Service Disabled
  28 +Const SVC_SERVICE_LOGON_FAILURE = 15 ' Service Logon Failure
  29 +Const SVC_SERVICE_MARKED_FOR_DELETION = 16 ' Service Marked For Deletion
  30 +Const SVC_SERVICES_NO_THREAD = 17 ' Service No Thread
  31 +Const SVC_STATUS_CIRCULAR_DEPENDENCY = 18 ' Status Circular Dependency
  32 +Const SVC_STATUS_DUPLICATE_NAME = 19 ' Status Duplicate Name
  33 +Const SVC_INVALID_NAME = 20 ' Status Invalid Name
  34 +Const SVC_STATUS_INVALID_PARAMETER = 21 ' Status Invalid Parameter
  35 +Const SVC_INVALID_SERVICES_ACCOUNT = 22 ' Status Invalid Service Account
  36 +Const SVC_STATUS_SERVICE_EXISTS = 23 ' Status Service Exists
  37 +Const SVC_SERVICE_ALREADY_PAUSED = 24 ' Service Already Paused
2 38  
3 39 'Detecting current OS
4   -strComputer = "."
  40 +Dim strComputer, currOS, doRunAs
  41 +
  42 +strComputer = "."
5 43 currOS = ""
6 44 doRunAs = false
7 45  
8 46 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
9 47 Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
10 48  
11   -For Each objOperatingSystem in colOperatingSystems
12   - currOS = objOperatingSystem.Caption & " " & objOperatingSystem.Version
13   - currOS = trim(currOS)
  49 +For Each objOperatingSystem in colOperatingSystems
  50 + currOS = objOperatingSystem.Caption & " " & objOperatingSystem.Version
  51 + currOS = trim(currOS)
14 52 Next
15 53  
16   -If left(currOS, 19) = "Microsoft Windows 7" Then
17   - doRunAs = true
18   -End If
  54 +Public Function isWindowsVista()
  55 + isWindows7 = false
  56 + If left(currOS, 19) = "Microsoft Windows Vista" Then
  57 + isWindows7 = true
  58 + End If
  59 +End Function
  60 +
  61 +Public Function isWindows7()
  62 + isWindows7 = false
  63 + If left(currOS, 19) = "Microsoft Windows 7" Then
  64 + isWindows7 = true
  65 + End If
  66 +End Function
  67 +
  68 +Public Function isWindows2008()
  69 + isWindows7 = false
  70 + If left(currOS, 19) = "Microsoft Windows 2008" Then
  71 + isWindows7 = true
  72 + End If
  73 +End Function
19 74  
  75 +' Will call this further down when the individual services need starting
20 76 If doRunAs = true Then
21   - Set objShell = CreateObject("Shell.Application")
22   - Set objFolder = objShell.Namespace("C:\Program Files (x86)\Zend\ktdms\knowledgetree")
23   - Set objFolderItem = objFolder.ParseName("dmsctl_install.bat")
24   - objFolderItem.InvokeVerb "runas"
25   -End If
26 77 \ No newline at end of file
  78 + 'runAs "C:\Program Files (x86)\Zend\ktdms\knowledgetree", "dmsctl_install.bat"
  79 +End If
  80 +
  81 +Public Sub runAs(ByVal strFolder, ByVal strFile)
  82 + Set objShell = CreateObject("Shell.Application")
  83 + Set objFolder = objShell.Namespace(strFolder)
  84 + Set objFolderItem = objFolder.ParseName(strFile)
  85 + objFolderItem.InvokeVerb "runas"
  86 +End Sub
  87 +
  88 +dim objArgs, errMsg, result, strUsage, isSuccess
  89 +
  90 +strUsage = "USAGE:" &_
  91 +"dmsctl.bat <start|stop|restart|install|uninstall> [servicename]" & vbNewLine &_
  92 +vbNewLine &_
  93 +"help - this screen " & vbNewLine &_
  94 +"start - start the services" & vbNewLine &_
  95 +"stop - stop the services" & vbNewLine &_
  96 +"restart - restart the services" & vbNewLine &_
  97 +"install - install the services" & vbNewLine &_
  98 +"uninstall - uninstall the services" & vbNewLine &_
  99 +vbNewLine &_
  100 +"servicename - optional service name to start/stop only that service." & vbNewLine &_
  101 +" only mysql is supported for individual control at this time."
  102 +
  103 +Set objArgs = WScript.Arguments
  104 +If objArgs.count < 1 Then
  105 + Wscript.Echo strUsage
  106 +Else
  107 + Select Case objArgs.Item(0)
  108 + Case "install"
  109 + isSuccess = true ' Track if anything went wrong
  110 +
  111 + ' Installing KTOffice
  112 + result = exec("C:\Program Files\Zend\ktdms\knowledgetree\var\bin\officeinstall.bat")
  113 +
  114 + 'Install Failed
  115 + If result = 0 Then
  116 + isSuccess = false
  117 + logEvent "The KnowledgeTree KTOffice service could not be installed"
  118 + End If
  119 +
  120 + ' Installing KTScheduler
  121 + result = exec("C:\Program Files\Zend\ktdms\knowledgetree\var\bin\schedulerinstall.bat")
  122 +
  123 + 'Install Failed
  124 + If result = 0 Then
  125 + isSuccess = false
  126 + logEvent "The KnowledgeTree KTScheduler service could not be installed"
  127 + End If
  128 +
  129 + ' Installing KTLucene
  130 + result = exec("C:\Program Files\Zend\ktdms\knowledgetree\var\bin\luceneinstall.bat")
  131 +
  132 + 'Install Failed
  133 + If result = 0 Then
  134 + isSuccess = false
  135 + logEvent "The KnowledgeTree KTLucene service could not be installed"
  136 + End If
  137 +
  138 + If (isSuccess) Then
  139 + Wscript.Echo "The KnowledgeTree services were successfully installed"
  140 + Else
  141 + Wscript.Echo "There were errors installing the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')"
  142 + End If
  143 +
  144 + Case "start"
  145 + isSuccess = true
  146 +
  147 + svcName = KTOFFICE
  148 + If (NOT isServiceStarted(svcName)) Then
  149 + If (NOT startService(svcName)) Then
  150 + isSuccess = false
  151 + End If
  152 + End If
  153 +
  154 + svcName = KTSCHEDULER
  155 + If (NOT isServiceStarted(svcName)) Then
  156 + If (NOT startService(svcName)) Then
  157 + isSuccess = false
  158 + End If
  159 + End If
  160 +
  161 + svcName = KTLUCENE
  162 + If (NOT isServiceStarted(svcName)) Then
  163 + If (NOT startService(svcName)) Then
  164 + isSuccess = false
  165 + End If
  166 + End If
  167 +
  168 + If (isSuccess) Then
  169 + Wscript.Echo "The KnowledgeTree services were successfully started"
  170 + Else
  171 + Wscript.Echo "There were errors starting the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')"
  172 + End If
  173 +
  174 + Case "stop"
  175 + isSuccess = true
  176 +
  177 + svcName = KTOFFICE
  178 + If (isServiceStarted(svcName)) Then
  179 + If (NOT stopService(svcName)) Then
  180 + isSuccess = false
  181 + End If
  182 + End If
  183 +
  184 + svcName = KTSCHEDULER
  185 + If (isServiceStarted(svcName)) Then
  186 + If (NOT stopService(svcName)) Then
  187 + isSuccess = false
  188 + End If
  189 + End If
  190 +
  191 + svcName = KTLUCENE
  192 + If (isServiceStarted(svcName)) Then
  193 + If (NOT stopService(svcName)) Then
  194 + isSuccess = false
  195 + End If
  196 + End If
  197 +
  198 + If (isSuccess) Then
  199 + Wscript.Echo "The KnowledgeTree services were successfully stopped"
  200 + Else
  201 + Wscript.Echo "There were errors sopping the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')"
  202 + End If
  203 +
  204 + Case "uninstall"
  205 + isSuccess = true ' Track if anything went wrong
  206 +
  207 + ' Stopping Then Uninstalling
  208 + 'svcName = KTOFFICE
  209 + 'If (isServiceStarted(svcName)) Then
  210 + ' If (NOT stopService(svcName)) Then
  211 + ' isSuccess = false
  212 + ' End If
  213 + 'End If
  214 +
  215 + ' Uninstalling KTOffice
  216 + result = exec("sc delete " & KTOFFICE)
  217 +
  218 + 'Uninstall Failed
  219 + If result = 0 Then
  220 + isSuccess = false
  221 + logEvent "The KnowledgeTree KTOffice service could not be uninstalled"
  222 + End If
  223 +
  224 + ' Stopping Then Uninstalling
  225 + 'svcName = KTSCHEDULER
  226 + 'If (isServiceStarted(svcName)) Then
  227 + ' If (NOT stopService(svcName)) Then
  228 + ' isSuccess = false
  229 + ' End If
  230 + 'End If
  231 +
  232 + ' Uninstalling KTScheduler
  233 + result = exec("sc delete " & KTSCHEDULER)
  234 +
  235 + 'Uninstall Failed
  236 + If result = 0 Then
  237 + isSuccess = false
  238 + logEvent "The KnowledgeTree KTScheduler service could not be uninstalled"
  239 + End If
  240 +
  241 +
  242 +
  243 + ' Stopping Then Uninstalling
  244 + 'svcName = KTLUCENE
  245 + 'If (isServiceStarted(svcName)) Then
  246 + ' If (NOT stopService(svcName)) Then
  247 + ' isSuccess = false
  248 + ' End If
  249 + 'End If
  250 +
  251 + ' Uninstalling KTLucene
  252 + result = exec("sc delete " & KTLUCENE)
  253 +
  254 + 'Uninstall Failed
  255 + If result = 0 Then
  256 + isSuccess = false
  257 + logEvent "The KnowledgeTree KTLucene service could not be uninstalled"
  258 + End If
  259 +
  260 + If (isSuccess) Then
  261 + Wscript.Echo "The KnowledgeTree services were uninstalled"
  262 + Else
  263 + Wscript.Echo "There were errors uninstalling the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')"
  264 + End If
  265 +
  266 + Case Else
  267 + Wscript.Echo strUsage
  268 + End Select
  269 +
  270 +End If
  271 +
  272 +' Method to interrogate a service
  273 +Public Function isServiceStarted(ByVal svcName)
  274 + strComputer = "."
  275 + Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
  276 + ' Obtain an instance of the the class
  277 + ' using a key property value.
  278 + Set objShare = objWMIService.Get("Win32_Service.Name='" & svcName & "'")
  279 +
  280 + ' no InParameters to define
  281 +
  282 + ' Execute the method and obtain the return status.
  283 + ' The OutParameters object in objOutParams
  284 + ' is created by the provider.
  285 + Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='" & svcName & "'", "InterrogateService")
  286 +
  287 + If (objOutParams.ReturnValue = SVC_SERVICE_NOT_ACTIVE) Then
  288 + isServiceStarted = FALSE
  289 + Else
  290 + isServiceStarted = TRUE
  291 + End If
  292 +
  293 +end Function
  294 +
  295 +' Method to start a service
  296 +Public Function startService(ByVal svcName)
  297 + strComputer = "."
  298 + Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
  299 + ' Obtain an instance of the the class
  300 + ' using a key property value.
  301 + Set objShare = objWMIService.Get("Win32_Service.Name='" & svcName &"'")
  302 +
  303 + ' no InParameters to define
  304 +
  305 + ' Execute the method and obtain the return status.
  306 + ' The OutParameters object in objOutParams
  307 + ' is created by the provider.
  308 + Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='" & svcName & "'", "StartService")
  309 +
  310 + If (objOutParams.ReturnValue = SVC_SUCCESS) Then
  311 + startService = TRUE
  312 + Else
  313 + startService = FALSE
  314 + End If
  315 +
  316 +End Function
  317 +
  318 +' Method to stop a service
  319 +Public Function stopService(ByVal svcName)
  320 + strComputer = "."
  321 + Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
  322 + ' Obtain an instance of the the class
  323 + ' using a key property value.
  324 + Set objShare = objWMIService.Get("Win32_Service.Name='" & svcName &"'")
  325 +
  326 + ' no InParameters to define
  327 +
  328 + ' Execute the method and obtain the return status.
  329 + ' The OutParameters object in objOutParams
  330 + ' is created by the provider.
  331 + Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='" & svcName & "'", "StopService")
  332 +
  333 + If (objOutParams.ReturnValue = SVC_SUCCESS) Then
  334 + stopService = TRUE
  335 + Else
  336 + stopService = FALSE
  337 + End If
  338 +
  339 +End Function
  340 +
  341 +' Execute a command
  342 +Public Function exec(ByVal cmd)
  343 +
  344 + Dim WshShell, oExec
  345 + Set WshShell = CreateObject("WScript.Shell")
  346 +
  347 + Set oExec = WshShell.Exec(cmd)
  348 +
  349 + Do While oExec.Status = 0
  350 + WScript.Sleep 100
  351 + Loop
  352 +
  353 + exec = oExec.Status
  354 +
  355 +End Function
  356 +
  357 +Public Sub createCustomEventLog(ByVal strName)
  358 + Const NO_VALUE = Empty
  359 +
  360 + Set WshShell = WScript.CreateObject("WScript.Shell")
  361 + WshShell.RegWrite _
  362 + "HKLM\System\CurrentControlSet\Services\EventLog\" & strName & "\", NO_VALUE
  363 +End Sub
  364 +
  365 +' Event logging only works on Server 2003, 2000 and XP
  366 +Public Sub logEvent(ByVal strMessage)
  367 +
  368 + If (NOT isWindowsVista() AND NOT isWindows7 AND NOT isWindows2008) Then
  369 + Const EVENT_SUCCESS = 0
  370 + Set objShell = Wscript.CreateObject("Wscript.Shell")
  371 + objShell.LogEvent EVENT_SUCCESS, strMessage
  372 + End If
  373 +
  374 +End Sub
... ...