' ---------------------------------------------------------------------------------------------------------------- ' REGISTRY access using WMI Wbemscripting ' ---------------------------------------------------------------------------------------------------------------- newscript report() report ' ---------------------------------------------------------- ' there are multiple examples, numbered 1..6, select the one you want to run dim SelectExample = 5 ' ---------------------------------------------------------- ' ---------------------------------------------------------------------------------------------------------------- ' Useful functions for displaying the input & output ' ---------------------------------------------------------------------------------------------------------------- FUNCTION DisplayTrigger ( inParams , useMethod ) dim tmp = inparams.properties_.item("Hdefkey").Value report("Trigger:") freestring = "Request: " IF ( tmp == HKCR ) then freestring = freestring & "HKEY_CLASSES_ROOT" ELSEIF ( tmp == HKCU ) then freestring = freestring & "HKEY_CURRENT_USER" ELSEIF ( tmp == HKLM ) then freestring = freestring & "HKEY_LOCAL_MACHINE" ELSEIF ( tmp == HKU ) then freestring = freestring & "HKEY_USERS" ELSEIF ( tmp == HKCC ) then freestring = freestring & "HKEY_CURRENT_CONFIG" ELSE freestring = freestring & "???? -" & hex(tmp) & "-" END if freestring = freestring & "\\" & inparams.properties_.item("sSubKeyName").Value IF ( useMethod ="GetStringValue" ) then freestring = freestring & "\\" & inparams.properties_.item("Svaluename").Value END If report = freestring END Function ' ---------------------------------------------------------------------------------------------------------------- FUNCTION DisplayResponse ( outParams , useMethod ) dim lvRow, index sob (soblv, "empty") report("Response:") IF ( Outparams.returnValue ) then report = " Failure: Return code = " & Outparams.returnValue STOP END IF ' the entire (I think) response is visible using: report = Outparams.GetObjectText_ ' warning need whitespace after the underscore, is NOT a line continuation underscore IF ( useMethod == "EnumValues" ) then dim nameArray = Outparams . snames dim typeArray = Outparams . types dim tkarr = split ( "REG_SZ,REG_EXPAND_SZ,REG_BINARY,REG_DWORD,REG_DWORD_BIG_ENDIAN,REG_LINK,REG_MULTI_SZ,REG_RESOURCE_LIST,REG_FULL_RESOURCE_DESCRIPTOR,REG_RESOURCE_REQUIREMENTS_LIST,REG_QWORD",",") IF instr ( istype(nameArray ), "arr") then sob (soblv, "add" , "column", "Name") sob (soblv, "add" , "column", "Type") lvRow = 0 FOR index = lbound ( nameArray ) to ubound ( nameArray ) sob (soblv, "add" , "row", nameArray (index)) lvRow = lvRow + 1 sob (soblv, "set" , "cell", lvRow, 2, tkarr ( typeArray (index) -1 )) NEXT index END if SOB ( soblv, "SET" , "COLUMN" , "WIDTH" , -2 ) ' set colums widths to show all text ELSEIF ( useMethod == "EnumKey" ) then dim nameArray = Outparams . snames IF instr ( istype(nameArray ), "arr") then sob (soblv, "add" , "column", "Name") lvRow = 0 FOR index = lbound ( nameArray ) to ubound ( nameArray ) sob (soblv, "add" , "row", nameArray (index)) lvRow = lvRow + 1 NEXT index END if SOB ( soblv, "SET" , "COLUMN" , "WIDTH" , -2 ) ' set colums widths to show all text ELSEIF ( useMethod == "GetStringValue" ) then report = Outparams.sValue END if END Function ' ---------------------------------------------------------------------------------------------------------------- ' Useful definitions ' ---------------------------------------------------------------------------------------------------------------- Constant strComputer = "." ' means this computer ' ---------------------------------------------------------- ' hives Constant HKCR = 0xffffffff80000000 ' = HKEY_CLASSES_ROOT = -2147483648 in a 64 bit signed int Constant HKCU = 0xffffffff80000001 ' = HKEY_CURRENT_USER = -2147483647 Constant HKLM = 0xffffffff80000002 ' = HKEY_LOCAL_MACHINE = -2147483646 Constant HKU = 0xffffffff80000003 ' = HKEY_USERS = -2147483645 Constant HKCC = 0xffffffff80000005 ' = HKEY_CURRENT_CONFIG = -2147483643 ' ---------------------------------------------------------------------------------------------------------------- ' Now start the work: ' ---------------------------------------------------------------------------------------------------------------- ' basically we use the OBJECT Wbemscripting.SWbemLocator dim objLocator = CreateObject("Wbemscripting.SWbemLocator") ' ---------------------------------------------------------- ' In some cases a method will require a specially prepared data structure ' this is handled using WbemScripting.SWbemNamedValueSet dim objCtx = CreateObject("WbemScripting.SWbemNamedValueSet") objCtx.Add ( "__ProviderArchitecture", 64 ) ' 32 or 64 bit ' ---------------------------------------------------------- dim objServices = objLocator.ConnectServer(strComputer,"root\cimv2","","",,,,objCtx) dim objStdRegProv = objServices.Get("StdRegProv") ' ---------------------------------------------------------- ' now we need to create the REGISTRY access parameters ' do this in the Inparams ' Hdefkey ' identifies the HIVE ' sSubKeyName ' svaluename ' not all requests need this, in fact not all request SUPPORT this parameter ' ---------------------------------------------------------- dim useMethod IF ( SelectExample = 1 ) then ' on my PC there are less than 20 keys at the top levl of HKEY_CURRENT_USER ' this example will list the top level useMethod ="EnumKey" dim Inparams = objStdRegProv.Methods_.item( useMethod ).Inparameters inparams.properties_.item("Hdefkey").Value = HKCU inparams.properties_.item("sSubKeyName").Value = "" ELSEIF ( SelectExample = 2 ) then ' one of those top level keys is "Printers" ' this example will list the subkeys of "Printers" useMethod ="EnumKey" dim Inparams = objStdRegProv.Methods_.item( useMethod ).Inparameters inparams.properties_.item("Hdefkey").Value = HKCU inparams.properties_.item("sSubKeyName").Value = "Printers" ELSEIF ( SelectExample = 3 ) then ' under "Printers" one of the subkeys is "Settings" ' this example will list the subkeys of "Printers\Settings" ' On my PC there are no subkeys, only values, this example works (no error) useMethod ="EnumKey" dim Inparams = objStdRegProv.Methods_.item( useMethod ).Inparameters inparams.properties_.item("Hdefkey").Value = HKCU inparams.properties_.item("sSubKeyName").Value = "Printers\Settings" ELSEIF ( SelectExample = 4 ) then ' under "Printers\Settings" there are no subkeys, only values ' this example will list the Values in "Printers\Settings" useMethod ="EnumValues" dim Inparams = objStdRegProv.Methods_.item( useMethod ).Inparameters inparams.properties_.item("Hdefkey").Value = HKCU inparams.properties_.item("sSubKeyName").Value = "Printers\Settings" ELSEIF ( SelectExample = 5 ) then ' under "Printers\Settings" there are no subkeys, only values ' on my PC only 1 value whose name is "EPSON ET-4850 Series" ' this example will get the VALUE associated with a specific name ' the value is of type REG_BINARY useMethod ="GetBinaryValue" dim Inparams = objStdRegProv.Methods_.item( useMethod ).Inparameters inparams.properties_.item("Hdefkey").Value = HKCU inparams.properties_.item("sSubKeyName").Value = "Printers\Settings" inparams.properties_.item("Svaluename").Value = "EPSON ET-4850 Series" ELSEIF ( SelectExample = 6 ) then ' this example will get the VALUE associated with a specific name ' the value is of type REG_EXPAND_SZ (string) useMethod ="GetStringValue" dim Inparams = objStdRegProv.Methods_.item( useMethod ).Inparameters inparams.properties_.item("Hdefkey").Value = HKCU inparams.properties_.item("sSubKeyName").Value = "Environment" inparams.properties_.item("Svaluename").Value = "OneDrive" ELSE message ="No example selected" stop END IF ' ---------------------------------------------------------- ' submit the request dim Outparams = objStdRegProv.ExecMethod_(useMethod ,Inparams,,objCtx) ' ---------------------------------------------------------- DisplayTrigger ( inparams , useMethod ) DisplayResponse ( outParams , useMethod ) nop ' script finished