Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Class WMIReader
- Private mColInstances
- Private mStrClassName
- Private mStrComputerName
- Private mStrUserName
- Private mStrPassword
- Private Sub Class_Initialize()
- PComputerName = Empty
- Set mColInstances = CreateObject("System.Collections.ArrayList")
- End Sub
- Public Sub WMIReader(ByVal strClassName)
- mStrClassName = strClassName
- End Sub
- Public Property Get LOCALHOST()
- LOCALHOST = "."
- End Property
- Public Property Let PComputerName(ByVal strComputerName)
- If IsEmpty(strComputerName) Then
- mStrComputerName = LOCALHOST
- Else
- If LCase(strComputerName) = "localhost" Then
- mStrComputerName = LOCALHOST
- Else
- mStrComputerName = strComputerName
- End If
- End If
- End Property
- Public Property Let PUsername(ByVal strUserName)
- mStrUserName = strUserName
- End Property
- Public Property Let PPassword(ByVal strPassword)
- mStrPassword = strPassword
- End Property
- Public Function ReadWMI()
- Const NAME_SPACE = "root\cimv2"
- Const IMPERSONATION_LEVEL_ANONYMOUS = 1
- Const IMPERSONATION_LEVEL_IDENTIFY = 2
- Const IMPERSONATION_LEVEL_IMPERSONATE = 3
- Const IMPERSONATION_LEVEL_DELEGATE = 4
- Dim objSWbemLocator
- Dim objConnectedServer
- Dim objClassInstances
- Dim objClassInstance
- Dim objProperty
- Dim objMessagesDictionary
- Dim strPropertyValue
- Dim strErrorMessage
- If Not IsEmpty(mStrClassName) Then
- On Error Resume Next
- Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
- Set objConnectedServer = objSWbemLocator.ConnectServer( _
- mStrComputerName, _
- NAME_SPACE, _
- mStrUserName, _
- mStrPassword _
- )
- objConnectedServer.Security_.ImpersonationLevel _
- = IMPERSONATION_LEVEL_IMPERSONATE
- Set objClassInstances = objConnectedServer.InstancesOf(mStrClassName)
- For Each objClassInstance In objClassInstances
- Set objMessagesDictionary = CreateObject("Scripting.Dictionary")
- For Each objProperty In objClassInstance.Properties_
- strMessage = ""
- If IsArray(objProperty.Value) Then
- For Each strPropertyValue In objProperty.Value
- strMessage = ConnectValue(strMessage, strPropertyValue)
- Next
- Else
- strMessage = objProperty.Value
- End If
- If Not objMessagesDictionary.Exists(objProperty.Name) Then
- objMessagesDictionary(objProperty.Name) = strMessage
- Else
- Call objMessagesDictionary.Add(objProperty.Name, strMessage)
- End If
- Next
- Set objProperty = Nothing
- Call mColInstances.Add(objMessagesDictionary)
- Next
- Set objClassInstance = Nothing
- Set objConnectedServer = Nothing
- Set objSWbemLocator = Nothing
- If Err.Number <> 0 Then
- strErrorMessage = ""
- strErrorMessage = strErrorMessage & "[Error] File open error..." & vbCrLf
- strErrorMessage = strErrorMessage & "Err.Number: " & Err.Number & vbCrLf
- strErrorMessage = strErrorMessage & "Err.Source: " & Err.Source & vbCrLf
- strErrorMessage = strErrorMessage & "Err.Description: " & Err.Description & vbCrLf
- Call WScript.Echo(strErrorMessage)
- End If
- Call Err.Clear
- End If
- Set ReadWMI = mColInstances
- End Function
- ' Private
- Private Function ConnectValue(ByVal strLine, ByVal strValue)
- Const SLASH = " / "
- Dim strConnectededLine
- strConnectededLine = strLine
- If IsEmpty(strConnectededLine) Then
- strConnectededLine = strValue
- Else
- strConnectededLine = strConnectededLine & SLASH & strValue
- End If
- ConnectValue = strConnectededLine
- End Function
- Private Sub Class_Terminate()
- Set mColInstances = Nothing
- End Sub
- End Class
- ' ### For debugging ###
- ' Save file as follow. Not include but execute directly.
- If WScript.ScriptName = "WMIReader.vbs" Then
- Const CLASS_NAME = "Win32_DiskDrive"
- Const COMPUTER_NAME = "localhost"
- Const USER_NAME = Empty
- Const PASSWORD = Empty
- Dim objWMIReader
- Dim colInstances
- Dim objMessagesDictionary
- Dim strPropertyName
- Dim strMessage
- Set objWMIReader = New WMIReader
- ' ### 1. Set class name
- Call objWMIReader.WMIReader(CLASS_NAME)
- ' ### 2. Set computer name if accessing a remote host.
- objWMIReader.PComputerName = COMPUTER_NAME
- ' ### 3. Set user name and password if accessing a remote host.
- objWMIReader.PUsername = USER_NAME
- objWMIReader.PPassword = PASSWORD
- ' ### 4. Get result.
- Set colInstances = objWMIReader.ReadWMI
- ' ### 5. Confirm
- strMessage = ""
- For Each objMessagesDictionary In colInstances
- strMessage = strMessage & objMessagesDictionary("Caption") & vbCrLf
- strMessage = strMessage & "===========================" & vbCrLf
- For Each strPropertyName In objMessagesDictionary.Keys
- strMessage = strMessage & strPropertyName & " : "
- strMessage = strMessage & objMessagesDictionary(strPropertyName) & vbCrLf
- Next
- strMessage = strMessage & vbCrLf
- Next
- Call WScript.Echo(strMessage)
- ' ### 6. End
- Set objMessagesDictionary = Nothing
- Set colInstances = Nothing
- Set objWMIReader = Nothing
- End If
Add Comment
Please, Sign In to add comment