Enumerating services on Win 2000,2003 SP2
Hi
I use a VBS script, that starts to work improperly on some Windows 2000,2003
with SP2. It executes query:
SELECT DisplayName,Name,State,StartMode,StartName,Description FROM
Win32_Service
as a LocalSystem (script is called by a service). Function ExecQuery returns
a result with no error, but enumeration of the results ("for each item in
...) causes the error:
Error: 70 (Hex: 46) - Permission denied
Other queries (for example: SELECT * FROM Win32_OperatingSystem), returns
correct results, so I conclude, that WMI and DCOM are set properly.
I've checked specific rights for querying Win32_Service
(SC_MANAGER_ENUMERATE_SERVICE,SC_MANAGER_CONNECT, etc), and it seems, that
all required rights are set.
Unfortunately, on my local Windows all works right, and I have no acces to
systems with problem, I described.
Please write me, what I should check to find a reason of such a behavior, or
what is wrong in my script...
Below I attached script, and result of script execution obtained by execute
it with "at" command.
Best regards
---------------- script:
On Error Resume Next
On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Dim pQuery
Dim objWmiService
Call Init
Call DisplayError
Call DisplayError
pQuery = "SELECT DisplayName,Name,State,StartMode,StartName,Description FROM
Win32_Service"
Call RunQuery
Call DisplayError
pQuery = "SELECT * FROM Win32_OperatingSystem"
Call RunQuery
Call DisplayError
' END
Sub DisplayError
If Err.Number <> 0 Then
Wscript.Echo "Error: " & Err.Number
Wscript.Echo "Error (Hex): " & Hex(Err.Number)
Wscript.Echo "Source: " & Err.Source
Wscript.Echo "Description: " & Err.Description
Err.Clear
End if
End Sub
Sub RunQuery
Dim counter
Dim key
Dim value
WScript.Echo "# " & pQuery
Set items = objWMIService.ExecQuery(pQuery, "WQL",
wbemFlagReturnImmediately + wbemFlagForwardOnly)
counter = 0
For Each item In items
For Each prop in item.Properties_
key = ""
value = ""
key = prop.name
If (Not IsNull(prop) ) Then
If ( prop.IsArray ) Then
value = Join(prop, ",")
Else
value = replace(replace(prop.value,vbCr,""),vbLf," ")
End If
End If
WScript.Echo key & "=" & value
Next
WScript.Echo " "
counter = counter + 1
Next
WScript.Echo "[CLASS_STATISTICS]"
WScript.Echo "count=" & counter
WScript.Echo " "
End Sub
Sub Init
Set objWMIService =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
If (Err.number <> 0) Then
If IsNull(wmiService) Then
WScript.StdErr.Write "[ERROR] WMI service not available."
Else
WScript.StdErr.Write "[ERROR] " & Err.description
End If
WScript.Quit(-1)
End If
End Sub
----------- results:
# SELECT DisplayName,Name,State,StartMode,StartName,Description FROM
Win32_Service
Error: 70
Error (Hex): 46
Source: Microsoft VBScript runtime error
Description: Permission denied
# SELECT * FROM Win32_OperatingSystem
BootDevice=\Device\HarddiskVolume1
BuildNumber=3790
BuildType=Multiprocessor Free
/-----------cut/ - further output looks OK...
date: Mon, 11 Aug 2008 05:08:01 -0700
author: Arek