Solved

Use vbs to check WMI and write registry key

Posted on 2011-03-02
4
2,843 Views
Last Modified: 2012-06-27
I created a script to check if BitLocker was enabled on a machine. The script works well if BitLocker is enabled. The registry key is created. The problem is that no registry key is created if BitLocker is not enabled.

I thought that the below would take care of any machine that does not have BitLocker enabled. Instead, no error appears but the registry key with a value of false is not created.

 Else
      Call BitLocker_Off

If I query WMI directly using wbemtest on a machine without BitLocker enabled, Select * from Win32_EncryptableVolume, it does not pull up any information.

Can anyone point me in the right direction?
strComputer = "."

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")

Set BitLocker = SWBemlocator.ConnectServer(strComputer,"root\CIMV2\security\microsoftvolumeencryption")
Set BitLockerItems = BitLocker.ExecQuery("Select * from Win32_EncryptableVolume",,48)
	For Each BitLockerItem in BitLockerItems
		If BitLockerItem.DriveLetter = "C:" And BitLockerItem.ProtectionStatus = 1  Then		
			Call BitLocker_On
		Else
			Call BitLocker_Off
		End If
	Next
	
		
Sub BitLocker_On

		strComputer = "."
		Const HKEY_LOCAL_MACHINE = &H80000002
		
		Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_ 
		strComputer & "\root\default:StdRegProv")
		
		strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
		oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
		
		strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
		strApp = "BitLocker Enabled"
		strAppValue = "True"
		
		oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strApp,strAppValue
		
		strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
		strApp1 = "Script Last Run"
		strAppValue1 = Date & " - " & Time
		
		oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strApp1,strAppValue1
	
End Sub

Sub BitLocker_Off

		strComputer = "."
		Const HKEY_LOCAL_MACHINE = &H80000002		

		Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_ 
		strComputer & "\root\default:StdRegProv")
		
		strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
		oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
		
		strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
		strApp = "BitLocker Enabled"
		strAppValue = "False"
		
		oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strApp,strAppValue
		
		strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
		strApp1 = "Script Last Run"
		strAppValue1 = Date & " - " & Time
		
		oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strApp1,strAppValue1
	
End Sub

Open in new window

0
Comment
Question by:Lorrec
  • 3
4 Comments
 

Author Comment

by:Lorrec
ID: 35018922
I believe my problem is that the wmi query (Select * from Win32_EncryptableVolume) does not produce any results since the drive is not encrypted.

Is it possible to call BitLocker_Off if the query does produce any results?
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 35020197
Hi,

Please try the following

strComputer = "."
result_status=False

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")

Set BitLocker = SWBemlocator.ConnectServer(strComputer,"root\CIMV2\security\microsoftvolumeencryption")
Set BitLockerItems = BitLocker.ExecQuery("Select * from Win32_EncryptableVolume",,48)
      For Each BitLockerItem in BitLockerItems
            result_status=True            
            If BitLockerItem.DriveLetter = "C:" And BitLockerItem.ProtectionStatus = 1  Then            
                  Call BitLocker_On
            Else
                  Call BitLocker_Off
            End If
      Next
      
      If result_status=false Then
            Call BitLocker_Off
      End if
            
Sub BitLocker_On

            strComputer = "."
            Const HKEY_LOCAL_MACHINE = &H80000002
            
            Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
            strComputer & "\root\default:StdRegProv")
            
            strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
            oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
            
            strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
            strApp = "BitLocker Enabled"
            strAppValue = "True"
            
            oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strApp,strAppValue
            
            strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
            strApp1 = "Script Last Run"
            strAppValue1 = Date & " - " & Time
            
            oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strApp1,strAppValue1
      
End Sub

Sub BitLocker_Off

            strComputer = "."
            Const HKEY_LOCAL_MACHINE = &H80000002            

            Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
            strComputer & "\root\default:StdRegProv")
            
            strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
            oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
            
            strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
            strApp = "BitLocker Enabled"
            strAppValue = "False"
            
            oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strApp,strAppValue
            
            strKeyPath = "SOFTWARE\Clariant\CCP\BitLocker"
            strApp1 = "Script Last Run"
            strAppValue1 = Date & " - " & Time
            
            oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strApp1,strAppValue1
      
End Sub
0
 

Author Comment

by:Lorrec
ID: 35021181
Thank you. That worked perfectly.
0
 

Author Closing Comment

by:Lorrec
ID: 35021186
Thank you again.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question