Solved

Use vbs to check WMI and write registry key

Posted on 2011-03-02
4
2,807 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you. That worked perfectly.
0
 

Author Closing Comment

by:Lorrec
Comment Utility
Thank you again.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now