/build/static/layout/Breadcrumb_cap_w.png

Quering WMI in Windows 7

Hi Guys

I am trying to deploy Lotus Notes 8.0.2 over the top of 8.0.1 in a Windows 7 environment which works OK using the setup.exe supplied, but am getting a popup during the upgrade which refers to the 'multi user cleanup service' service which requires to be closed down. Apparently this service is for 'Notes multi user environments' and the popup error only occurs if are installing Notes for a single user environment and is therefore a known issue, which the vendor hasnt fixed.

To the point...I have attempted to use numerous scripts to stop the service but all fail. On further research I am not able to stop any service addresssing WMI on the Windows 7 platform.

Has any one had any success with a script which perhaps addresses issues for Windows 7 I have not thought of.

Here is the script I want to use, but which doesnt stop the service.


Thank you for any assistance!!

Option Explicit
'on error resume next

Dim WSHShell
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, colServiceList, Return

Set WSHShell = CreateObject("WScript.Shell")
strComputer = "."

'On Error Resume Next
' NB strService is case sensitive.
strService = "'Multi-user Cleanup Service'"
wscript.echo strService

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")

For Each objService in colListOfServices
objService.StopService()
WScript.Echo "Return value: " & Return

Next

0 Comments   [ + ] Show comments

Answers (4)

Posted by: anonymous_9363 13 years ago
Red Belt
0
I'm not sure what you mean. Is INSTALL.VBS the script which stops the service? If so, whatever is supposed to happen after INSTALL.VBS has run could be included in that file, after the call to ControlServiceWMI:blnReturn = ControlServiceWMI(".", strServiceName, strServiceDisplayName, strControlWord, intWait, strErrorMsg)

If Not blnReturn Then
'Call RecordErrorMgsBox(strErrorMsg) '// Use this error reporting sub when run as CScript/WScript-hosted VB Script
Call RecordErrorMSI(strErrorMsg) '// Use this error reporting sub for MSI-hosted VB Script
End If

'// If we got here, the call to ControlServiceWMI succeeded, so add code for whatever the next stage is after this.
Posted by: krpa 13 years ago
Senior Yellow Belt
0
Yes INSTALL.VBS begins with stopping the service and then goes on to run setup.exe to install Lotus Notes. What I was trying to say is INSTALL.VBS has to run in WIN 7 Admin mode otherwise I cant stop the service. The 'runas'solution code in my calling script enables me to call the second script INSTALL.VBS and run that in admin mode.

I was just wondering if somehow I could combine the logic of the two scripts (the outcome being one script), which would seem neater. Or is there another way to script a 'real' admin install in Win7.

Dont reply if you are too busy VB as the solution I have described works, I was basically looking to get your feedback to see if I could improve th e solution by having only one script.

Thanks
Posted by: anonymous_9363 13 years ago
Red Belt
0
Does the account under which the script executes have sufficient rights to stop services?

BTW, there's no point in echoing the 'Return' variable, as it's empty. Here's a script which I've used countless times. Your account still needs appropriate permissions, though:'// Copyright (c) DeepNet Limited, Ian Northwood
'// Programming obfuscation a speciality

Option Explicit

Dim blnReturn
Dim objWSHShell
Dim objFSO
Dim strServiceName
Dim strServiceDisplayName
Dim strControlWord
Dim intWait
Dim strErrorMsg

Set objWSHShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

strServiceName = "" '// Process (EXE) name
strServiceDisplayName = "User Authentication Manager" '// Display name as it appears in 'NET START'
strControlWord = "START" '// Operation to perform. Can be:
'// START
'// STOP
'// PAUSE
'// CONTINUE
intWait = 5 '// Number of seconds to wait before deciding operation
'// has failed

'blnReturn = ControlServiceADSI(".", strServiceName, strServiceDisplayName, strControlWord, intWait, strErrorMsg)
blnReturn = ControlServiceWMI(".", strServiceName, strServiceDisplayName, strControlWord, intWait, strErrorMsg)

If Not blnReturn Then
'Call RecordErrorMgsBox(strErrorMsg) '// Use this error reporting sub when run as CScript/WScript-hosted VB Script
Call RecordErrorMSI(strErrorMsg) '// Use this error reporting sub for MSI-hosted VB Script
End If

Set objFSO = Nothing
Set objWSHShell = Nothing

Sub RecordErrorMgsBox(ByVal strMsg)
objWSHShell.LogEvent 1, strMsg
'MsgBox strMsg
End Sub

