Guest User

Untitled

a guest
Dec 11th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Class WMIReader
  4. Private mColInstances
  5. Private mStrClassName
  6. Private mStrComputerName
  7. Private mStrUserName
  8. Private mStrPassword
  9.  
  10. Private Sub Class_Initialize()
  11. PComputerName = Empty
  12. Set mColInstances = CreateObject("System.Collections.ArrayList")
  13. End Sub
  14.  
  15. Public Sub WMIReader(ByVal strClassName)
  16. mStrClassName = strClassName
  17. End Sub
  18.  
  19. Public Property Get LOCALHOST()
  20. LOCALHOST = "."
  21. End Property
  22.  
  23. Public Property Let PComputerName(ByVal strComputerName)
  24. If IsEmpty(strComputerName) Then
  25. mStrComputerName = LOCALHOST
  26. Else
  27. If LCase(strComputerName) = "localhost" Then
  28. mStrComputerName = LOCALHOST
  29. Else
  30. mStrComputerName = strComputerName
  31. End If
  32. End If
  33. End Property
  34.  
  35. Public Property Let PUsername(ByVal strUserName)
  36. mStrUserName = strUserName
  37. End Property
  38.  
  39. Public Property Let PPassword(ByVal strPassword)
  40. mStrPassword = strPassword
  41. End Property
  42.  
  43. Public Function ReadWMI()
  44. Const NAME_SPACE = "root\cimv2"
  45. Const IMPERSONATION_LEVEL_ANONYMOUS = 1
  46. Const IMPERSONATION_LEVEL_IDENTIFY = 2
  47. Const IMPERSONATION_LEVEL_IMPERSONATE = 3
  48. Const IMPERSONATION_LEVEL_DELEGATE = 4
  49. Dim objSWbemLocator
  50. Dim objConnectedServer
  51. Dim objClassInstances
  52. Dim objClassInstance
  53. Dim objProperty
  54. Dim objMessagesDictionary
  55. Dim strPropertyValue
  56. Dim strErrorMessage
  57. If Not IsEmpty(mStrClassName) Then
  58. On Error Resume Next
  59. Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
  60. Set objConnectedServer = objSWbemLocator.ConnectServer( _
  61. mStrComputerName, _
  62. NAME_SPACE, _
  63. mStrUserName, _
  64. mStrPassword _
  65. )
  66. objConnectedServer.Security_.ImpersonationLevel _
  67. = IMPERSONATION_LEVEL_IMPERSONATE
  68. Set objClassInstances = objConnectedServer.InstancesOf(mStrClassName)
  69. For Each objClassInstance In objClassInstances
  70. Set objMessagesDictionary = CreateObject("Scripting.Dictionary")
  71. For Each objProperty In objClassInstance.Properties_
  72. strMessage = ""
  73. If IsArray(objProperty.Value) Then
  74. For Each strPropertyValue In objProperty.Value
  75. strMessage = ConnectValue(strMessage, strPropertyValue)
  76. Next
  77. Else
  78. strMessage = objProperty.Value
  79. End If
  80. If Not objMessagesDictionary.Exists(objProperty.Name) Then
  81. objMessagesDictionary(objProperty.Name) = strMessage
  82. Else
  83. Call objMessagesDictionary.Add(objProperty.Name, strMessage)
  84. End If
  85. Next
  86. Set objProperty = Nothing
  87. Call mColInstances.Add(objMessagesDictionary)
  88. Next
  89. Set objClassInstance = Nothing
  90. Set objConnectedServer = Nothing
  91. Set objSWbemLocator = Nothing
  92. If Err.Number <> 0 Then
  93. strErrorMessage = ""
  94. strErrorMessage = strErrorMessage & "[Error] File open error..." & vbCrLf
  95. strErrorMessage = strErrorMessage & "Err.Number: " & Err.Number & vbCrLf
  96. strErrorMessage = strErrorMessage & "Err.Source: " & Err.Source & vbCrLf
  97. strErrorMessage = strErrorMessage & "Err.Description: " & Err.Description & vbCrLf
  98. Call WScript.Echo(strErrorMessage)
  99. End If
  100. Call Err.Clear
  101. End If
  102. Set ReadWMI = mColInstances
  103. End Function
  104.  
  105. ' Private
  106. Private Function ConnectValue(ByVal strLine, ByVal strValue)
  107. Const SLASH = " / "
  108. Dim strConnectededLine
  109. strConnectededLine = strLine
  110. If IsEmpty(strConnectededLine) Then
  111. strConnectededLine = strValue
  112. Else
  113. strConnectededLine = strConnectededLine & SLASH & strValue
  114. End If
  115. ConnectValue = strConnectededLine
  116. End Function
  117.  
  118. Private Sub Class_Terminate()
  119. Set mColInstances = Nothing
  120. End Sub
  121. End Class
  122.  
  123. ' ### For debugging ###
  124. ' Save file as follow. Not include but execute directly.
  125. If WScript.ScriptName = "WMIReader.vbs" Then
  126. Const CLASS_NAME = "Win32_DiskDrive"
  127. Const COMPUTER_NAME = "localhost"
  128. Const USER_NAME = Empty
  129. Const PASSWORD = Empty
  130. Dim objWMIReader
  131. Dim colInstances
  132. Dim objMessagesDictionary
  133. Dim strPropertyName
  134. Dim strMessage
  135.  
  136. Set objWMIReader = New WMIReader
  137. ' ### 1. Set class name
  138. Call objWMIReader.WMIReader(CLASS_NAME)
  139. ' ### 2. Set computer name if accessing a remote host.
  140. objWMIReader.PComputerName = COMPUTER_NAME
  141. ' ### 3. Set user name and password if accessing a remote host.
  142. objWMIReader.PUsername = USER_NAME
  143. objWMIReader.PPassword = PASSWORD
  144. ' ### 4. Get result.
  145. Set colInstances = objWMIReader.ReadWMI
  146. ' ### 5. Confirm
  147. strMessage = ""
  148. For Each objMessagesDictionary In colInstances
  149. strMessage = strMessage & objMessagesDictionary("Caption") & vbCrLf
  150. strMessage = strMessage & "===========================" & vbCrLf
  151. For Each strPropertyName In objMessagesDictionary.Keys
  152. strMessage = strMessage & strPropertyName & " : "
  153. strMessage = strMessage & objMessagesDictionary(strPropertyName) & vbCrLf
  154. Next
  155. strMessage = strMessage & vbCrLf
  156. Next
  157. Call WScript.Echo(strMessage)
  158. ' ### 6. End
  159. Set objMessagesDictionary = Nothing
  160. Set colInstances = Nothing
  161. Set objWMIReader = Nothing
  162. End If
Add Comment
Please, Sign In to add comment