Unlock bitlocker drive with VBScript - methods

I'm trying to script out the unlocking of a bitlocker drive using a DRA certificate. I'm attempting to use the WMI Method UnlockWithCertificateFile and I can't for the life of me figure out what i'm doing wrong or even find an example.
I know the certificate and pin work because i can manually unlock the drive using manage-bde -unlock....
when i run my script i get a return value of -2146885623 wich i've looked up to be -2146885623, "Cannot find the requested object."
i'm not sure what object its talking about.
here is the code i'm using (minus the pin)
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\Security\MicrosoftVolumeEncryption")
' Obtain an instance of the the class
' using a key property value.
Set objShare = objWMIService.Get("Win32_EncryptableVolume.DeviceID='\\?\Volume{a2965903-4af0-11e2-be65-806e6f6e6963}\'")
' Obtain an InParameters object specific
' to the method.
Set objInParam = objShare.Methods_("UnlockWithCertificateFile"). _
inParameters.SpawnInstance_()
' Add the input parameters.
objInParam.Properties_.Item("PathWithFileName") = "D:\BitLocker.pfx"
objInParam.Properties_.Item("Pin") = "PinCode
' Execute the method and obtain the return status.
' The OutParameters object in objOutParams
' is created by the provider.
Set objOutParams = objWMIService.ExecMethod("Win32_EncryptableVolume.DeviceID='\\?\Volume{a2965903-4af0-11e2-be65-806e6f6e6963}\'", "UnlockWithCertificateFile", objInParam)
' List OutParams
Wscript.Echo "Out Parameters: "
Wscript.echo "ReturnValue: " & objOutParams.ReturnValue
has anyone ever used this method or can anyone see something i may be doing wrong? also i'm using the windows 8 commandline recovery option, i'm not sure if that makes a difference because i have tested calling other methods and wmi and scripting seem to be fully implemented.

Related

How to get the full computer name using Test Complete

How to get the full computer name using Test Complete
For example,
Computer Name : RAMAKRISHNA
Full Computer Name : RAMAKRISHNA.XYZ.COM
Domain Name : XYZ.COM
Using TestComplete, I have tried the following:
log.Message sys.HostName 'Gives "RAMAKRISHNA"
log.Message sys.DomainName 'Gives "XYZ"
Here, I am missing to get ".COM" using TestComplete
Please help me to get the full computer like RAMAKRISHNA.XYZ.COM
You can do this using WMI.
Sub Test
Log.Message getFullPCName
End Sub
Function getFullPCName
Set cSystem = WMI.Service.InstancesOf("Win32_ComputerSystem").ItemIndex(0)
getFullPCName = cSystem.Name & "." & cSystem.Domain
End Function
Windows Management Instrumentation technology provides ways to manage Windows settings and operations. You can use the Win32_ComputerSystem WMI class to get information on the system. TestComplete provides an easy way to work with WMI using the corresponding object. Find more information in the WMI Object help topic.
Try this if it helps:
Set wshShell = CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
WScript.Echo "Computer Name: " & strComputerName & "." & GetDomainName
Function GetDomainName()
Dim Info
Set Info = CreateObject("AdSystemInfo")
GetDomainName = Info.DomainDNSName
End Function

VBScript WMI PnPEntity obtaining HardwareID

