Created
March 30, 2017 19:21
-
-
Save gjyoung1974/ba4907e739bb46610136d7b973780e71 to your computer and use it in GitHub Desktop.
test a registry key for a given value via javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Gordon Young 2017 gjyoung1974@gmail.com | |
// This script : | |
// 1. Reads the given MSFT Windows registry subkey value reg_multi_sz data type | |
// 2. Compares it to the provided expected setting | |
// if the configured setting is compliant the script returns 'pass', | |
// if the setting is non-compliant the script returns 'fail' | |
//The registry path we want to test | |
var hklm = 0x80000002, KeyName = "System\\CurrentControlSet\\Services\\LanManServer\\Parameters", ValueName = "NullSessionPipes"; | |
//set our Array of expected CI Settings values: | |
var sExpectedValue = new Array(); //expected to be an empty string array | |
// The following function wmiExec() is is just a generic way to call "C Style" provider methods that require output parameters. This helps to return output from the GetMultiStringValue | |
// For example the windows regisry method we are calling look like: https://msdn.microsoft.com/en-us/library/aa390458(v=vs.85).aspx | |
/* | |
uint32 GetMultiStringValue( | |
[in] uint32 hDefKey = HKEY_LOCAL_MACHINE, | |
[in] string sSubKeyName, | |
[in] string sValueName, | |
[out] string sValue[] << we need a way to get the output value | |
); | |
*/ | |
function wmiExec(wmiService, providerName, methodName, methodParameters) { | |
var wmiProviderObject = wmiService.Get(providerName); | |
var wmiProviderMethod = wmiProviderObject.Methods_.Item(methodName); | |
var wmiInputParameterObject = wmiProviderMethod.Inparameters.SpawnInstance_(); | |
for (var parameter in methodParameters) { | |
wmiInputParameterObject.Properties_.item(parameter) = methodParameters[parameter]; | |
} | |
wmiOutParameterObject = wmiProviderObject.ExecMethod_(wmiProviderMethod.Name, wmiInputParameterObject); | |
return wmiOutParameterObject; | |
} | |
var wmiLocatorObj = WScript.CreateObject("WbemScripting.SWbemLocator"); //instanciate WMI provider | |
var wmiServiceDefault = wmiLocatorObj.ConnectServer(".", "root\\default"); | |
//set our method properties | |
var methodParameters = new Object(); | |
methodParameters["hDefKey"] = hklm; | |
methodParameters["sSubKeyName"] = KeyName; | |
methodParameters["sValueName"] = ValueName; | |
// Executing the GetMultiStringValue method and creating an array of the resulting values. | |
var result = wmiExec(wmiServiceDefault, "StdRegProv", "GetMultiStringValue", methodParameters); | |
try { | |
var presentValues = result.sValue.toArray(); var newValues = new Array(); //marshal "presentValues" as proper Javascript Array as newValues | |
for (var i = 0; i < presentValues.length; i++) { | |
newValues.push(presentValues[i]); | |
} | |
//this polyfill implements Javascript ECMA-262 Array.protype.every() - We use this to compare the expected settings array with the returned results | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every | |
Array.prototype.every || (Array.prototype.every = function (a, b) { "use strict"; var c, d; if (null == this) throw new TypeError("this is null or not defined"); var e = Object(this), f = e.length >>> 0; if ("function" != typeof a) throw new TypeError; for (arguments.length > 1 && (c = b), d = 0; d < f;) { var g; if (d in e) { g = e[d]; var h = a.call(c, g, d, e); if (!h) return !1 } d++ } return !0 }); | |
//sort our arrays to avoid comparison failures due to reg_multi_sz ordering | |
sExpectedValue.sort(); newValues.sort(); | |
//compare our expected CI Settings with those returned from the registry | |
var is_same = sExpectedValue.length == newValues.length && sExpectedValue.every(function (element, index) { | |
return element === newValues[index]; | |
}); | |
if (is_same) { | |
WScript.Echo('pass') | |
} | |
else { | |
WScript.Echo('fail') | |
} | |
} | |
catch (err) { | |
WScript.Echo('fail'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment