locked
How to get the result of a getProperties method of a wmi class in vb.net using managementobjectsearcher RRS feed

  • Question

  • 'Connects to the remote PLT
       Set objWMIService1 = GetObject( "winmgmts:" _
                                      & "{authenticationLevel=pktPrivacy}!\\" _
                                      & strPLT _
                                      & "\root\CIMV2\Security\MicrosoftVolumeEncryption")

       Set colItems1 = objWMIService1.ExecQuery("SELECT * FROM Win32_EncryptableVolume",,48)

       Dim arEncryptionMethod
       arEncryptionMethod = Array( "None", "AES 128 With Diffuser", "AES 256 With Diffuser", "AES 128", "AES 256" )

       Dim arProtectionStatus
       arProtectionStatus = Array( "Protection Off", "Protection On", "Protection Unknown" )

       Dim arConversionStatus
       arConversionStatus = Array( "Fully Decrypted", "Fully Encrypted", "Encryption In Progress", "Decryption In Progress", "Encryption Paused", "Decryption Paused" )

       Dim arLockStatus
       arLockStatus = Array( "Unlocked", "Locked" )
     
      'Gets the Operating system of the PLT
       OSReturn = GetOS(strPLT)

       'Gets the BDE Status
       For Each objItem1 in colItems1
          GetBDEStatus = "Success"   
          Dim EncryptionMethod
          Dim ProtectionStatus
          Dim ConversionStatus
          Dim EncryptionPercentage      
          Dim VolumeKeyProtectorID
          Dim LockStatus
          Dim completeVolumeKeyProtectorFriendlyName

          objItem1.GetEncryptionMethod EncryptionMethod
          objItem1.GetProtectionStatus ProtectionStatus
          objItem1.GetConversionStatus ConversionStatus, EncryptionPercentage
          objItem1.GetKeyProtectors 0,VolumeKeyProtectorID
          objItem1.GetLockStatus LockStatus

          'Writes in the Log file  
          wscript.echo "DeviceID:             " & objItem1.DeviceID
          wscript.echo "DriveLetter:          " & objItem1.DriveLetter
          wscript.echo "EncryptionMethod:     " & arEncryptionMethod(EncryptionMethod) & " (" & EncryptionMethod & ")"
          wscript.echo "ProtectionStatus:     " & arProtectionStatus(ProtectionStatus) & " (" & ProtectionStatus & ")"
          wscript.echo "ConversionStatus:     " & arConversionStatus(ConversionStatus) & " (" & ConversionStatus & ")"
          wscript.echo "EncryptionPercentage: " & EncryptionPercentage & "%"
          wscript.echo "LockStatus:           " & arLockStatus(LockStatus) & " (" & LockStatus & ")"

          completeVolumeKeyProtectorFriendlyName = ""
          For Each objId in VolumeKeyProtectorID
             Dim VolumeKeyProtectorFriendlyName

             objItem1.GetKeyProtectorFriendlyName objId, VolumeKeyProtectorFriendlyName

             If VolumeKeyProtectorFriendlyName <> "" Then
                wscript.echo "   KeyProtectors:        " & VolumeKeyProtectorFriendlyName
                completeVolumeKeyProtectorFriendlyName = completeVolumeKeyProtectorFriendlyName & "/" & VolumeKeyProtectorFriendlyName
             End If
          Next      
             
       Next     

     

    Please find the code above, which i was able to run in vbscirpt, but when i tried to move this code in vb.net i was not able to get the result for EncryptionMethod, EncryptionPercentage etc.

    Please help.


    Vineet Verma

    Friday, March 22, 2013 8:08 PM

Answers

  • Hi Vineet,

    You could use WMI Code creator to create codes as you need:

    Imports System
    Imports System.Management
    Imports System.Windows.Forms
    
    Namespace WMISample
    
        Public Class MyWMIQuery
    
            Public Overloads Shared Function Main() As Integer
    
                Try
                    Dim searcher As New ManagementObjectSearcher( _
                        "root\cimv2\Security\MicrosoftVolumeEncryption", _
                        "SELECT * FROM Win32_EncryptableVolume") 
    
                    For Each queryObj As ManagementObject in searcher.Get()
    
                        Console.WriteLine("-----------------------------------")
                        Console.WriteLine("Win32_EncryptableVolume instance")
                        Console.WriteLine("-----------------------------------")
                        Console.WriteLine("ConversionStatus: {0}", queryObj("ConversionStatus"))
                        Console.WriteLine("DeviceID: {0}", queryObj("DeviceID"))
                        Console.WriteLine("DriveLetter: {0}", queryObj("DriveLetter"))
                        Console.WriteLine("EncryptionMethod: {0}", queryObj("EncryptionMethod"))
                        Console.WriteLine("IsVolumeInitializedForProtection: {0}", queryObj("IsVolumeInitializedForProtection"))
                        Console.WriteLine("PersistentVolumeID: {0}", queryObj("PersistentVolumeID"))
                        Console.WriteLine("ProtectionStatus: {0}", queryObj("ProtectionStatus"))
                        Console.WriteLine("VolumeType: {0}", queryObj("VolumeType"))
                    Next
                Catch err As ManagementException
                    MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
                End Try

    Hope this helps.

    Regards,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Marked as answer by vineet0707 Monday, March 25, 2013 1:27 PM
    • Edited by Youen Zen Tuesday, March 26, 2013 3:14 AM Grammar correction
    Monday, March 25, 2013 9:18 AM

