I remembered that the WMI provides the StdRegProv class. You may try it at work.
JScript (that we may include into a hybrid script later on):
registry.js
Code: Select all
/*
http://msdn.microsoft.com/en-us/library/aa393664(v=vs.85).aspx
*/
/* Hives */
var HKCR = HKEY_CLASSES_ROOT = 0x80000000;
var HKCU = HKEY_CURRENT_USER = 0x80000001;
var HKLM = HKEY_LOCAL_MACHINE = 0x80000002;
var HKU = HKEY_USERS = 0x80000003;
var HKCC = HKEY_CURRENT_CONFIG = 0x80000005;
/* Data Types */
var REG_NONE = 0;
var REG_SZ = 1;
var REG_EXPAND_SZ = 2;
var REG_BINARY = 3;
var REG_DWORD = 4;
var REG_MULTI_SZ = 7;
var REG_QWORD = 11;
/* Access Rights (can be combined with the | operator) */
var KEY_QUERY_VALUE = 0x00000001; //Required to query the values of a registry key.
var KEY_SET_VALUE = 0x00000002; //Required to create, delete, or set a registry value.
var KEY_CREATE_SUB_KEY = 0x00000004; //Required to create a subkey of a registry key.
var KEY_ENUMERATE_SUB_KEYS = 0x00000008; //Required to enumerate the subkeys of a registry key.
var KEY_NOTIFY = 0x00000010; //Required to request change notifications for a registry key or for subkeys of a registry key.
var KEY_CREATE = 0x00000020; //Required to create a registry key.
var DELETE = 0x00010000; //Required to delete a registry key.
var READ_CONTROL = 0x00020000; //Combines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY values.
var WRITE_DAC = 0x00040000; //Required to modify the DACL in the object's security descriptor.
var WRITE_OWNER = 0x00080000; //Required to change the owner in the object's security descriptor.
function regCheckAccess(strComputer, uHive, strRegPath, uRequiredAccess) {
try {
var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var objService = objLocator.ConnectServer(strComputer, "root\\default");
objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
var objReg = objService.Get("StdRegProv");
var objAccessMethod = objReg.Methods_.Item("CheckAccess");
var objAccessInParam = objAccessMethod.InParameters.SpawnInstance_();
objAccessInParam.hDefKey = uHive;
objAccessInParam.sSubKeyName = strRegPath;
objAccessInParam.uRequired = uRequiredAccess;
var objAccessOutParam = objReg.ExecMethod_(objAccessMethod.Name, objAccessInParam);
if (objAccessOutParam.ReturnValue == 0) {
if (objAccessOutParam.bGranted) {return true;}
}
return false;
}
catch(e) {return false;}
}
function regCreateKey(strComputer, uHive, strRegPath) {
try {
var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var objService = objLocator.ConnectServer(strComputer, "root\\default");
objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
var objReg = objService.Get("StdRegProv");
var objCreateMethod = objReg.Methods_.Item("CreateKey");
var objCreateInParam = objCreateMethod.InParameters.SpawnInstance_();
objCreateInParam.hDefKey = uHive;
objCreateInParam.sSubKeyName = strRegPath;
var objCreateOutParam = objReg.ExecMethod_(objCreateMethod.Name, objCreateInParam);
if (objCreateOutParam.ReturnValue == 0) {return true;}
return false;
}
catch(e) {return false;}
}
function regWriteVal(strComputer, uHive, strRegPath, strValName, iType, vData) {
try {
var bRet = false;
var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var objService = objLocator.ConnectServer(strComputer, "root\\default");
objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
var objReg = objService.Get("StdRegProv");
var objCreateMethod = objReg.Methods_.Item("CreateKey");
var objCreateInParam = objCreateMethod.InParameters.SpawnInstance_();
objCreateInParam.hDefKey = uHive;
objCreateInParam.sSubKeyName = strRegPath;
var objCreateOutParam = objReg.ExecMethod_(objCreateMethod.Name, objCreateInParam);
if (objCreateOutParam.ReturnValue != 0) {return false;}
switch (iType) {
case REG_SZ:
var objSetMethod = objReg.Methods_.Item("SetStringValue");
var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
objSetInParam.hDefKey = uHive;
objSetInParam.sSubKeyName = strRegPath;
objSetInParam.sValueName = strValName;
objSetInParam.sValue = vData;
var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
if (objSetOutParam.ReturnValue == 0) {bRet = true;}
break;
case REG_EXPAND_SZ:
var objSetMethod = objReg.Methods_.Item("SetExpandedStringValue");
var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
objSetInParam.hDefKey = uHive;
objSetInParam.sSubKeyName = strRegPath;
objSetInParam.sValueName = strValName;
objSetInParam.sValue = vData;
var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
if (objSetOutParam.ReturnValue == 0) {bRet = true;}
break;
case REG_BINARY:
var objSetMethod = objReg.Methods_.Item("SetBinaryValue");
var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
objSetInParam.hDefKey = uHive;
objSetInParam.sSubKeyName = strRegPath;
objSetInParam.sValueName = strValName;
objSetInParam.uValue = vData;
var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
if (objSetOutParam.ReturnValue == 0) {bRet = true;}
break;
case REG_DWORD:
var objSetMethod = objReg.Methods_.Item("SetDWORDValue");
var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
objSetInParam.hDefKey = uHive;
objSetInParam.sSubKeyName = strRegPath;
objSetInParam.sValueName = strValName;
objSetInParam.uValue = vData;
var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
if (objSetOutParam.ReturnValue == 0) {bRet = true;}
break;
case REG_MULTI_SZ:
var objSetMethod = objReg.Methods_.Item("SetMultiStringValue");
var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
objSetInParam.hDefKey = uHive;
objSetInParam.sSubKeyName = strRegPath;
objSetInParam.sValueName = strValName;
objSetInParam.sValue = vData;
var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
if (objSetOutParam.ReturnValue == 0) {bRet = true;}
break;
case REG_QWORD:
var objSetMethod = objReg.Methods_.Item("SetQWORDValue");
var objSetInParam = objSetMethod.InParameters.SpawnInstance_();
objSetInParam.hDefKey = uHive;
objSetInParam.sSubKeyName = strRegPath;
objSetInParam.sValueName = strValName;
objSetInParam.uValue = vData;
var objSetOutParam = objReg.ExecMethod_(objSetMethod.Name, objSetInParam);
if (objSetOutParam.ReturnValue == 0) {bRet = true;}
break;
default:
return false;
}
return bRet;
}
catch(e) {return false;}
}
function regReadVal(strComputer, uHive, strRegPath, strValName) {
try {
var vRet = null, iType = -1;
var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var objService = objLocator.ConnectServer(strComputer, "root\\default");
objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
var objReg = objService.Get("StdRegProv");
var objEnumMethod = objReg.Methods_.Item("EnumValues");
var objEnumInParam = objEnumMethod.InParameters.SpawnInstance_();
objEnumInParam.hDefKey = uHive;
objEnumInParam.sSubKeyName = strRegPath;
var objEnumOutParam = objReg.ExecMethod_(objEnumMethod.Name, objEnumInParam);
if (objEnumOutParam.ReturnValue == 0) {
if (objEnumOutParam.sNames != null) {
for (var i = 0; i <= objEnumOutParam.sNames.ubound(); i++) {
if (objEnumOutParam.sNames.getItem(i).toLowerCase() == strValName.toLowerCase()) {
iType = objEnumOutParam.Types.getItem(i);
break;
}
}
}
else {if (strValName == "") {iType = REG_NONE;}}
if (iType == -1) {return null;}
}
else {return null;}
switch (iType) {
case REG_SZ:
var objGetMethod = objReg.Methods_.Item("GetStringValue");
var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.sValue;}
break;
case REG_EXPAND_SZ:
var objGetMethod = objReg.Methods_.Item("GetExpandedStringValue");
var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.sValue;}
break;
case REG_BINARY:
var objGetMethod = objReg.Methods_.Item("GetBinaryValue");
var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue.toArray();}}
break;
case REG_DWORD:
var objGetMethod = objReg.Methods_.Item("GetDWORDValue");
var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.uValue;}
break;
case REG_MULTI_SZ:
var objGetMethod = objReg.Methods_.Item("GetMultiStringValue");
var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue.toArray();}}
break;
case REG_QWORD:
var objGetMethod = objReg.Methods_.Item("GetQWORDValue");
var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {vRet = objGetOutParam.uValue;}
break;
case REG_NONE:
var objGetMethod = objReg.Methods_.Item("GetStringValue");
var objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
var objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue; break;}}
objGetMethod = objReg.Methods_.Item("GetExpandedStringValue");
objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue; break;}}
objGetMethod = objReg.Methods_.Item("GetBinaryValue");
objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue.toArray(); break;}}
objGetMethod = objReg.Methods_.Item("GetDWORDValue");
objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue; break;}}
objGetMethod = objReg.Methods_.Item("GetMultiStringValue");
objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.sValue != null) {vRet = objGetOutParam.sValue.toArray(); break;}}
objGetMethod = objReg.Methods_.Item("GetQWORDValue");
objGetInParam = objGetMethod.InParameters.SpawnInstance_();
objGetInParam.hDefKey = uHive;
objGetInParam.sSubKeyName = strRegPath;
objGetInParam.sValueName = strValName;
objGetOutParam = objReg.ExecMethod_(objGetMethod.Name, objGetInParam);
if (objGetOutParam.ReturnValue == 0) {if (objGetOutParam.uValue != null) {vRet = objGetOutParam.uValue; break;}}
default:
return null;
}
return vRet;
}
catch(e) {return null;}
}
function regDelKey(strComputer, uHive, strRegPath) {
try {
var iRet = 0;
var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var objService = objLocator.ConnectServer(strComputer, "root\\default");
objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
var objReg = objService.Get("StdRegProv");
var objEnumMethod = objReg.Methods_.Item("EnumKey");
var objEnumInParam = objEnumMethod.InParameters.SpawnInstance_();
objEnumInParam.hDefKey = uHive;
objEnumInParam.sSubKeyName = strRegPath;
var objEnumOutParam = objReg.ExecMethod_(objEnumMethod.Name, objEnumInParam);
if (objEnumOutParam.ReturnValue == 0) {
if (objEnumOutParam.sNames != null) {
for (var i = 0; i <= objEnumOutParam.sNames.ubound(); i++) {
var strNewPath = strRegPath + "\\" + objEnumOutParam.sNames.getItem(i);
iRet += regDelKey(strComputer, uHive, strNewPath);
}
}
var objDelMethod = objReg.Methods_.Item("DeleteKey");
var objDelInParam = objDelMethod.InParameters.SpawnInstance_();
objDelInParam.hDefKey = uHive;
objDelInParam.sSubKeyName = strRegPath;
var objDelOutParam = objReg.ExecMethod_(objDelMethod.Name, objDelInParam);
iRet = (objDelOutParam.ReturnValue == 0) ? ++iRet : -1;
}
return iRet;
}
catch(e) {return -1;}
}
function regDelVal(strComputer, uHive, strRegPath, strValName) {
try {
var objLocator = new ActiveXObject("WbemScripting.SWbemLocator");
var objService = objLocator.ConnectServer(strComputer, "root\\default");
objService.Security_.ImpersonationLevel = 3; //wbemImpersonationLevelImpersonate
var objReg = objService.Get("StdRegProv");
var objDelMethod = objReg.Methods_.Item("DeleteValue");
var objDelInParam = objDelMethod.InParameters.SpawnInstance_();
objDelInParam.hDefKey = uHive;
objDelInParam.sSubKeyName = strRegPath;
objDelInParam.sValueName = strValName;
var objDelOutParam = objReg.ExecMethod_(objDelMethod.Name, objDelInParam);
if (objDelOutParam.ReturnValue == 0) {return true;}
return false;
}
catch(e) {return false;}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var sCompName = ".";
if (regCheckAccess(sCompName, HKCU, "Software\\Microsoft\\Command Processor", KEY_QUERY_VALUE)) {
WScript.Echo("Extensions (User)\n" + regReadVal(sCompName, HKCU, "Software\\Microsoft\\Command Processor", "EnableExtensions"));
} else {
WScript.Echo("HKCU Access Denied");
}
if (regCheckAccess(sCompName, HKLM, "Software\\Microsoft\\Command Processor", KEY_QUERY_VALUE)) {
WScript.Echo("Extensions (System)\n" + regReadVal(sCompName, HKLM, "Software\\Microsoft\\Command Processor", "EnableExtensions"));
} else {
WScript.Echo("HKLM Access Denied");
}
Regards
aGerman