Sub RecordErrorMSI(ByVal strMsg)
Const msiMessageTypeFatalExit = &H00000000 '// Premature termination, possibly fatal out of memory.
Const msiMessageTypeError = &H01000000 '// Formatted error message, [1] is message number in Error table.
Const msiMessageTypeWarning = &H02000000 '// Formatted warning message, [1] is message number in Error table.
Const msiMessageTypeUser = &H03000000 '// User request message, [1] is message number in Error table.
Const msiMessageTypeInfo = &H04000000 '// Informative message for log, not to be displayed.
Const msiMessageTypeFilesInUse = &H05000000 '// List of files in use that need to be replaced.
Const msiMessageTypeResolveSource = &H06000000 '// Request to determine a valid source location.
Const msiMessageTypeOutOfDiskSpace = &H07000000 '// Insufficient disk space message.
Const msiMessageTypeActionStart = &H08000000 '// Start of action,
'// [1] action name,
'// [2] description,
'// [3] template for ACTIONDATA messages.
Const msiMessageTypeActionData = &H09000000 '// Action data. Record fields correspond to the template of ACTIONSTART message.
Const msiMessageTypeProgress = &H0A000000 '// Progress bar information. See the description of record fields below.
Const msiMessageTypeCommonData = &H0B000000 '// To enable the Cancel button set [1] to 2 and [2] to 1.
'// To disable the Cancel button set [1] to 2 and [2] to 0
Dim objMSIRecord

'// Make an entry in Event Log
objWSHShell.LogEvent 1, strMsg

'// Make an entry in MSI log
Set objMSIRecord = Session.Installer.CreateRecord(0)
objMSIRecord.StringData(0) = strMsg
Session.Message msiMessageTypeError, objMSIRecord

Set objMSIRecord = Nothing
End Sub

'// =========================================================================================================
'// Name: ControlServiceADSI
'// Purpose: Controls services using ADSI
'// Input: strComputer - String - the computer on which we want to control the service. Null means local machine
'// strService - String - the name of the service. If blank, the function will use strDisplayName
'// strDisplayName - String - the display name of the service. If blank, the function will use strService
'// strOperation - String - the operation for the service: START, STOP, PAUSE or CONTINUE
'// intWaitTime - Integer - nbr of times to loop (effectively, the nbr of seconds to wait
'// for 'strOperation' to complete
'// Output: strError - String - contains error text, if operation fails
'// Returns: True/False
'// =========================================================================================================
Function ControlServiceADSI(ByVal strComputer, ByVal strService, ByVal strDisplayName, ByVal strOperation, ByVal intWaitTime, ByRef strError)

'// Define ADSI status constants
Const ADS_SERVICE_STOPPED = 1
Const ADS_SERVICE_START_PENDING = 2
Const ADS_SERVICE_STOP_PENDING = 3
Const ADS_SERVICE_RUNNING = 4
Const ADS_SERVICE_CONTINUE_PENDING = 5
Const ADS_SERVICE_PAUSE_PENDING = 6
Const ADS_SERVICE_PAUSED = 7
Const ADS_SERVICE_ERROR = 8

'// Define string constants for service methods
Const START_SERVICE = "START"
Const STOP_SERVICE = "STOP"
Const PAUSE_SERVICE = "PAUSE"
Const CONTINUE_SERVICE = "CONTINUE"

Dim strMsgText
Dim objComputer
Dim objService
Dim intCounter

ControlServiceADSI = False

On Error Resume Next

If Len(strService) = 0 And Len(strDisplayName) = 0 Then
strMsgText = ""
strMsgText = strMsgText & "Neither the service name and service display name were specified."
Exit Function
End If

'// Get ADSI objects and initial variables
Set objComputer = GetObject("WinNT://" & strComputer & ",computer")
Set objService = objComputer.GetObject("Service", strService)

'// Determine the operation and carry it out
With objService
Select Case (strOperation)
Case START_SERVICE
If .Status = ADS_SERVICE_STOPPED Then
.Start

Do
If .Status = ADS_SERVICE_RUNNING Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If

Case STOP_SERVICE
If .Status = ADS_SERVICE_RUNNING Or .Status = ADS_SERVICE_PAUSED Then
.Stop

Do
If .Status = ADS_SERVICE_STOPPED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If

Case PAUSE_SERVICE
If .Status = ADS_SERVICE_RUNNING Then
.Pause

Do
If .Status = ADS_SERVICE_PAUSED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If

Case CONTINUE_SERVICE
If .Status = ADS_SERVICE_PAUSED Then
.Continue
Do
If .Status = ADS_SERVICE_RUNNING Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End Select
End With

If Err.Number = 0 Then
ControlServiceADSI = True
Else
strMsgText = ""
strMsgText = strMsgText & "Error " & Err.Number & " occured."

If Len(Err.Description) > 0 Then
strMsgText = strMsgText & Err.Description
End If

strError = strMsgText
End If

On Error Goto 0

End Function