I'm looking to extract the hardwareID from each device installed on a system using VBScript.
I can extract most properties from the PnPEntity class however the HardwareId or CompatibleId does seem to cause trouble - I'm presuming because it potentially returns an array.
My script is as follows:
Set TxtDriverOutput = objFSO.CreateTextFile("C:\Program Files\xxx\drivers.log", 8, True)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
txtDriverOutput.WriteLine Now() & Chr(32) & "Begin HardwareID WMI Query"
txtDriverOutput.WriteLine "----------------------------------------------------------------------------"
txtDriverOutput.WriteLine ""
Set colsHardwareID = objWmiService.ExecQuery("Select * from Win32_PnPEntity")
For Each objItem In colshardwareID
For Each StrHardwareID In objItem.HardwareID
txtdriveroutput.WriteLine StrHardwareID
Next
Next
When I run the script, I see one hardware ID populated into the text file - and then I get Error: Object is not a collection flagged on the line of my second for loop (sometimes I have seen it flag the line after it errors for some reason so maybe take that with a pinch of salt.
I have tried using ObjItem.HarwareID.count, UBound(ObjItem.HardwareID) in case there is a PnP Device that doesn't have a hardware ID (not sure if possible). Can anyone point me in the right direction? Thanks!
I managed to get to the bottom of this in the end.
I had to use the following:
For Each objItem In colshardwareID
If Not IsNull(objItem.HardwareID) Then
For Each StrHardwareID In objItem.HardwareID
ReDim Preserve ArrHardwareID(i)
ArrHardwareID(i) = strHardwareID
i = i + 1
Next
End If
Next
As expected - the collection was empty. A .count or .isempty was not working, it had to be IsNull.

VBScript get SNMP OID values

I'm trying to use access SNMP via VBScript to access a few OID values as it's a climate monitor which has figures for temperature, humidity and airflow. I know which OID's I need to use but cannot find any script or piece of code which effectively connects and pulls out out this information.
Has anyone got anything that fits the bill?
I tried using this code but keep getting an error: ActiveX component can't create object: 'Scripting.SNMPManager'
Set oSNMPManager = CreateObject("Scripting.SNMPManager")
oSNMPManager.Agent = "unitip"
oSNMPManager.Community = "public"
'Add Variable objects to Variables collection
Call oSNMPManager.Variables.Add( "1.3.6.1.4.1.17373.2.2.1.6.1" )
Result = oSNMPManager.Get( ErrorIndex )
WScript.Echo "Get result: " & Result
If Result = 10 Then
WScript.Echo "ErrorIndex: " & ErrorIndex
End If
'Display properties of all Variable objects
WScript.Echo "OID, Type, Value"
For Each SNMPVariable in oSNMPManager.Variables
WScript.Echo SNMPVariable.OID & ", " & SNMPVariable.Type & ", " & SNMPVariable.Value
next
'Remove all Variable objects from the Variables collection
oSNMPManager.Variables.RemoveAll
Set objSnmpManager = CreateObject( "AxNetwork.SnmpManager" )
Set objConstants = CreateObject( "AxNetwork.NwConstants" )
' A license key is required to unlock this component after the trial period has expired.
' Call 'Activate' with a valid license key as its first parameter. Second parameter determines whether to save the license key permanently
' to the registry (True, so you need to call Activate only once), or not to store the key permanently (False, so you need to call Activate
' every time the component is created). For details, see manual, chapter "Product Activation".
'
' objSnmpManager.Activate "XXXXX-XXXXX-XXXXX", False

Two different names for Windows services (VB6)

I am having a minor problem automating the starting and stopping of services. When I open services.msc and look at the list of services, they all have names. However, when I run this code:
Dim objService As Object
Dim objSet As Object
IsServiceRunning = False
Set objSet = GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_Service")
For Each objService In objSet
If (UCase(strServiceName) = UCase(objService.Name)) And (UCase(objService.State) = UCase("Running")) Then
IsServiceRunning = True
End If
Next
The objService.Name value is not the same as the name in the list. For example, "Computer Browser" is just "browser", "Distributed File System" is "dfs", and "Net Logon" is "netlogon". Is there a way to pull the full, longer name for these services from this objService object? I can workaround this, but for the sake of clarity in the code I'd rather use the same value for determining if the service is running, making a NET START or NET STOP command line call, and logging.
Just use objService.Caption to access "long name" of service.
I discovered the name of the property like this:
For Each objService In objSet
For Each vElem In objService.Properties_
Debug.Print vElem.Name; "=";
Debug.Print vElem.Value
Next
Exit For
...
Next
Just put objService in watch window to find out Properties_ property. Put vElem in watch window too to find Name and the default property Value (besides IsArray, etc.) of SWbemProperty object.

It keeps saying ActiveX component can't create object: 'Shell.LocalMachine'

When I run the code, I get an error saying
ActiveX component can't create object: 'Shell.LocalMachine'
Class MachineName
Private internal_ComputerName
Public Function SetMachineName
Set objComputer = CreateObject("Shell.LocalMachine")
internal_ComputerName = objComputer.MachineName
End Function
Public Property Get GetMachineName
GetMachineName = internal_ComputerName
End Property
End Class
Dim objMachine
Set objMachine = New MachineName
objMachine.SetMachineName
thanks for this. I am having the same problems when using this Shell.Localmachine on my windows 7 64 bit machine when i try to run a simple vbscript code. I had to default to WScript.Network instead:
'just a test script
'set objComputer = CreateObject("Shell.LocalMachine")
'wscript.echo "computer name" & objcomputer.machinename
Set objWshNet = CreateObject("WScript.Network")
wscript.echo "computer name : " & objwshnet.computername
Morbo said "Must admit I hadn't come across that object before. I'd normally create a "WScript.Network" object and get the ComputerName property. If you're diagnosing "Shell.LocalMachine" I can tell you that on my copy of XP it is provided by system32\shgina.dll"

Resources