diff --git a/dmsctl.vbs b/dmsctl.vbs index 8b952d1..faee13e 100644 --- a/dmsctl.vbs +++ b/dmsctl.vbs @@ -36,13 +36,17 @@ Const SVC_INVALID_SERVICES_ACCOUNT = 22 ' Status Invalid Service Account Const SVC_STATUS_SERVICE_EXISTS = 23 ' Status Service Exists Const SVC_SERVICE_ALREADY_PAUSED = 24 ' Service Already Paused -'Detecting current OS -Dim strComputer, currOS, doRunAs +Dim strComputer, currOS, currDir, doRunAs, lastErrorCode + +' Detecting the current directory +Set oFso = CreateObject("Scripting.FileSystemObject") +currDir = oFso.GetParentFolderName(Wscript.ScriptFullName) strComputer = "." currOS = "" doRunAs = false +' Detecting the current OS Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem") @@ -52,28 +56,28 @@ For Each objOperatingSystem in colOperatingSystems Next Public Function isWindowsVista() - isWindows7 = false + isWin = false If left(currOS, 19) = "Microsoft Windows Vista" Then - isWindows7 = true + isWin = TRUE End If End Function Public Function isWindows7() - isWindows7 = false + isWin = false If left(currOS, 19) = "Microsoft Windows 7" Then - isWindows7 = true + isWin = TRUE End If End Function Public Function isWindows2008() - isWindows7 = false + isWin = false If left(currOS, 19) = "Microsoft Windows 2008" Then - isWindows7 = true + isWin = TRUE End If End Function ' Will call this further down when the individual services need starting -If doRunAs = true Then +If doRunAs = TRUE Then 'runAs "C:\Program Files (x86)\Zend\ktdms\knowledgetree", "dmsctl_install.bat" End If @@ -100,38 +104,44 @@ vbNewLine &_ " only mysql is supported for individual control at this time." Set objArgs = WScript.Arguments -If objArgs.count < 1 Then +If (objArgs.count < 1) Then Wscript.Echo strUsage Else Select Case objArgs.Item(0) Case "install" - isSuccess = true ' Track if anything went wrong + isSuccess = TRUE ' Track if anything went wrong ' Installing KTOffice - result = exec("C:\Program Files\Zend\ktdms\knowledgetree\var\bin\officeinstall.bat") + result = exec(currDir & "\var\bin\officeinstall.bat") 'Install Failed If result = 0 Then isSuccess = false - logEvent "The KnowledgeTree KTOffice service could not be installed" + writeLog "The " & KTOFFICE & " KnowledgeTree service could not be installed." + Else + writeLog "The " & KTOFFICE & " KnowledgeTree service was successfully installed." End If ' Installing KTScheduler - result = exec("C:\Program Files\Zend\ktdms\knowledgetree\var\bin\schedulerinstall.bat") + result = exec(currDir & "\var\bin\schedulerinstall.bat") 'Install Failed If result = 0 Then isSuccess = false - logEvent "The KnowledgeTree KTScheduler service could not be installed" + writeLog "The " & KTSCHEDULER & " KnowledgeTree service could not be installed." + Else + writeLog "The " & KTSCHEDULER & " KnowledgeTree service was successfully installed." End If ' Installing KTLucene - result = exec("C:\Program Files\Zend\ktdms\knowledgetree\var\bin\luceneinstall.bat") + result = exec(currDir & "\var\bin\luceneinstall.bat") 'Install Failed If result = 0 Then isSuccess = false - logEvent "The KnowledgeTree KTLucene service could not be installed" + writeLog "The " & KTLUCENE & " KnowledgeTree service could not be installed." + Else + writeLog "The " & KTLUCENE & " KnowledgeTree service was successfully installed." End If If (isSuccess) Then @@ -141,69 +151,254 @@ Else End If Case "start" - isSuccess = true - - svcName = KTOFFICE - If (NOT isServiceStarted(svcName)) Then - If (NOT startService(svcName)) Then - isSuccess = false + If (objArgs.count > 1) Then + Select Case objArgs.Item(1) + Case "soffice" + isSuccess = TRUE + svcName = KTOFFICE + If (NOT isServiceStarted(svcName)) Then + If (NOT startService(svcName)) Then + isSuccess = FALSE + writeLog "The " & KTOFFICE & " KnowledgeTree service could not be started. Result Code: " & lastErrorCode + Else + writeLog "The " & KTOFFICE & " KnowledgeTree service was successfully started" + End If + + writeLog "Successfully started " & KTOFFICE + Else + writeLog KTOFFICE & " already started. Result Code: " & lastErrorCode + End If + + If (isSuccess) Then + Wscript.Echo "The " & KTOFFICE & " KnowledgeTree service was successfully started" + Else + Wscript.Echo "The " & KTOFFICE & " KnowledgeTree service could not be started. Result Code: " & lastErrorCode + End If + + Case "scheduler" + isSuccess = TRUE + svcName = KTSCHEDULER + If (NOT isServiceStarted(svcName)) Then + If (NOT startService(svcName)) Then + isSuccess = FALSE + writeLog "The " & KTSCHEDULER & " KnowledgeTree service could not be started. Result Code: " & lastErrorCode + Else + writeLog "The " & KTSCHEDULER & " KnowledgeTree service was successfully started" + End If + + writeLog "Successfully started " & KTSCHEDULER + Else + writeLog KTSCHEDULER & " already started. Result Code: " & lastErrorCode + End If + + If (isSuccess) Then + Wscript.Echo "The " & KTSCHEDULER & " KnowledgeTree service was successfully started" + Else + Wscript.Echo "The " & KTSCHEDULER & " KnowledgeTree service could not be started. Result Code: " & lastErrorCode + End If + + Case "lucene" + isSuccess = TRUE + svcName = KTLUCENE + If (NOT isServiceStarted(svcName)) Then + If (NOT startService(svcName)) Then + isSuccess = false + writeLog "The " & KTLUCENE & " KnowledgeTree service could not be started. Result Code: " & lastErrorCode + Else + writeLog "The " & KTLUCENE & " KnowledgeTree service was successfully started" + End If + + writeLog "Successfully started " & KTLUCENE + Else + writeLog KTLUCENE & " already started. Result Code: " & lastErrorCode + End If + + If (isSuccess) Then + Wscript.Echo "The " & KTLUCENE & " KnowledgeTree service was successfully started" + Else + Wscript.Echo "The " & KTLUCENE & " KnowledgeTree service could not be started. Result Code: " & lastErrorCode + End If + End Select + Else + isSuccess = TRUE + + svcName = KTOFFICE + If (NOT isServiceStarted(svcName)) Then + If (NOT startService(svcName)) Then + isSuccess = false + End If + writeLog "Successfully started " & KTOFFICE + Else + writeLog KTOFFICE & " already started. Result Code: " & lastErrorCode End If - End If - svcName = KTSCHEDULER - If (NOT isServiceStarted(svcName)) Then - If (NOT startService(svcName)) Then - isSuccess = false + svcName = KTSCHEDULER + If (NOT isServiceStarted(svcName)) Then + If (NOT startService(svcName)) Then + isSuccess = false + End If + writeLog "Successfully started " & KTSCHEDULER + Else + writeLog KTSCHEDULER & " already started. Result Code: " & lastErrorCode End If - End If - svcName = KTLUCENE - If (NOT isServiceStarted(svcName)) Then - If (NOT startService(svcName)) Then - isSuccess = false + svcName = KTLUCENE + If (NOT isServiceStarted(svcName)) Then + If (NOT startService(svcName)) Then + isSuccess = false + End If + writeLog "Successfully started " & KTLUCENE + Else + writeLog KTLUCENE & " already started. Result Code: " & lastErrorCode End If - End If - If (isSuccess) Then - Wscript.Echo "The KnowledgeTree services were successfully started" - Else - Wscript.Echo "There were errors starting the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')" + If (isSuccess) Then + Wscript.Echo "The KnowledgeTree services were successfully started" + Else + Wscript.Echo "There were errors starting the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')" + End If End If - Case "stop" - isSuccess = true + If (objArgs.count > 1) Then + Select Case objArgs.Item(1) + Case "soffice" + isSuccess = TRUE + svcName = KTOFFICE + If (isServiceStarted(svcName)) Then + If (NOT stopService(svcName)) Then + isSuccess = false + writeLog "The " & KTOFFICE & " KnowledgeTree service could not be stopped. Result Code: " & lastErrorCode + Else + writeLog "The " & KTOFFICE & " KnowledgeTree service was successfully stopped" + End If + + writeLog "Successfully stopped " & KTOFFICE + Else + writeLog KTOFFICE & " already stopped. Result Code: " & lastErrorCode + End If + + If (isSuccess) Then + Wscript.Echo "The " & KTOFFICE & " KnowledgeTree service was successfully stopped" + Else + Wscript.Echo "The " & KTOFFICE & " KnowledgeTree service could not be stopped. Result Code: " & lastErrorCode + End If + + Case "scheduler" + isSuccess = TRUE + svcName = KTSCHEDULER + If (isServiceStarted(svcName)) Then + If (NOT stopService(svcName)) Then + isSuccess = false + writeLog "The " & KTSCHEDULER & " KnowledgeTree service could not be stopped. Result Code: " & lastErrorCode + Else + writeLog "The " & KTSCHEDULER & " KnowledgeTree service was successfully stopped" + End If + + writeLog "Successfully stopped " & KTSCHEDULER + Else + writeLog KTSCHEDULER & " already stopped. Result Code: " & lastErrorCode + End If + + If (isSuccess) Then + Wscript.Echo "The " & KTSCHEDULER & " KnowledgeTree service was successfully stopped" + Else + Wscript.Echo "The " & KTSCHEDULER & " KnowledgeTree service could not be stopped. Result Code: " & lastErrorCode + End If + + Case "lucene" + isSuccess = TRUE + svcName = KTLUCENE + If (isServiceStarted(svcName)) Then + If (NOT stopService(svcName)) Then + isSuccess = false + writeLog "The " & KTLUCENE & " KnowledgeTree service could not be stopped. Result Code: " & lastErrorCode + Else + writeLog "The " & KTLUCENE & " KnowledgeTree service was successfully stopped" + End If + + writeLog "Successfully stopped " & KTLUCENE + Else + writeLog KTLUCENE & " already stopped. Result Code: " & lastErrorCode + End If + + If (isSuccess) Then + Wscript.Echo "The " & KTLUCENE & " KnowledgeTree service was successfully stopped" + Else + Wscript.Echo "The " & KTLUCENE & " KnowledgeTree service could not be stopped. Result Code: " & lastErrorCode + End If + End Select + Else + 'Stopping all the services + isSuccess = TRUE + + svcName = KTOFFICE + If (isServiceStarted(svcName)) Then + If (NOT stopService(svcName)) Then + isSuccess = false + End If + writeLog "Successfully stopped " & KTOFFICE + Else + writeLog KTOFFICE & " already stopped. Result Code: " & lastErrorCode + End If + + svcName = KTSCHEDULER + If (isServiceStarted(svcName)) Then + If (NOT stopService(svcName)) Then + isSuccess = false + End If + writeLog "Successfully stopped " & KTSCHEDULER + Else + writeLog KTSCHEDULER & " already stopped. Result Code: " & lastErrorCode + End If + + svcName = KTLUCENE + If (isServiceStarted(svcName)) Then + If (NOT stopService(svcName)) Then + isSuccess = false + End If + writeLog "Successfully stopped " & KTLUCENE + Else + writeLog KTLUCENE & " already stopped. Result Code: " & lastErrorCode + End If + + If (isSuccess) Then + Wscript.Echo "The KnowledgeTree services were successfully stopped" + Else + Wscript.Echo "There were errors sopping the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')" + End If + End If + + Case "uninstall" + isSuccess = TRUE svcName = KTOFFICE - If (isServiceStarted(svcName)) Then - If (NOT stopService(svcName)) Then - isSuccess = false - End If + If (NOT uninstallService(svcName)) Then + isSuccess = false + writeLog "The KnowledgeTree KTOffice service could not be uninstalled" End If svcName = KTSCHEDULER - If (isServiceStarted(svcName)) Then - If (NOT stopService(svcName)) Then - isSuccess = false - End If + If (NOT uninstallService(svcName)) Then + isSuccess = false + writeLog "The KnowledgeTree KTScheduler service could not be uninstalled" End If svcName = KTLUCENE - If (isServiceStarted(svcName)) Then - If (NOT stopService(svcName)) Then - isSuccess = false - End If + If (NOT uninstallService(svcName)) Then + isSuccess = false + writeLog "The KnowledgeTree KTLucene service could not be uninstalled" End If If (isSuccess) Then - Wscript.Echo "The KnowledgeTree services were successfully stopped" + Wscript.Echo "The KnowledgeTree services were uninstalled" Else - Wscript.Echo "There were errors sopping the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')" + Wscript.Echo "There were errors uninstalling the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')" End If - Case "uninstall" - isSuccess = true ' Track if anything went wrong + Case "uninstall_old" 'Depricated : This method prevents verbose error logging, using WMI to uninstall instead + isSuccess = TRUE ' Track if anything went wrong - ' Stopping Then Uninstalling + ' Stopping Then FALSE 'svcName = KTOFFICE 'If (isServiceStarted(svcName)) Then ' If (NOT stopService(svcName)) Then @@ -211,16 +406,16 @@ Else ' End If 'End If - ' Uninstalling KTOffice + ' FALSE KTOffice result = exec("sc delete " & KTOFFICE) 'Uninstall Failed If result = 0 Then isSuccess = false - logEvent "The KnowledgeTree KTOffice service could not be uninstalled" + writeLog "The KnowledgeTree KTOffice service could not be uninstalled" End If - ' Stopping Then Uninstalling + ' Stopping Then FALSE 'svcName = KTSCHEDULER 'If (isServiceStarted(svcName)) Then ' If (NOT stopService(svcName)) Then @@ -228,32 +423,30 @@ Else ' End If 'End If - ' Uninstalling KTScheduler + ' FALSE KTScheduler result = exec("sc delete " & KTSCHEDULER) 'Uninstall Failed If result = 0 Then isSuccess = false - logEvent "The KnowledgeTree KTScheduler service could not be uninstalled" + writeLog "The KnowledgeTree KTScheduler service could not be uninstalled" End If - - - ' Stopping Then Uninstalling + ' Stopping Then FALSE 'svcName = KTLUCENE 'If (isServiceStarted(svcName)) Then ' If (NOT stopService(svcName)) Then - ' isSuccess = false + ' isSuccess = FALSE ' End If 'End If - ' Uninstalling KTLucene + ' FALSE KTLucene result = exec("sc delete " & KTLUCENE) 'Uninstall Failed If result = 0 Then - isSuccess = false - logEvent "The KnowledgeTree KTLucene service could not be uninstalled" + isSuccess = FALSE + writeLog "The KnowledgeTree KTLucene service could not be uninstalled" End If If (isSuccess) Then @@ -261,21 +454,56 @@ Else Else Wscript.Echo "There were errors uninstalling the KnowledgeTree services please see the log for details ('knowledgetree/var/log/dmsctl.log')" End If + Case Else Wscript.Echo strUsage End Select End If + +' Method to check if a service is installed +Public Function isServiceInstalled(ByVal svcName) + strComputer = "." + Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") + + ' Obtain an instance of the the class + ' using a key property value. + + isServiceInstalled = FALSE + + svcQry = "SELECT * from Win32_Service" + Set objOutParams = objWMIService.ExecQuery(svcQry) + + For Each objSvc in objOutParams + + Select Case objSvc.Name + Case svcName + isServiceInstalled = TRUE + End Select + + Next + + If (Not isServiceInstalled) Then + lastErrorCode = SVC_INVALID_NAME + End If +End Function + ' Method to interrogate a service Public Function isServiceStarted(ByVal svcName) + If (NOT isServiceInstalled( svcName )) Then + isServiceStarted = FALSE + Exit Function + End If + strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") + ' Obtain an instance of the the class ' using a key property value. Set objShare = objWMIService.Get("Win32_Service.Name='" & svcName & "'") - + ' no InParameters to define ' Execute the method and obtain the return status. @@ -283,16 +511,24 @@ Public Function isServiceStarted(ByVal svcName) ' is created by the provider. Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='" & svcName & "'", "InterrogateService") + lastErrorCode = objOutParams.ReturnValue + If (objOutParams.ReturnValue = SVC_SERVICE_NOT_ACTIVE) Then isServiceStarted = FALSE Else isServiceStarted = TRUE End If - + end Function ' Method to start a service Public Function startService(ByVal svcName) + If (NOT isServiceInstalled( svcName )) Then + writeLog "The KnowledgeTree " & svcName & " service could not be started because it isn't installed. Run 'dmsctl.vbs install' to correct." + startService = FALSE + Exit Function + End If + strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") ' Obtain an instance of the the class @@ -306,6 +542,8 @@ Public Function startService(ByVal svcName) ' is created by the provider. Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='" & svcName & "'", "StartService") + lastErrorCode = objOutParams.ReturnValue + If (objOutParams.ReturnValue = SVC_SUCCESS) Then startService = TRUE Else @@ -316,6 +554,12 @@ End Function ' Method to stop a service Public Function stopService(ByVal svcName) + If (NOT isServiceInstalled( svcName )) Then + writeLog "The KnowledgeTree " & svcName & " service could not be stopped because it isn't installed. Run 'dmsctl.vbs install' to correct." + stopService = FALSE + Exit Function + End If + strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") ' Obtain an instance of the the class @@ -328,6 +572,8 @@ Public Function stopService(ByVal svcName) ' The OutParameters object in objOutParams ' is created by the provider. Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='" & svcName & "'", "StopService") + + lastErrorCode = objOutParams.ReturnValue If (objOutParams.ReturnValue = SVC_SUCCESS) Then stopService = TRUE @@ -337,6 +583,49 @@ Public Function stopService(ByVal svcName) End Function +' Method to uninstall a service +Public Function uninstallService(ByVal svcName) + If (NOT isServiceInstalled( svcName )) Then + uninstallService = TRUE ' Service already uninstalled so return TRUE + Exit Function + End If + + wasStopped = TRUE + If (NOT stopService(svcName)) Then + wasStopped = FALSE + End If + + strComputer = "." + Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") + ' Obtain an instance of the the class + ' using a key property value. + Set objShare = objWMIService.Get("Win32_Service.Name='" & svcName & "'") + + ' no InParameters to define + + ' Execute the method and obtain the return status. + ' The OutParameters object in objOutParams + ' is created by the provider. + Set objOutParams = objWMIService.ExecMethod("Win32_Service.Name='" & svcName & "'", "Delete") + + lastErrorCode = objOutParams.ReturnValue + + If (objOutParams.ReturnValue = SVC_SUCCESS) Then + uninstallService = TRUE + Else + uninstallService = FALSE + + ' Extra Event logging to assist the Administrator + If (wasStopped) Then + writeLog "The KnowledgeTree " & svcName & " service could not be uninstalled because it could not be stopped. Stop this service manually before uninstalling." + uninstallService = FALSE ' Must be stop service before it can be uninstalled + End If + + End If + +End Function + + ' Execute a command Public Function exec(ByVal cmd) @@ -363,7 +652,7 @@ End Sub ' Event logging only works on Server 2003, 2000 and XP Public Sub logEvent(ByVal strMessage) - + If (NOT isWindowsVista() AND NOT isWindows7 AND NOT isWindows2008) Then Const EVENT_SUCCESS = 0 Set objShell = Wscript.CreateObject("Wscript.Shell") @@ -371,3 +660,13 @@ Public Sub logEvent(ByVal strMessage) End If End Sub + +' Event logging only works on Server 2003, 2000 and XP +Public Sub writeLog(ByVal strMessage) + Set objFSO = CreateObject("Scripting.FileSystemObject") + ForAppending = 8 + Set objTextFile = objFSO.OpenTextFile (currDir & "\var\log\dmsctl.log", ForAppending, True) + objTextFile.WriteLine strMessage + objTextFile.Close +End Sub +