'// =========================================================================================================
'// Name: ControlServiceWMI
'// Purpose: Controls services using WMI
'// Input: strComputer - String - the computer on which we want to control the service. Null means local machine
'// strService - String - the name of the service. If blank, the function will use strDisplayName
'// strDisplayName - String - the display name of the service. If blank, the function will use strService
'// strOperation - String - the operation for the service: START, STOP, PAUSE or CONTINUE
'// intWaitTime - Integer - nbr of times to loop (effectively, the nbr of seconds to wait
'// for 'strOperation' to complete
'// Output: strError - String - contains error text, if operation fails
'// Returns: True/False
'// =========================================================================================================
Function ControlServiceWMI(ByVal strComputer, ByVal strService, ByVal strDisplayName, ByVal strOperation, ByVal intWaitTime, ByRef strError)

'// Define WMI *state* constants these are for the 'State' property
Const WMI_SERVICE_STOPPED = "Stopped"
Const WMI_SERVICE_STARTED = "Running"
Const WMI_SERVICE_START_PENDING = "Start Pending"
Const WMI_SERVICE_STOP_PENDING = "Stop Pending"
Const WMI_SERVICE_RUNNING = "Running"
Const WMI_SERVICE_CONTINUE_PENDING = "Continue Pending"
Const WMI_SERVICE_PAUSE_PENDING = "Pause Pending"
Const WMI_SERVICE_PAUSED = "Paused"
Const WMI_SERVICE_ERROR = "Unknown"

'// Define WMI *status* constants these are for the 'Status' property
Const WMI_SERVICE_OK = "OK"
Const WMI_SERVICE_DEGRADED = "Degraded"
Const WMI_SERVICE_UNKNOWN = "Unknown"
Const WMI_SERVICE_PRED_FAIL = "Pred Fail"
Const WMI_SERVICE_STARTING = "Starting"
Const WMI_SERVICE_STOPPING = "Stopping"
Const WMI_SERVICE_SERVICE = "Service" '// ?

'// Define string constants for service methods
Const START_SERVICE = "START"
Const STOP_SERVICE = "STOP"
Const PAUSE_SERVICE = "PAUSE"
Const CONTINUE_SERVICE = "CONTINUE"
Const SET_AUTOMATIC = "AUTOMATIC"
Const SET_MANUAL = "MANUAL"
Const SET_DISABLED = "DISABLED"

'// Win32_Service Method Return Value Table
Const WMI_Success = 0
Const WMI_NotSupported = 1
Const WMI_AccessDenied = 2
Const WMI_DependentServicesRunning = 3
Const WMI_InvalidServiceControl = 4
Const WMI_ServiceCannotAcceptControl = 5
Const WMI_ServiceNotActive = 6
Const WMI_ServiceRequestTimeout = 7
Const WMI_UnknownFailure = 8
Const WMI_PathNotFound = 9
Const WMI_ServiceAlreadyRunning = 10
Const WMI_ServiceDatabaseLocked = 11
Const WMI_ServiceDependencyDeleted = 12
Const WMI_ServiceDependencyFailure = 13
Const WMI_ServiceDisabled = 14
Const WMI_ServiceLogonFailure = 15
Const WMI_ServiceMarkedForDeletion = 16
Const WMI_ServiceNoThread = 17
Const WMI_StatusCircularDependency = 18
Const WMI_StatusDuplicateName = 19
Const WMI_StatusInvalidName = 20
Const WMI_StatusInvalidParameter = 21
Const WMI_StatusInvalidServiceAccount = 22
Const WMI_StatusServiceExists = 23
Const WMI_ServiceAlreadyPaused = 24

'// Build an array of the possible return values
Dim strWMI_ReturnErrors
Dim arrWMI_ReturnErrors

strWMI_ReturnErrors = ""
strWMI_ReturnErrors = strWMI_ReturnErrors & "Success,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Not Supported,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Access Denied,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Dependent Services Running,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Invalid Service Control,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Cannot Accept Control,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Not Active,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Request Timeout,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Unknown Failure,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Path Not Found,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Already Running,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Database Locked,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Dependency Deleted,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Dependency Failure,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Disabled,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Logon Failure,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Marked For Deletion,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service No Thread,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Circular Dependency,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Duplicate Name,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Invalid Name,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Invalid Parameter,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Invalid Service Account,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Status Service Exists,"
strWMI_ReturnErrors = strWMI_ReturnErrors & "Service Already Paused"

'// Just in case you left the trailing comma in place...
If Left(strWMI_ReturnErrors, 1) = "," Then
strWMI_ReturnErrors = Left(strWMI_ReturnErrors, Len(strWMI_ReturnErrors) - 1)
End If

arrWMI_ReturnErrors = Split(strWMI_ReturnErrors, ",")

Dim strQuery
Dim objComputer
Dim objServiceList
Dim objService
Dim intCounter
Dim blnServiceReturn

ControlServiceWMI = False