All replies

  • 'Connects to the remote PLT
       Set objWMIService1 = GetObject( "winmgmts:" _
                                      & "{authenticationLevel=pktPrivacy}!\\" _
                                      & strPLT _
                                      & "\root\CIMV2\Security\MicrosoftVolumeEncryption")

       Set colItems1 = objWMIService1.ExecQuery("SELECT * FROM Win32_EncryptableVolume",,48)

       Dim arEncryptionMethod
       arEncryptionMethod = Array( "None", "AES 128 With Diffuser", "AES 256 With Diffuser", "AES 128", "AES 256" )

       Dim arProtectionStatus
       arProtectionStatus = Array( "Protection Off", "Protection On", "Protection Unknown" )

       Dim arConversionStatus
       arConversionStatus = Array( "Fully Decrypted", "Fully Encrypted", "Encryption In Progress", "Decryption In Progress", "Encryption Paused", "Decryption Paused" )

       Dim arLockStatus
       arLockStatus = Array( "Unlocked", "Locked" )
     
      'Gets the Operating system of the PLT
       OSReturn = GetOS(strPLT)

       'Gets the BDE Status
       For Each objItem1 in colItems1
          GetBDEStatus = "Success"   
          Dim EncryptionMethod
          Dim ProtectionStatus
          Dim ConversionStatus
          Dim EncryptionPercentage      
          Dim VolumeKeyProtectorID
          Dim LockStatus
          Dim completeVolumeKeyProtectorFriendlyName

          objItem1.GetEncryptionMethod EncryptionMethod
          objItem1.GetProtectionStatus ProtectionStatus
          objItem1.GetConversionStatus ConversionStatus, EncryptionPercentage
          objItem1.GetKeyProtectors 0,VolumeKeyProtectorID
          objItem1.GetLockStatus LockStatus

          'Writes in the Log file  
          wscript.echo "DeviceID:             " & objItem1.DeviceID
          wscript.echo "DriveLetter:          " & objItem1.DriveLetter
          wscript.echo "EncryptionMethod:     " & arEncryptionMethod(EncryptionMethod) & " (" & EncryptionMethod & ")"
          wscript.echo "ProtectionStatus:     " & arProtectionStatus(ProtectionStatus) & " (" & ProtectionStatus & ")"
          wscript.echo "ConversionStatus:     " & arConversionStatus(ConversionStatus) & " (" & ConversionStatus & ")"
          wscript.echo "EncryptionPercentage: " & EncryptionPercentage & "%"
          wscript.echo "LockStatus:           " & arLockStatus(LockStatus) & " (" & LockStatus & ")"

          completeVolumeKeyProtectorFriendlyName = ""
          For Each objId in VolumeKeyProtectorID
             Dim VolumeKeyProtectorFriendlyName

             objItem1.GetKeyProtectorFriendlyName objId, VolumeKeyProtectorFriendlyName

             If VolumeKeyProtectorFriendlyName <> "" Then
                wscript.echo "   KeyProtectors:        " & VolumeKeyProtectorFriendlyName
                completeVolumeKeyProtectorFriendlyName = completeVolumeKeyProtectorFriendlyName & "/" & VolumeKeyProtectorFriendlyName
             End If
          Next      
             
       Next     

    Please find the code above, which i was able to run in vbscirpt, but when i tried to move this code in vb.net i was not able to get the result for EncryptionMethod, EncryptionPercentage etc.

    Please help.


    Vineet Verma

    I am using ManagementobjectSearcher for wmi query as shown below

     Dim Scope As ManagementScope
            Dim query As ObjectQuery
            Dim searcher As ManagementObjectSearcher
            Dim queryCollection As ManagementObjectCollection
            Dim m As ManagementObject
            Dim myConnectionOptions As New System.Management.ConnectionOptions
            With myConnectionOptions
                '.Impersonation = System.Management.ImpersonationLevel.Impersonate
                '* Use next line for XP
                '.Authentication = System.Management.AuthenticationLevel.Packet
                '* Use next line for Win prior XP
                '*.Authentication = System.Management.AuthenticationLevel.Connect
                .Authentication = System.Management.AuthenticationLevel.PacketPrivacy
            End With
            Scope = New ManagementScope("\\" & SystemIP & "\root\CIMV2\Security\MicrosoftVolumeEncryption", myConnectionOptions)
            Scope.Connect()
            If Scope.IsConnected = True Then
                query = New ObjectQuery("Select * from Win32_EncryptableVolume")
                searcher = New ManagementObjectSearcher(Scope, query)
                queryCollection = searcher.Get()
                For Each m In queryCollection
                    MsgBox(m.ToString())
                    'MsgBox(m("DeviceID"))
                    'MsgBox(m("Encrypt").ToString())
                    'MsgBox(m("ProtectionStatus").ToString())
                    MsgBox(m.GetMethodParameters("GetConversionStatus"))
                    'MsgBox(m("ConversionStatus").ToString())
                Next
            End If


    Vineet Verma

    Friday, March 22, 2013 8:10 PM
  • Hi Vineet,

    You could use WMI Code creator to create codes as you need:

    Imports System
    Imports System.Management
    Imports System.Windows.Forms
    
    Namespace WMISample
    
        Public Class MyWMIQuery
    
            Public Overloads Shared Function Main() As Integer
    
                Try
                    Dim searcher As New ManagementObjectSearcher( _
                        "root\cimv2\Security\MicrosoftVolumeEncryption", _
                        "SELECT * FROM Win32_EncryptableVolume") 
    
                    For Each queryObj As ManagementObject in searcher.Get()
    
                        Console.WriteLine("-----------------------------------")
                        Console.WriteLine("Win32_EncryptableVolume instance")
                        Console.WriteLine("-----------------------------------")
                        Console.WriteLine("ConversionStatus: {0}", queryObj("ConversionStatus"))
                        Console.WriteLine("DeviceID: {0}", queryObj("DeviceID"))
                        Console.WriteLine("DriveLetter: {0}", queryObj("DriveLetter"))
                        Console.WriteLine("EncryptionMethod: {0}", queryObj("EncryptionMethod"))
                        Console.WriteLine("IsVolumeInitializedForProtection: {0}", queryObj("IsVolumeInitializedForProtection"))
                        Console.WriteLine("PersistentVolumeID: {0}", queryObj("PersistentVolumeID"))
                        Console.WriteLine("ProtectionStatus: {0}", queryObj("ProtectionStatus"))
                        Console.WriteLine("VolumeType: {0}", queryObj("VolumeType"))
                    Next
                Catch err As ManagementException
                    MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
                End Try

    Hope this helps.

    Regards,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Marked as answer by vineet0707 Monday, March 25, 2013 1:27 PM
    • Edited by Youen Zen Tuesday, March 26, 2013 3:14 AM Grammar correction
    Monday, March 25, 2013 9:18 AM
  • Shame on you Shanks. :) The writelines could have been much shorter if it had been a single statement.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Monday, March 25, 2013 10:40 AM
  • Hi Shanks,

    Thanks for the reply..

    I had to get the ecryption percentage from the GETCONVERSIONSTATUS method, so i used the

    invokemethod function to get the out of the getconversionstatus to be sotred inteh object array as shown below.

    Dim strObject(1) As Object

    m.InvokeMethod("GetConversionStatus", strObject)

    /////////CODE////////////

    Public Function getBDEStatus(ByVal SystemIP As String)
            Dim strProtectionStatus

    Dim arConversionStatus() As String = {"Fully Decrypted", "Fully Encrypted", "Encryption In Progress", "Decryption In Progress", "Encryption Paused", "Decryption Paused"}      

    Dim arProtectionStatus() As String = {"Protection Off", "Protection On", "Protection Unknown"}
            strProtectionStatus = ""
            Dim strObject(1) As Object
            Dim Scope As ManagementScope
            Dim query As ObjectQuery
            Dim searcher As ManagementObjectSearcher
            Dim queryCollection As ManagementObjectCollection
            Dim m As ManagementObject
            Dim myConnectionOptions As New System.Management.ConnectionOptions
            With myConnectionOptions
                '.Impersonation = System.Management.ImpersonationLevel.Impersonate
                '* Use next line for XP
                '.Authentication = System.Management.AuthenticationLevel.Packet
                '* Use next line for Win prior XP
                '*.Authentication = System.Management.AuthenticationLevel.Connect
                .Authentication = System.Management.AuthenticationLevel.PacketPrivacy
            End With
            Scope = New ManagementScope("\\" & SystemIP & "\root\CIMV2\Security\MicrosoftVolumeEncryption", myConnectionOptions)
            Try
                Scope.Connect()
                If Scope.IsConnected = True Then
                    query = New ObjectQuery("Select * from Win32_EncryptableVolume")
                    searcher = New ManagementObjectSearcher(Scope, query)
                    queryCollection = searcher.Get()
                    For Each m In queryCollection
                        strProtectionStatus = arProtectionStatus(m("ProtectionStatus"))
                        m.InvokeMethod("GetConversionStatus", strObject)
                    Next
                End If
                getBDEStatus = strProtectionStatus.ToString().Replace(vbCrLf, "") & "," & arConversionStatus(strObject(0)) & "," & strObject(1).ToString() & "%"
            Catch ex As Exception
                getBDEStatus = "Error quering Slate."
            End Try
            Return getBDEStatus
        End Function

    Thanks for your reply.

    Regards,

    Vineet


    Vineet Verma

    Monday, March 25, 2013 1:27 PM
  •         Bit = ""
            Dim DeviceID As String = ""
            Dim DriveLetter As String = ""
            Dim PersistentVolumeID As String = ""
            Dim ConversionStatus As String = ""
            Dim EncryptionMethod As String = ""
            Dim ProtectionStatus As String = ""
            Dim EncryptionPercentage As String = ""
            Dim VolumeKeyProtectorID As String = ""
            Dim VolumeKeyName As String = ""
            Dim arEncryptionMethod() As String = {"None", "AES 128 With Diffuser", "AES 256 With Diffuser", "AES 128", "AES 256"}
            Dim arProtectionStatus() As String = {"Protection Off", "Protection On", "Protection Unknown"}
            Dim arConversionStatus() As String = {"Fully Decrypted", "Fully Encrypted", "Encryption In Progress", "Decryption In Progress", "Encryption Paused", "Decryption Paused"}

            Try
                Dim strObject(1) As Object
                Dim searcher As New ManagementObjectSearcher("root\cimv2\Security\MicrosoftVolumeEncryption", "SELECT * FROM Win32_EncryptableVolume")

                For Each queryObj As ManagementObject In searcher.Get()

                    DeviceID = queryObj("DeviceID")
                    DriveLetter = queryObj("DriveLetter")
                    PersistentVolumeID = queryObj("PersistentVolumeID")
                    ProtectionStatus = arProtectionStatus(queryObj("ProtectionStatus"))

                    queryObj.InvokeMethod("GetConversionStatus", strObject)
                    ConversionStatus = arConversionStatus(strObject(0))
                    EncryptionPercentage = strObject(1).ToString() & "%"

                    queryObj.InvokeMethod("GetEncryptionMethod", strObject)
                    EncryptionMethod = arEncryptionMethod(strObject(0))

                    strObject(0) = 2    ' get Externalkey
                    queryObj.InvokeMethod("GetKeyProtectors", strObject)
                    Dim protectors() As String = strObject(1)
                    VolumeKeyProtectorID = Join(protectors, "|")

                    strObject(0) = VolumeKeyProtectorID
                    queryObj.InvokeMethod("GetExternalKeyFileName", strObject)
                    VolumeKeyName = strObject(1)

                    Bit = "DriveLetter: " & DriveLetter & vbCrLf & "ConversionStatus: " & ConversionStatus & vbCrLf & "EncryptionMethod: " & EncryptionMethod & vbCrLf & "ProtectionStatus: " & ProtectionStatus & vbCrLf & "EncryptionPercentage: " & EncryptionPercentage & vbCrLf & "KeyName: " & VolumeKeyName

                Next

            Catch ex As Exception
                Bit = ex.ToString()

            End Try

    Thursday, June 20, 2013 6:28 PM
  • Radimus68. Your code will be easier to read if you paste it into the Insert Code Block function to enter it into your post. That is the function to the right of the letters HTML at the top of an open post.

    For example

    Bit = ""
             Dim DeviceID As String = ""
             Dim DriveLetter As String = ""
             Dim PersistentVolumeID As String = ""
             Dim ConversionStatus As String = ""
             Dim EncryptionMethod As String = ""
             Dim ProtectionStatus As String = ""
             Dim EncryptionPercentage As String = ""
             Dim VolumeKeyProtectorID As String = ""
             Dim VolumeKeyName As String = ""
             Dim arEncryptionMethod() As String = {"None", "AES 128 With Diffuser", "AES 256 With Diffuser", "AES 128", "AES 256"}
             Dim arProtectionStatus() As String = {"Protection Off", "Protection On", "Protection Unknown"}
             Dim arConversionStatus() As String = {"Fully Decrypted", "Fully Encrypted", "Encryption In Progress", "Decryption In Progress", "Encryption Paused", "Decryption Paused"}
    
            Try
                 Dim strObject(1) As Object
                 Dim searcher As New ManagementObjectSearcher("root\cimv2\Security\MicrosoftVolumeEncryption", "SELECT * FROM Win32_EncryptableVolume")
    
                For Each queryObj As ManagementObject In searcher.Get()
    
                    DeviceID = queryObj("DeviceID")
                     DriveLetter = queryObj("DriveLetter")
                     PersistentVolumeID = queryObj("PersistentVolumeID")
                     ProtectionStatus = arProtectionStatus(queryObj("ProtectionStatus"))
    
                    queryObj.InvokeMethod("GetConversionStatus", strObject)
                     ConversionStatus = arConversionStatus(strObject(0))
                     EncryptionPercentage = strObject(1).ToString() & "%"
    
                    queryObj.InvokeMethod("GetEncryptionMethod", strObject)
                     EncryptionMethod = arEncryptionMethod(strObject(0))
    
                    strObject(0) = 2    ' get Externalkey
                     queryObj.InvokeMethod("GetKeyProtectors", strObject)
                     Dim protectors() As String = strObject(1)
                     VolumeKeyProtectorID = Join(protectors, "|")
    
                    strObject(0) = VolumeKeyProtectorID
                     queryObj.InvokeMethod("GetExternalKeyFileName", strObject)
                     VolumeKeyName = strObject(1)
    
                    Bit = "DriveLetter: " & DriveLetter & vbCrLf & "ConversionStatus: " & ConversionStatus & vbCrLf & "EncryptionMethod: " & EncryptionMethod & vbCrLf & "ProtectionStatus: " & ProtectionStatus & vbCrLf & "EncryptionPercentage: " & EncryptionPercentage & vbCrLf & "KeyName: " & VolumeKeyName
    
                Next
    
            Catch ex As Exception
                 Bit = ex.ToString()
    
            End Try
    


    You've taught me everything I know but not everything you know.

    Thursday, June 20, 2013 6:36 PM
  • Hey... this looks like my code (except for some echos and some "1"s) :). I have provided the exact same solution for VB at http://www.normanbauer.com/2013/11/29/how-to-get-some-information-on-bitlocker-using-visual-basic-and-wmi/
    Friday, November 29, 2013 8:16 AM