Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listwmiclasses.vbs

(view source code of listwmiclasses.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim arrClasses( ), arrNameSpaces( )
  4. Dim blnRC, blnRecursive
  5. Dim intRC, intSize, intValid, i, j, k
  6. dim objNameSpace, objNewOption
  7. Dim strHolder, strNameSpace
  8.  
  9. blnRC        = False
  10. blnRecursive = True
  11. intRC        = 0
  12.  
  13. With WScript.Arguments
  14. 	intValid = 0
  15. 	If .Named.Exists( "NR" ) Then
  16. 		intValid = intValid + 1
  17. 		blnRC    = True
  18. 	End If
  19. 	If .Named.Exists( "RC" ) Then
  20. 		intValid = intValid + 1
  21. 		blnRC    = True
  22. 	End If
  23. 	Select Case .Unnamed.Count
  24. 		Case 0
  25. 			strNameSpace = "root"
  26. 		Case 1
  27. 			strNameSpace = .Unnamed(0)
  28. 			If Left( LCase( strNameSpace ), 4 ) <> "root" Then
  29. 				If InStr( strNameSpace, "?" ) < 1 Then
  30. 					WScript.Echo "Error: invalid namespace """ & strNameSpace & """"
  31. 				End If
  32. 				Syntax
  33. 			End If
  34. 			intValid     = intValid + 1
  35. 		Case Else
  36. 			Syntax
  37. 	End Select
  38. 	If intValid <> .Count Then Syntax
  39. End With
  40.  
  41. intSize = 0
  42. ReDim Preserve arrNameSpaces(0)
  43. arrNameSpaces(0) = strNameSpace
  44. If Not WScript.Arguments.Named.Exists( "NR" ) Then
  45. 	' Get a list of available namespaces
  46. 	EnumNameSpaces( arrNameSpaces(0) )
  47. 	' Sort the array of namespaces
  48. 	If UBound( arrNameSpaces ) > 1 Then
  49. 		For i = UBound( arrNameSpaces ) - 1 To 0 Step -1
  50. 			For j= 0 to i
  51. 				k = j + 1
  52. 				If UCase( arrNameSpaces(j) ) > UCase( arrNameSpaces(k) ) Then
  53. 					strHolder     = arrNameSpaces(k)
  54. 					arrNameSpaces(k) = arrNameSpaces(j)
  55. 					arrNameSpaces(j) = strHolder
  56. 				End If
  57. 			Next
  58. 		Next
  59. 	End If
  60. End If
  61.  
  62. ' List the classes for each namespace
  63. For i = 0 To UBound( arrNameSpaces )
  64. 	ListClasses( arrNameSpaces(i) )
  65. Next
  66.  
  67. If blnRC Then WScript.Quit intRC
  68.  
  69.  
  70. Sub EnumNameSpaces( myNameSpace )
  71. 	Dim colNameSpaces, intSize, objNameSpace, objWMIService
  72.  
  73. 	intSize = UBound( arrNameSpaces )
  74.  
  75. 	On Error Resume Next
  76.  
  77. 	Set objWMIService = GetObject ( "winmgmts:{impersonationLevel=impersonate}//./" & myNameSpace )
  78. 	If Err Then
  79. 		WScript.Echo "WMI Error #" & Err.Number & ": " & Err.Description
  80. 		Syntax
  81. 	End If
  82. 	Set colNameSpaces = objWMIService.InstancesOf( "__NAMESPACE" )
  83. 	For Each objNameSpace In colNameSpaces
  84. 		intSize = UBound( arrNameSpaces ) + 1
  85. 		ReDim Preserve arrNameSpaces( intSize )
  86. 		arrNameSpaces( intSize ) = myNameSpace & "/" & objNameSpace.Name
  87. 		EnumNameSpaces myNameSpace & "/" & objNameSpace.Name
  88. 	Next
  89.  
  90. 	On Error Goto 0
  91. End Sub
  92.  
  93.  
  94. Sub ListClasses( myNameSpace )
  95. 	Dim colClasses, intSize, i, j, k, objClass, objWMIService, strHolder
  96.  
  97. 	If myNameSpace = "root" Then Exit Sub
  98.  
  99. 	Set objWMIService = GetObject( "winmgmts:{impersonationLevel=impersonate}" & myNameSpace )
  100. 	If Err Then
  101. 		MsgBox "Could not connect to " & myNameSpace, vbOKOnly, "NameSpace Error"
  102. 		WScript.Quit 1
  103. 	End If
  104. 	Set colClasses    = objWMIService.SubClassesOf
  105. 	If Err Then
  106. 		MsgBox "Error " & Err.Number & ": " & Err.Description, vbOKOnly, "Error"
  107. 		WScript.Quit 1
  108. 	End If
  109.  
  110. 	' Store the list of classes in an array
  111. 	intSize = 0
  112. 	For Each objClass In colClasses
  113. 		ReDim Preserve arrClasses( intSize )
  114. 		arrClasses( intSize ) = objClass.Path_.Class
  115. 		intSize = intSize + 1
  116. 		intRC   = intRC + 1
  117. 	Next
  118.  
  119. 	If IsArray( arrClasses ) Then
  120. 		If UBound( arrClasses ) > 1 Then
  121. 			' Sort the classes
  122. 			For i = ( UBound( arrClasses ) - 1 ) To 0 Step -1
  123. 				For j= 0 to i
  124. 					k = j + 1
  125. 					If UCase( arrClasses(j) ) > UCase( arrClasses(k) ) Then
  126. 						strHolder     = arrClasses(k)
  127. 						arrClasses(k) = arrClasses(j)
  128. 						arrClasses(j) = strHolder
  129. 					End If
  130. 				Next
  131. 			Next
  132. 		End if
  133.  
  134. 		' Display the classes list
  135. 		For i = 0 To UBound( arrClasses )
  136. 			WScript.Echo myNameSpace & ":" & arrClasses(i)
  137. 		Next
  138. 	End If
  139. End Sub
  140.  
  141.  
  142. Sub Syntax
  143. 	Dim strMsg
  144. 	strMsg = vbCrLf _
  145. 	       & "ListWMIClasses.vbs,  Version 3.01" _
  146. 	       & vbCrLf _
  147. 	       & "List all WMI classes available in the specified namespace" _
  148. 	       & vbCrLf & vbCrLf _
  149. 	       & "Usage:  CSCRIPT //NoLogo LISTWMICLASSES.VBS  [ namespace ]  [ /NR ]  [ /RC ]" _
  150. 	       & vbCrLf & vbCrLf _
  151. 	       & "Where:  ""namespace""  is the WMI namespace to be queried (default: ""root"")" _
  152. 	       & vbCrLf _
  153. 	       & "        /NR          do not recurse through namespaces" _
  154. 	       & vbCrLf _
  155. 	       & "        /RC          returns total number of classes found in return code" _
  156. 	       & vbCrLf & vbCrLf _
  157. 	       & "Written by Rob van der Woude" _
  158. 	       & vbCrLf _
  159. 	       & "http://www.robvanderwoude.com"
  160. 	WScript.Echo strMsg
  161. 	If blnRC Then
  162. 		WScript.Quit 0
  163. 	Else
  164. 		WScript.Quit 1
  165. 	End If
  166. End Sub

page last uploaded: 2016-12-15, 11:20