If Len(strService) = 0 And Len(strDisplayName) = 0 Then
strMsgText = ""
strMsgText = strMsgText & "Neither the service name and service display name were specified."
Exit Function
End If

On Error Resume Next

'// Get WMI objects and initial variables
'Set objComputer = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objComputer = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

If Err.Number = 0 Then
'// Only interested in controllable services (i.e. not system services, drivers, etc)
'// (well, we would be if ANY service set its 'AcceptStop' flag...)
strQuery = ""
strQuery = strQuery & "Select * From Win32_Service"
strQuery = strQuery & " Where "

If Len(strService) > 0 Then
strQuery = strQuery & "Name = '" & strService & "'"
If Len(strDisplayName) > 0 Then
strQuery = strQuery & " And "
strQuery = strQuery & "DisplayName = '" & strDisplayName & "'"
End If
Else
strQuery = strQuery & "DisplayName = '" & strDisplayName & "'"
End If

'strQuery = strQuery & " And "
'strQuery = strQuery & "AcceptStop = True"

Set objServiceList = objComputer.ExecQuery (strQuery)

If Err.Number = 0 Then
If objServiceList.Count > 0 Then
For Each objService in objServiceList
'// Determine the operation and carry it out
With objService
Select Case (strOperation)
Case SET_AUTOMATIC
If (.StartMode <> SET_AUTOMATIC) Then
Err.Number = .ChangeStartMode("Automatic")
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STARTED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If

Case SET_MANUAL
If (.StartMode <> SET_MANUAL) Then
Err.Number = .ChangeStartMode("Manual")
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STARTED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If

Case SET_DISABLED
If (.StartMode <> SET_DISABLED) Then
Err.Number = .ChangeStartMode("Disabled")
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STARTED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If

Case START_SERVICE
If (.State = WMI_SERVICE_STOPPED) Then
Err.Number = .StartService()
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STARTED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If

Case STOP_SERVICE
If (.State = WMI_SERVICE_RUNNING) Or (.State = WMI_SERVICE_PAUSED) Then
Err.Number = .StopService()
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_STOPPED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If

Case PAUSE_SERVICE
If (.State = WMI_SERVICE_RUNNING) Then
Err.Number = .PauseService()
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_PAUSED Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If

Case CONTINUE_SERVICE
If (.State = WMI_SERVICE_PAUSED) Then
Err.Number = .ContinueService()
If Err.Number <> 0 Then
Do
If .State = WMI_SERVICE_RUNNING Then
Exit Do
End If
Call Sleep(1)
intCounter = intCounter + 1
Loop Until intCounter = intWaitTime '// We're only going to wait intWaitTime seconds
End If
End If
End Select
End With
Next
Else
With Err
.Description = "The service name specified "
.Raise 999

If Len(strService) > 0 Then
If Len(strDisplayName) > 0 Then
.Description = .Description & "'" & strDisplayName & "'"
End If
Else
.Description = .Description & "=" & strService & "="
End If

.Description = .Description & " does not exist."
.Source = "ControlServiceWMI"
End With
End If
End If
End If

If Err.Number = 0 Then
ControlServiceWMI = True
Else
'// Loop through the error array and, when you hit what Err.Number is,
'// drop out and set the appropriate error text
For intCounter = 0 To UBound(arrWMI_ReturnErrors)
If Err.Number = intCounter Then
Err.Description = arrWMI_ReturnErrors(intCounter)
End If
Next

strMsgText = ""
strMsgText = strMsgText & "Error " & Err.Number & " occured."

If Len(Err.Description) > 0 Then
strMsgText = strMsgText & Err.Description
End If

strError = strMsgText
End If

On Error Goto 0

End Function

Sub Sleep(ByVal intDelayInSecs)
'// Sleep is here because, of course, one can't use WScript.Sleep in embedded VBS CAs
Dim datStart
Dim blnCompleted

datStart = Now()
blnCompleted = False

While Not blnCompleted
If DateDiff("s", datStart, Now()) >= CInt(intDelayInSecs) Then
blnCompleted = True
End If
Wend
End Sub
Posted by: krpa 13 years ago
Senior Yellow Belt
0
script executes have sufficient rights to stop services

Thanks VB. You were right about the rights. Giving the script elevated rights through use of ShellExecute method of Shell.Application object solved my problem.

My solution requires an additional script to call my script which stops the service:

Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = FSO.GetParentFolderName (WScript.ScriptFullName)
If FSO.FileExists(strPath & "\Install.vbs") Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\Install.VBS" & Chr(34), "", "runas", 1
Else
MsgBox "Script file Install.vbs not found"
End If

Do you think this solution is acceptable? It would be nice to have code in the same script but that would probably end up recursive.

Thanks again for taking the trouble to assist me, and providing your code!!
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