MSSQL 2014: Service Pack Check

Microsoft.SQLServer.2014.ServicePackVersion (UnitMonitorType)

This monitor type checks the current service pack is greater than or equal the specified value.

Element properties:

RunAsMicrosoft.SQLServer.2014.SQLProbeAccount
AccessibilityInternal
Support Monitor RecalculateFalse

Member Modules:

ID Module Type TypeId RunAs 
DataSource DataSource System.CommandExecuterPropertyBagSource Default
BadFilter ConditionDetection System.ExpressionFilter Default
GoodFilter ConditionDetection System.ExpressionFilter Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IntervalSecondsint$Config/IntervalSeconds$Interval (seconds)The recurring interval of time in seconds in which to run the workflow.
GoodValueint$Config/GoodValue$Minimal Service Pack level for SQL Server 2014The minimal Service Pack level as per company policy. By default, it equals to 0 (integer).

Source Code:

<UnitMonitorType ID="Microsoft.SQLServer.2014.ServicePackVersion" Accessibility="Internal" RunAs="SQL2014Core!Microsoft.SQLServer.2014.SQLProbeAccount">
<MonitorTypeStates>
<MonitorTypeState ID="ValueGood"/>
<MonitorTypeState ID="ValueBad"/>
</MonitorTypeStates>
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ComputerName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SQLServiceName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="IntervalSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="GoodValue" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" ParameterType="int" Selector="$Config/IntervalSeconds$"/>
<OverrideableParameter ID="GoodValue" ParameterType="int" Selector="$Config/GoodValue$"/>
</OverrideableParameters>
<MonitorImplementation>
<MemberModules>
<DataSource TypeID="System!System.CommandExecuterPropertyBagSource" ID="DataSource">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<ApplicationName>%windir%\system32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>//nologo $File/DetectServicePackVersion.js$ "$Config/SQLServiceName$" "$Config/ComputerName$" $Config/GoodValue$</CommandLine>
<TimeoutSeconds>600</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>DetectServicePackVersion.js</Name>
<Contents><Script>//#Include File:SQL2014Constants.js

var MANAGEMENT_PACK_VERSION = "6.7.2.0"

//#Include File:Common.js

var MAX_DRIVER_VERSION_STR = "99999999.99999999.99999999.99999999";

Function.prototype.setAlias = function (funcName) {
this.getAlias = function () {
return funcName;
};
};

Error.prototype.toString = function () {
if (this.message) {
var parts = [];
parts.push(this.message);
parts.push('\n');

if (this.number) {
parts.push("Error Number : ");
parts.push(this.number);
parts.push('\n');
parts.push("Error Code : ");
parts.push(this.number &amp; 0xFFFF);
parts.push('\n');
parts.push("Win32 Facility : ");
parts.push((this.number &gt;&gt; 16 &amp; 0x1FFF));
parts.push('\n');
}
if (this.description) {
parts.push("Error Description : ");
parts.push(this.description);
}
parts.push('\n');
return parts.join('');
} else {
return this.toString();
}
};

Array.prototype.indexOf = function (item) {
for (var i = 0; i &lt; this.length; i++) {
if (this[i] === item) {
return i;
}
}
return -1;
};

Array.prototype.intersectsWith = function (arrB) {
for (var i = 0; i &lt; this.length; i++) {
for (var j = 0; j &lt; arrB.length; j++) {
if (this[i] === arrB[j]) {
return true;
}
}
}
return false;
};

if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}

var DriverVersion = function (versionStr) {
arguments.callee.setAlias('DriverVersion.constructor');
var versionRegex = /^(\d{1,8})(.\d{1,8}){0,3}$/g;
if (!versionRegex.exec(versionStr)) {
throw new Exception("Invalid version string.");
}

var parts = versionStr.split(".");
this.major = parseInt(parts[0]);
this.minor = 0;
this.build = 0;
this.revision = 0;
if (parts.length &gt; 1) {
this.minor = parseInt(parts[1]);
if (parts.length &gt; 2) {
this.build = parseInt(parts[2]);
if (parts.length &gt; 3) {
this.revision = parseInt(parts[3]);
}
}
}

this.toString = function () {
arguments.callee.setAlias('DriverVersion.toString');
return "" + this.major + "." + this.minor + "." + this.build + "." + this.revision;
};

this.compareTo = function (v) {
arguments.callee.setAlias('DriverVersion.compareTo');
if (!(v instanceof DriverVersion)) {
throw new Exception("Parameter is not an instance of type DriverVersion.");
}
var d = this.major - v.major;
if (d !== 0) {
return d;
}
d = this.minor - v.minor;
if (d !== 0) {
return d;
}
d = this.build - v.build;
if (d !== 0) {
return d;
}
d = this.revision - v.revision;
return d;
};
};

var VersionRange = function (sStartVersion, sEndVersion) {
arguments.callee.setAlias('VersionRange.constructor');
this.startVersion = GetDriverVersionObj(sStartVersion);
this.endVersion = GetDriverVersionObj(sEndVersion);
};

function GetDriverVersionObj(oVersion) {
arguments.callee.setAlias('GetDriverVersionObj');
if (oVersion instanceof DriverVersion) {
return oVersion
} else {
if ((typeof oVersion) === "string") {
return new DriverVersion(oVersion);
}
}
throw new Exception("Parameter is not an instance of type DriverVersion.");
}

var RegistryValueTypes = {
"REG_SZ": 1,
"REG_EXPAND_SZ": 2,
"REG_BINARY": 3,
"REG_DWORD": 4,
"REG_MULTI_SZ": 7
};

function EnumerateRegistryKeyValues(hiveName, keyPath) {
arguments.callee.setAlias('EnumerateRegistryKeyValues');
var hiveId, ex;
switch (hiveName) {
case "HKEY_CLASSES_ROOT":
hiveId = 2147483648;
break;
case "HKEY_CURRENT_USER":
hiveId = 2147483649;
break;
case "HKEY_LOCAL_MACHINE":
hiveId = 2147483650;
break;
case "HKEY_USERS":
hiveId = 2147483651;
break;
case "HKEY_CURRENT_CONFIG":
hiveId = 2147483653;
break;
default:
throw new Exception("Registry hive name '" + hiveName + "' is invalid");
}

try {
var providerName = "StdRegProv";
var services = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\default");
var registry = services.Get(providerName);
var enumFuncName = "EnumValues";

var inParams = registry.Methods_(enumFuncName).InParameters.SpawnInstance_();
inParams.hDefKey = hiveId;
inParams.sSubKeyName = keyPath;
var outParams = registry.ExecMethod_(enumFuncName, inParams);

var valueNames = outParams.sNames.toArray();
var valueTypes = outParams.Types.toArray();

return { ValueNames: valueNames, ValueTypes: valueTypes };
}
catch (ex) {
throw new Exception("Cannot enumerate values of registry key '" + hiveName + "\\" + keyPath + "'", ex);
}
}

var DriverItem = function (sName, oNameVersion, oDriverVersion, oParseObj) {
arguments.callee.setAlias('DriverItem.constructor');
this.name = sName;
this.nameVersion = GetDriverVersionObj(oNameVersion);
this.driverVersion = GetDriverVersionObj(oDriverVersion);
this.parseObject = oParseObj;
};

var DriverSelectorRule = function (sNameRegex, sNameVersionRegex, sNameMinVersion, sNameMaxVersion, oVersionRangeArray) {
arguments.callee.setAlias('DriverSelectorRule.constructor');
var _computerId = ".";
this.nameRegex = sNameRegex;
this.nameVersionRegex = sNameVersionRegex;
this.nameMinVersion = GetDriverVersionObj(sNameMinVersion);
this.nameMaxVersion = GetDriverVersionObj(sNameMaxVersion);
var _versionRangeArray = oVersionRangeArray;
this.driverCollection = new Array();
this.isNativeClient = false;
var _nameRegexObj = new RegExp(sNameRegex);

var _nameVersionRegexObj = null;
if (sNameVersionRegex) {
if (sNameVersionRegex.length &gt; 0) {
_nameVersionRegexObj = new RegExp(sNameVersionRegex);
}
}

var checkVersion = function (oVersion, vMinVersion, vMaxVersion) {
arguments.callee.setAlias('DriverSelectorRule.checkVersion');
var vVersion = GetDriverVersionObj(oVersion);
return vVersion.compareTo(vMinVersion) &gt;= 0 &amp;&amp; (vVersion.toString() == MAX_DRIVER_VERSION_STR || vMinVersion.compareTo(vMaxVersion) == 0 || vVersion.compareTo(vMaxVersion) &lt; 0);
};

this.checkNameVersion = function (oVersion) {
arguments.callee.setAlias('DriverSelectorRule.checkNameVersion');
return checkVersion(oVersion, this.nameMinVersion, this.nameMaxVersion);
};

this.checkDriverVersion = function (oVersion) {
arguments.callee.setAlias('DriverSelectorRule.checkDriverVersion');
var result = false;
for (var i = 0; i &lt; _versionRangeArray.length; i++) {
if (checkVersion(oVersion, _versionRangeArray[i].startVersion, _versionRangeArray[i].endVersion)) {
result = true;
break;
}
}
return result;
};

this.matchName = function (sName) {
arguments.callee.setAlias('DriverSelectorRule.matchName');
if (_nameRegexObj.exec(sName)) {
return true;
}
return false;
};

this.getNameVersion = function (sName) {
arguments.callee.setAlias('DriverSelectorRule.getNameVersion');
if (!_nameVersionRegexObj) {
return new DriverVersion("0");
}
var matches = _nameVersionRegexObj.exec(sName);
if (matches) {
return new DriverVersion(matches[0]);
}
throw new Exception("Driver name or version detection expression is invalid");
};

this.getDriverVersion = function (sName) {
arguments.callee.setAlias('DriverSelectorRule.getDriverVersion');
var registry = new Registry();
var driverPath = registry.Read("HKEY_LOCAL_MACHINE\\SOFTWARE\\ODBC\\ODBCINST.INI\\" + sName + "\\Driver");
try {
var objFso = new ActiveXObject("Scripting.FileSystemObject");
var sDllVersion = objFso.GetFileVersion(driverPath);
} catch (ex) {
throw new Exception("Cannot obtain driver version using path '" + driverPath + "'");
}
return new DriverVersion(sDllVersion);
};

this.addDriver = function (oDriver) {
arguments.callee.setAlias('DriverSelectorRule.addDriver');
if (!(oDriver instanceof DriverItem)) {
throw new Exception("Parameter is not an instance of type DriverItem.")
}
this.driverCollection.push(oDriver);
};

this.resetState = function () {
this.driverCollection = new Array();
};
};

var DriverSelector = function () {
arguments.callee.setAlias('DriverSelector.constructor');
var _selectorRules = new Array();
var _selectedDriverName = null;

var _computerId = ".";
var _defaultDriverName = "SQL Server";

var _ncli_ForceProtocolEncryption = false;
var _ncli_TrustServerCertificate = false;
var _ncli_tcpProtocolEnabled = true;
var _processed = false;
var _errorCollection = new Array();

var pr = new DriverSelectorRule("^SQL\\sServer\\sNative\\sClient\\s\\d{1,8}(\\.\\d{1,8})?$", "\\d{1,8}(\\.\\d{1,8})?$", "11.0", "11.0",
new Array(
new VersionRange("2011.110.6020.0", MAX_DRIVER_VERSION_STR),
new VersionRange("2011.110.5592.0", "2011.110.6000.0"),
new VersionRange("2011.110.5347.0", "2011.110.5522.0")));
pr.isNativeClient = true;
_selectorRules.push(pr);
_selectorRules.push(new DriverSelectorRule("^ODBC\\sDriver\\s\\d{1,8}(\\.\\d{1,8})?\\sfor\\sSQL\\sServer$", "\\d{1,8}(\\.\\d{1,8})?(?=\\sfor\\sSQL\\sServer$)", "11.0", MAX_DRIVER_VERSION_STR,
new Array(
new VersionRange("2014.120.4219.0", MAX_DRIVER_VERSION_STR),
new VersionRange("2014.120.2546.0", "2014.120.4000.0")
)));
pr = new DriverSelectorRule("^SQL\\sServer\\sNative\\sClient\\s\\d{1,8}(\\.\\d{1,8})?$", "\\d{1,8}(\\.\\d{1,8})?$", "0", MAX_DRIVER_VERSION_STR, [new VersionRange("0", MAX_DRIVER_VERSION_STR)]);
pr.isNativeClient = true;
_selectorRules.push(pr);
_selectorRules.push(new DriverSelectorRule("^ODBC\\sDriver\\s\\d{1,8}(\\.\\d{1,8})?\\sfor\\sSQL\\sServer$", "\\d{1,8}(\\.\\d{1,8})?(?=\\sfor\\sSQL\\sServer$)", "0", MAX_DRIVER_VERSION_STR, [new VersionRange("0", MAX_DRIVER_VERSION_STR)]));

function processSystemOdbcDrivers(ncProtocolsAreIntersected) {
arguments.callee.setAlias('DriverSelector.processSystemOdbcDrivers');
var i, j;
var registry = new Registry();
var oValues = EnumerateRegistryKeyValues("HKEY_LOCAL_MACHINE", "SOFTWARE\\ODBC\\ODBCINST.INI\\ODBC Drivers");
var driverNameArr = oValues.ValueNames;
var valueTypeArr = oValues.ValueTypes;
for (i = 0; i &lt; driverNameArr.length; i++) {
try {
if (valueTypeArr[i] !== RegistryValueTypes.REG_SZ) {
continue;
}
var isInstalled = registry.Read("HKEY_LOCAL_MACHINE\\SOFTWARE\\ODBC\\ODBCINST.INI\\ODBC Drivers" + "\\" + driverNameArr[i]);
if (isInstalled != "Installed") {
continue;
}
for (j = 0; j &lt; _selectorRules.length; j++) {
if (_selectorRules[j].matchName(driverNameArr[i])) {
if (!_selectorRules[j].isNativeClient || (_selectorRules[j].isNativeClient &amp;&amp; ncProtocolsAreIntersected)) {
var nameVersion = _selectorRules[j].getNameVersion(driverNameArr[i]);
if (_selectorRules[j].checkNameVersion(nameVersion)) {
var driverVersion = _selectorRules[j].getDriverVersion(driverNameArr[i]);
if (_selectorRules[j].checkDriverVersion(driverVersion)) {
var driverItem = new DriverItem(driverNameArr[i], nameVersion, driverVersion, _selectorRules[j]);
_selectorRules[j].addDriver(driverItem);
}
}
}
}
}
} catch (ex) {
addError(ex);
}
}
}

function getNsNameWithHighestVersion() {
arguments.callee.setAlias('DriverSelector.getNsNameWithHighestVersion');
var rootNs = "root\\Microsoft\\SqlServer";
var oWMI = new WMIProvider(rootNs);
var namespaces = oWMI.ExecQuery("SELECT Name FROM __NAMESPACE WHERE Name LIKE 'ComputerManagement%'");
if (namespaces.Count &gt; 0) {
var e = new Enumerator(namespaces);
var currentName = "";
for (; !e.atEnd() ; e.moveNext()) {
var nsName = e.item().Name;
currentName = nsName &gt; currentName ? nsName : currentName;
}
} else {
throw new Exception("No namespace 'ComputerManagement*' was found");
}
return rootNs + "\\" + currentName;
}

function getNativeClientOdbcSettings(namespaceFullName) {
arguments.callee.setAlias('DriverSelector.getNativeClientOdbcSettings');
var oWMI = new WMIProvider(namespaceFullName);
var oQuery = oWMI.ExecQuery("SELECT FlagName, FlagValue FROM ClientSettingsGeneralFlag");
if (oQuery.Count &gt; 0) {
var e = new Enumerator(oQuery);
for (; !e.atEnd() ; e.moveNext()) {
var prop = e.item();
switch (prop.FlagName) {
case "Force protocol encryption":
_ncli_ForceProtocolEncryption = prop.FlagValue;
break;
case "Trust Server Certificate":
_ncli_TrustServerCertificate = prop.FlagValue;
break;
}
}
}
}

function getEnabledNativeClientProtocols(namespaceFullName) {
var oWMI = new WMIProvider(namespaceFullName);
var oQuery = oWMI.ExecQuery("SELECT ProtocolName, ProtocolOrder FROM ClientNetworkProtocol");
var protocolsArr = [];

if (oQuery.Count &gt; 0) {
for (var e = new Enumerator(oQuery) ; !e.atEnd() ; e.moveNext()) {
if (e.item().ProtocolOrder &gt; 0) {
protocolsArr.push(e.item().ProtocolName.toLowerCase());
}
}
}
return protocolsArr;
}

function resetState() {
_errorCollection = new Array();
for (var i = 0; i &lt; _selectorRules.length; i++) {
_selectorRules[i].resetState();
}
}

function addError(oErr) {
var errStr = typeof (oErr.toString) === 'undefined' ? "[" + (oErr.number &amp; 0xFFFF) + "] " + oErr.message : oErr.toString();
_errorCollection.push(errStr);
}

this.processDrivers = function (enabledServerProtocols) {
arguments.callee.setAlias('DriverSelector.processDrivers');
var i, j;
resetState();

var ncNamespace = getNsNameWithHighestVersion();
var clientProtocols = getEnabledNativeClientProtocols(ncNamespace);
var ncProtocolsAreIntersected = clientProtocols.intersectsWith(enabledServerProtocols);

try {
processSystemOdbcDrivers(ncProtocolsAreIntersected);
} catch (ex) {
addError(ex);
_selectedDriverName = _defaultDriverName;
_processed = true;
return;
}
var currentSelect = null;
for (i = 0; i &lt; _selectorRules.length; i++) {
for (j = 0; j &lt; _selectorRules[i].driverCollection.length; j++) {
if (currentSelect) {
if (_selectorRules[i].driverCollection[j].nameVersion.compareTo(currentSelect.nameVersion) &gt;= 0 &amp;&amp; _selectorRules[i].driverCollection[j].driverVersion.compareTo(currentSelect.driverVersion) &gt;= 0) {
currentSelect = _selectorRules[i].driverCollection[j];
}
} else {
currentSelect = _selectorRules[i].driverCollection[j];
}
}
if (currentSelect) {
break;
}
}
if (currentSelect) {
_selectedDriverName = currentSelect.name;
if (currentSelect.parseObject.isNativeClient) {
try {
getNativeClientOdbcSettings(ncNamespace);
_ncli_tcpProtocolEnabled = ((getEnabledNativeClientProtocols(ncNamespace)).indexOf("tcp") &gt;= 0);
} catch (ex) {
addError(ex);
}
}
} else {
_selectedDriverName = _defaultDriverName;
}
_processed = true;
};

this.getErrorCollection = function () {
return _errorCollection.slice();
};

this.hasErrors = function () {
return _errorCollection.length &gt; 0;
};

function throwIfNotProcessed() {
if (!_processed) {
throw new Exception("Drivers are not processed. Call 'ProcessDrivers' first.");
}
}

this.getSelectedDriverName = function () {
throwIfNotProcessed()
return _selectedDriverName;
};

this.getUseFqdn = function () {
throwIfNotProcessed()
return _ncli_ForceProtocolEncryption &amp;&amp; !_ncli_TrustServerCertificate;
};

this.getClientTcpProtocolEnabled = function () {
throwIfNotProcessed();
return _ncli_tcpProtocolEnabled;
};
};

var UniqueCollection = function () {
var _dict = new ActiveXObject("Scripting.Dictionary");

this.put = function (item) {
if (!_dict.Exists(item)) {
_dict.add(item, '');
}
};

this.exists = function (item) {
return _dict.Exists(item);
};

this.get = function () {
return (new VBArray(_dict.Keys())).toArray();
};

this.clear = function () {
_dict.RemoveAll();
};
};

var Common = function () {
arguments.callee.setAlias('Common.constructor');
this.DEBUG = false;
this.ManagementGroupName = "$Target/ManagementGroup/Name$";
this.ManagementGroupID = "$Target/ManagementGroup/Id$";

this.EscapeWQLString = function (wqlString) {
return wqlString.replace(/'/g, "\\'");
};

this.ParseInputBool = function(inputVal, allowEmpty, errorMessage) {
if (inputVal === null || inputVal === "") {
if (allowEmpty) {
return "true";
} else {
throw new Exception(errorMessage);
}
}
var supportedValues = ["true", "false", "t", "f", ".t.", ".f.", "1", "0"];
var replacements = ["true", "false", "true", "false", "true", "false", "true", "false"];

var processed = ("" + inputVal).trim().toLowerCase();
var pos = supportedValues.indexOf(processed);
if (pos &lt; 0) {
throw new Exception(errorMessage);
}
return replacements[pos];
};

this.EscapeConnStringValue = function (connStringValue) {
return '{' + connStringValue.replace(/}/g, '}}') + '}';
};

this.DelimitSqlIdentifier = function(identifier) {
return "[" + identifier.replace(/\]/g, "]]") + "]";
};

this.GetConnectionString = function (driverName, dataSource, databaseName) {
return "Driver=" + this.EscapeConnStringValue(driverName) + ";Server=" + this.EscapeConnStringValue(dataSource) + ";Database=" + this.EscapeConnStringValue(databaseName) + ";Trusted_Connection=yes;";
};

this.SqlTcpPortIsEmpty = function(tcpPort) {
return tcpPort == "" || tcpPort == "0";
};

this.GetEnabledSqlServerProtocols = function(namespaceName, computerName, instanceName) {
var oWMI = new WMIProvider("root\\Microsoft\\SqlServer\\" + namespaceName);
var oQuery = oWMI.ExecQuery("SELECT ProtocolName, Enabled FROM ServerNetworkProtocol WHERE InstanceName = '" + this.EscapeWQLString(instanceName) + "'");
var protocolsArr = [];

if (oQuery.Count &gt; 0) {
for (var e = new Enumerator(oQuery); !e.atEnd(); e.moveNext()) {
if (e.item().Enabled) {
protocolsArr.push(e.item().ProtocolName.toLowerCase());
}
}
}
return protocolsArr;
};

this.GetSqlServerTcpIpSettings = function(instanceName) {
arguments.callee.setAlias('GetSqlServerTcpIpSettings');

var listenAllIps = false;
var e, e2, matches;
var uc = new UniqueCollection();
var ipSettings = new ActiveXObject("Scripting.Dictionary");
var portRegex = /(?:,|^)\s*([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])\s*(?=,|$)/g;
var oWMI = new WMIProvider("root\\Microsoft\\SqlServer\\ComputerManagement12");

var oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + this.EscapeWQLString(instanceName) + "' AND PropertyName = 'ListenOnAllIPs'");
if (oQuery.Count &gt; 0) {
e = new Enumerator(oQuery);
e.moveFirst();
var isListenAll = e.item();
if (isListenAll.PropertyNumVal === 1) {
listenAllIps = true;
}
}
if (listenAllIps) {
oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + this.EscapeWQLString(instanceName) + "' AND IPAddressName = 'IPAll' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal != ''");
if (oQuery.Count &gt; 0) {
for (e = new Enumerator(oQuery); !e.atEnd(); e.moveNext()) {
while (matches = portRegex.exec(e.item().PropertyStrVal)) {
if (!this.SqlTcpPortIsEmpty(matches[1])) {
uc.put(matches[1]);
}
}
}
ipSettings.add("IPAll", uc.get());
}
} else {
oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + this.EscapeWQLString(instanceName) + "' AND IPAddressName != '' AND PropertyName = 'Enabled' AND PropertyNumVal = 1");
if (oQuery.Count &gt; 0) {
for (e = new Enumerator(oQuery); !e.atEnd(); e.moveNext()) {
var ipAddress = '';
var ipAddressName = e.item().IPAddressName;
var oQuery2 = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + this.EscapeWQLString(instanceName) + "' AND IPAddressName = '" + this.EscapeWQLString(ipAddressName) + "' AND PropertyName = 'IpAddress' AND PropertyStrVal != ''");
if (oQuery2.Count &gt; 0) {
e2 = new Enumerator(oQuery2);
e2.moveFirst();
ipAddress = e2.item().PropertyStrVal;
} else {
continue;
}
oQuery2 = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + this.EscapeWQLString(instanceName) + "' AND IPAddressName = '" + this.EscapeWQLString(ipAddressName) + "' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal != ''");
uc.clear();
if (oQuery2.Count &gt; 0) {
for (e2 = new Enumerator(oQuery2); !e2.atEnd(); e2.moveNext()) {
while (matches = portRegex.exec(e2.item().PropertyStrVal)) {
if (!this.SqlTcpPortIsEmpty(matches[1])) {
uc.put(matches[1]);
}
}
}
try {
ipSettings.add(ipAddress, uc.get());
} catch(ex) {
}
}
}
}
}
return { listenAllIPs: listenAllIps, ipSettings: ipSettings };
};

this.CheckConnectionTarget = function (dbConnection, serverName, instanceName) {
arguments.callee.setAlias('CheckConnectionTarget');

var serverNameWithoutDomain = serverName;
var destinationTestQuery = "select CAST(SERVERPROPERTY('MachineName') AS nvarchar(128)) as ServerName, @@servicename as InstanceName";
var queryResult = dbConnection.ExecuteQuery(destinationTestQuery);
queryResult.MoveFirst();
if (!queryResult.EOF) {
var queryServerName = queryResult("ServerName").Value.toUpperCase();
var queryInstanceName = queryResult("InstanceName").Value.toUpperCase();
var dotPosition = serverName.indexOf(".");
if (dotPosition &gt; -1) {
serverNameWithoutDomain = serverName.substring(0, dotPosition);
}
if (serverNameWithoutDomain.toUpperCase() == queryServerName &amp;&amp; instanceName.toUpperCase() == queryInstanceName) {
return;
}
}
dbConnection.Close();
throw new Exception("Connection target check failed: connected to " + serverNameWithoutDomain + "\\" + instanceName + ", but got " + queryServerName + "\\" + queryInstanceName + ".");
};

this.GetDataSource = function(server, tcpPort) {
var dataSource = server;
if ((tcpPort !== "") &amp;&amp; (tcpPort !== "0")) {
var nameParts = dataSource.split("\\");
dataSource = nameParts[0] + "," + tcpPort;
}
return dataSource;
};

this.GetAndCheckConnection = function(driverName, dataSource, databaseName, provider, timeout, machineName, instanceName) {
arguments.callee.setAlias('GetAndCheckConnection');

var connectionString = this.GetConnectionString(driverName, dataSource, databaseName);
var dbMasterConnection = new ADODB(connectionString, provider, timeout);
this.CheckConnectionTarget(dbMasterConnection, machineName, instanceName);
return dbMasterConnection;
};

this.SmartConnect = function(serverName, databaseName, tcpPort, machineName, instanceName) {
arguments.callee.setAlias('SmartConnect');

var dataSource;
var targetName = serverName;
var lastError = null;
var errorMessageColl = [];

var enabledServerProtocols = this.GetEnabledSqlServerProtocols("ComputerManagement12", machineName, instanceName);

var ds = new DriverSelector();
ds.processDrivers(enabledServerProtocols);
var hasErrors = ds.hasErrors();
var useFqdn = ds.getUseFqdn();
var driverName = ds.getSelectedDriverName();

//try to use SQL server browser
try {
dataSource = this.GetDataSource(serverName, "");
return this.GetAndCheckConnection(driverName, dataSource, databaseName, "", 15, machineName, instanceName);
} catch(ex) {
lastError = ex;
errorMessageColl.push("Connection to data source '" + dataSource + "' failed: " + ex.message);
}

if (enabledServerProtocols.indexOf("tcp") &gt;= 0 &amp;&amp; ds.getClientTcpProtocolEnabled()) {
var pathArray = serverName.split("\\");
var dsComputerName = pathArray[0];
targetName = useFqdn ? machineName : dsComputerName;

var tcpIpSettings = this.GetSqlServerTcpIpSettings(instanceName);

if (tcpIpSettings.listenAllIPs) {
for (var j = 0; j &lt; tcpIpSettings.ipSettings.item("IPAll").length; j++) {
try {
dataSource = this.GetDataSource(targetName, tcpIpSettings.ipSettings.item("IPAll")[j]);
return this.GetAndCheckConnection(driverName, dataSource, databaseName, "", 10, machineName, instanceName);
} catch(ex) {
lastError = ex;
errorMessageColl.push("Connection to data source '" + dataSource + "' failed: " + ex.message);
}
}
} else {
var ipArr = (new VBArray(tcpIpSettings.ipSettings.Keys())).toArray();
var upc = new UniqueCollection();
for (var i = 0; i &lt; ipArr.length; i++) {
if (!upc.exists(tcpIpSettings.ipSettings.item(ipArr[i])[0])) {
upc.put(tcpIpSettings.ipSettings.item(ipArr[i])[0]);
try {
dataSource = this.GetDataSource(targetName, tcpIpSettings.ipSettings.item(ipArr[i])[0]);
return this.GetAndCheckConnection(driverName, dataSource, databaseName, "", 10, machineName, instanceName);
} catch(ex) {
lastError = ex;
errorMessageColl.push("Connection to data source '" + dataSource + "' failed: " + ex.message);
}
}
}
if (!useFqdn) {
for (var i = 0; i &lt; ipArr.length; i++) {
var portArr = tcpIpSettings.ipSettings.item(ipArr[i]);
for (var j = 0; j &lt; portArr.length; j++) {
try {
dataSource = this.GetDataSource(ipArr[i], tcpIpSettings.ipSettings.item(ipArr[i])[j]);
return this.GetAndCheckConnection(driverName, dataSource, databaseName, "", 10, machineName, instanceName);
} catch(ex) {
lastError = ex;
errorMessageColl.push("Connection to data source '" + dataSource + "' failed: " + ex.message);
}
}
}
}
}
}
throw new Exception("Cannot connect to the target Sql Server instance. Connection log:\n" + errorMessageColl.join("\n"), lastError);
};
};

var Exception = function (message, innerException) {
arguments.callee.setAlias('Exception.constructor');
this.message = message;
this.innerException = innerException;

var parts = [];
var collectArguments = function (caller) {
parts.push('(');
for (var i = 0; i &lt; caller.length; i++) {
if (typeof (caller[i]) !== 'undefined') {
parts.push(caller[i] === null ? 'null' : (typeof (caller[i].toString) === 'undefined' ? 'Object' : caller[i].toString()));
parts.push(',');
}
}
parts.pop();
parts.push(')');
parts.push(',\n');
};
var collectCallStack = function (caller) {
arguments.callee.setAlias('Exception.collectCallStack');
parts.push(caller.callee.getAlias ? caller.callee.getAlias() : 'anonymous');
collectArguments(caller);
var nextCaller = caller.caller;
if (nextCaller) {
collectCallStack(nextCaller);
}
};
collectCallStack(arguments);
this.callStack = parts.join('');

this.toString = function () {
arguments.callee.setAlias('Exception.toStringFull');
var toStringParts = [];
toStringParts.push(this.message);
toStringParts.push('\n');
if (this.innerException) {
if (this.innerException.number || this.innerException.description) {
toStringParts.push('Inner exception: \n');
if (this.innerException.number) {
toStringParts.push("Error Number : ");
toStringParts.push(this.innerException.number);
toStringParts.push('\n');
toStringParts.push("Error Code : ");
toStringParts.push(this.innerException.number &amp; 0xFFFF);
toStringParts.push('\n');
toStringParts.push("Win32 Facility : ");
toStringParts.push((this.innerException.number &gt;&gt; 16 &amp; 0x1FFF));
toStringParts.push('\n');
}
if (this.innerException.description) {
toStringParts.push("Error Description : ");
toStringParts.push(this.innerException.description);
toStringParts.push('\n');
}
}
}
toStringParts.push('Call stack:');
toStringParts.push(this.callStack);
return toStringParts.join('');
};
};

var OpsMgrAPI = function () {
arguments.callee.setAlias('MOMAPI.constructor');
var scriptAPI;
try {
scriptAPI = new ActiveXObject('MOM.ScriptAPI');
} catch (e) {
throw new Exception("Application cannot create MOM.ScriptAPI ActiveX object.", e);
}
this.ScriptAPI = scriptAPI;
};

var Logger = function () {
arguments.callee.setAlias('Logger.constructor');
this.common = new Common();
this.messageType = {
error: 1,
warning: 2,
information: 4
};

var opsMgrAPI = new OpsMgrAPI();

this.LogError = function (eventId, logMessage) {
arguments.callee.setAlias('Logger.LogError');
opsMgrAPI.ScriptAPI.LogScriptEvent("Management Group: " + this.common.ManagementGroupName + ". Script: " + WScript.ScriptName + ". Version: " + MANAGEMENT_PACK_VERSION, eventId, this.messageType.error, logMessage);
};
this.LogError2 = function (instanceName, eventId, logMessage) {
arguments.callee.setAlias('Logger.LogError2');
opsMgrAPI.ScriptAPI.LogScriptEvent("Management Group: " + this.common.ManagementGroupName + ". Script: " + WScript.ScriptName + ". Version: " + MANAGEMENT_PACK_VERSION + ". Instance: " + instanceName, eventId, this.messageType.error, logMessage);
};
this.LogWarning = function (eventId, logMessage) {
arguments.callee.setAlias('Logger.LogWarning');
opsMgrAPI.ScriptAPI.LogScriptEvent("Management Group: " + this.common.ManagementGroupName + ". Script: " + WScript.ScriptName + ". Version: " + MANAGEMENT_PACK_VERSION, eventId, this.messageType.warning, logMessage);
};
this.LogInformation = function (eventId, logMessage) {
arguments.callee.setAlias('Logger.LogInformation');
opsMgrAPI.ScriptAPI.LogScriptEvent("Management Group: " + this.common.ManagementGroupName + ". Script: " + WScript.ScriptName + ". Version: " + MANAGEMENT_PACK_VERSION, eventId, this.messageType.information, logMessage);
};
this.LogDebug = function (eventId, logMessage) {
if (this.common.DEBUG) {
arguments.callee.setAlias('Logger.LogDebug');
opsMgrAPI.ScriptAPI.LogScriptEvent("Management Group: " + this.common.ManagementGroupName + ". Script: " + WScript.ScriptName + ". Version: " + MANAGEMENT_PACK_VERSION, eventId, this.messageType.information, logMessage);
}
};
this.LogCustomInfo = function (param1, eventId, logMessage) {
arguments.callee.setAlias('Logger.LogCustomInfo');
opsMgrAPI.ScriptAPI.LogScriptEvent(param1, eventId, this.messageType.information, logMessage);
};
};

var ADODB = function (connectionString, provider, connectionTimeout) {
arguments.callee.setAlias('ADODB.constructor');
if (!connectionString) {
throw new Exception("Connection string cannot be null or empty string");
}



this.connectionTimeout = connectionTimeout ? connectionTimeout : 30;

var connection;
try {
connection = new ActiveXObject("ADODB.Connection");
} catch (e) {
throw new Exception("Can't create ActiveXObject 'ADODB.Connection'", e);
}

if (provider) {
connection.Provider = provider;
}

connection.ConnectionTimeout = this.connectionTimeout;
connection.ConnectionString = connectionString;
this.provider = connection.Provider;

try {
connection.Open();
} catch (e) {
throw new Exception("Can't connect to SQL Server. Connection string : '" + connectionString + "'. Error description: " + e.message, e);
}

this.ExecuteQuery = function (query) {
arguments.callee.setAlias('ADODB.ExecuteQuery');
var dbSet;
try {
dbSet = connection.Execute(query);
} catch (e) {
throw new Exception("Can't execute query '" + query + "': " + e.message, e);
}
return dbSet;
};

this.ExecuteQueryWithParams = function (query) {
arguments.callee.setAlias('ADODB.ExecuteQueryWithParams');
if (arguments.length &lt;= 1) {
throw new Exception("Can't go without any params");
}
var dbSet;
try {
var command = new ActiveXObject("ADODB.Command");
command.ActiveConnection = connection;
command.CommandText = query;
command.CommandType = 1; // adCmdText
for (var i = 1; i &lt; arguments.length; i++) {
this.AddParam(command, arguments[i]);
}
dbSet = command.Execute();
} catch (e) {
throw new Exception("Can't execute query '" + query + "': " + e.message, e);
}
return dbSet;
};

this.AddParam = function (cmd, value) {
var parameter;
switch (typeof value) {
case "number":
parameter = cmd.CreateParameter("", 20, 1, 8, value); // , adBigInt, adParamInput
break;
case "string":
parameter = cmd.CreateParameter("", 202, 1, Math.max(value.length, 1), value); // , adVarWChar, adParamInput
break;
default:
throw new Exception("Unknown parameter type: " + typeof value);
}
cmd.Parameters.Append(parameter);
};

this.Close = function () {
arguments.callee.setAlias('ADODB.Close');
try {
if (connection) {
connection.Close();
}
}
catch (e) {
throw new Exception("Can't close connection.", e);
}
};
};

var Registry = function () {
arguments.callee.setAlias('Registry.constructor');
var registry;
try {
registry = new ActiveXObject("WScript.Shell");
} catch (e) {
throw new Exception("Can't create ActiveXObject 'WScript.Shell'", e);
}

this.Write = function (key, value, type) {
arguments.callee.setAlias('Registry.Write');
try {
if (type) {
registry.RegWrite(key, value, type);
} else {
registry.RegWrite(key, value);
}
} catch (e) {
throw new Exception("Can't write value '" + value.toString() + "' by registry key '" + key.toString() + "'.", e);
}
};
this.Read = function (key) {
arguments.callee.setAlias('Registry.Read');
var value;
try {
value = registry.RegRead(key);
} catch (e) {
throw new Exception("Can't read value from registry key '" + key.toString() + "'.", e);
}
return value;
};
this.IsKeyExists = function (key) {
arguments.callee.setAlias('Registry.IsKeyExists');
var result;
try {
registry.RegRead(key);
result = true;
} catch (e) {
result = false;
}
return result;
};
this.Delete = function (key) {
arguments.callee.setAlias('Registry.Delete');
var value;
try {
value = registry.RegDelete(key);
} catch (e) {
throw new Exception("Can't delete registry key '" + key.toString() + "'.", e);
}
return value;
};
};

var WMIProvider = function (wmiNamespace, computerName) {
arguments.callee.setAlias('WMIProvider.constructor');
this.wmiNamespace = wmiNamespace;
this.computerName = (!computerName) ? computerName : ".";
var sWbemLocator;
try {
sWbemLocator = new ActiveXObject("WbemScripting.SWbemLocator");
} catch (e) {
throw new Exception("Cannot create 'WbemScripting.SWbemLocator' object.", e);
}
this.SWbemLocator = sWbemLocator;

var wmiService;
try {
wmiService = this.SWbemLocator.ConnectServer(this.computerName, this.wmiNamespace);
} catch (e) {
throw new Exception("Cannot connect to WMI of computer '" + this.computerName + "' by namespace '" + this.wmiNamespace + "'.", e);
}
this.WMIService = wmiService;

this.ExecQuery = function (query) {
arguments.callee.setAlias('WMIProvider.ExecQuery');
var result = null;
try {
result = this.WMIService.ExecQuery(query, "WQL", 0x0);
} catch (e) {
throw new Exception("Cannot execute WMI query '" + query + "'.", e);
}
return result;
};
};

var DBHelper = function () {
arguments.callee.setAlias('DBHelper.constructor');
this.EVENT_ID = 4201;
this.DatabaseState = {
ONLINE: 0,
RESTORING: 1,
RECOVERING: 2,
RECOVERY_PENDING: 3,
SUSPECT: 4,
EMERGENCY: 5,
OFFLINE: 6
};
this.common = new Common();
this.logger = new Logger();

this.IsRunningInstanceSQLServer = function (sqlInstanceName) {
arguments.callee.setAlias('IsRunningInstanceSQLServer');
var isRunning = false;
try {
var wmiProvider = new WMIProvider("root\\Microsoft\\SqlServer\\ComputerManagement12");
var sqlService = wmiProvider.ExecQuery("select state from SqlService WHERE (ServiceName = '" + this.common.EscapeWQLString(sqlInstanceName) + "' OR ServiceName = 'MSSQL$" + this.common.EscapeWQLString(sqlInstanceName) + "') and SQLServiceType ='1'");

if (sqlService.Count &gt; 0) {
var e = new Enumerator(sqlService);
e.moveFirst();
var sqlServiceObject = e.item();

if (sqlServiceObject.State === 4) {
isRunning = true;
}
}
} catch (exception) {
}
return isRunning;
};

this.GetDatabaseExcludeList = function (instanceName) {
arguments.callee.setAlias('GetDatabaseExcludeList');
var databaseExcludeList = "";
try {
var opsMgrAPI = new OpsMgrAPI();
var registryKey = opsMgrAPI.ScriptAPI.GetScriptStateKeyPath(this.common.ManagementGroupID);
this.logger.LogDebug(this.EVENT_ID, "ScriptStateKeyPath: " + registryKey);

var registry = new Registry();
var databaseExcludeListKey = "HKEY_LOCAL_MACHINE\\" + registryKey + "\\" + instanceName + "\\DatabaseExcludeList";
if (registry.IsKeyExists(databaseExcludeListKey)) {
databaseExcludeList = registry.Read(databaseExcludeListKey);
databaseExcludeList = databaseExcludeList.replace(/(^\s+)|(\s+$)/g, "");
if (databaseExcludeList !== '*') {
var excludeDatabases = databaseExcludeList.split(",");
for (var i = 0; i &lt; excludeDatabases.length; i++) {
excludeDatabases[i] = excludeDatabases[i].replace(/(^\s+)|(\s+$)/g, "");
}
databaseExcludeList = excludeDatabases.join(",");
}
}
} catch (e) {
throw new Exception("Can't read database exclude list from registry.", e);
}
return databaseExcludeList;
};

// Returns -1: If DB is not in AlwaysOn
// Returns 0: If DB is in AlwaysOn and replica allow connections is NO
// Returns 1: If DB is in AlwaysOn and replica allow connections is YES
this.AlwaysOnReplicaAllowConnections = function (aDbConnection, aDatabaseID) {
var query = " SELECT columns.id, " +
" CASE WHEN OBJECT_ID('sys.availability_replicas') IS NOT NULL THEN 1 ELSE 0 END AS HasAlwaysOn " +
" FROM master.sys.syscolumns columns where name = 'replica_id' and id = OBJECT_ID('sys.databases')";

var result = aDbConnection.ExecuteQuery(query);
if (!result.EOF) {
result.MoveFirst();
if (result("HasAlwaysOn").Value === 1) {
query = "SELECT d.name, d.database_id, drs.is_primary_replica AS db_is_primary_replica " +
", CASE WHEN d.replica_id IS NULL THEN 0 ELSE 1 END AS is_replica " +
", CASE WHEN drs.is_primary_replica = 1 THEN ar.primary_role_allow_connections ELSE ar.secondary_role_allow_connections END AS role_allow_connections " +
", CASE WHEN drs.is_suspended = 0 THEN -1 ELSE suspend_reason END AS db_suspended_state " +
" FROM sys.databases as d " +
" JOIN sys.dm_hadr_database_replica_states drs ON drs.database_id = d.database_id " +
" JOIN sys.availability_replicas ar on d.replica_id = ar.replica_id WHERE drs.is_local = 1 AND d.database_id = ?";

result = aDbConnection.ExecuteQueryWithParams(query, aDatabaseID);
if (!result.EOF) {
if (result("is_replica").Value === 1) {
if (result("role_allow_connections").Value &lt;= 1) {
return 0;
} else {
if ((result("db_suspended_state").Value &gt; 0) &amp;&amp;
(!result("db_is_primary_replica").Value || (result("db_is_primary_replica").Value &amp;&amp; result("db_suspended_state").Value !== 5))) {
return 0;
}
return 1;
}
}
}
}
}
return -1;
};
};

var ServicesHelper = function () {
arguments.callee.setAlias('ServicesHelper.constructor');

this.EVENT_ID = 4201;
this.IsServiceUnavailable = "ServiceUnavailable";
this.logger = new Logger();
this.common = new Common();

this.GetServiceUnavailableRegistryKey = function (serviceName) {
arguments.callee.setAlias('GetRegistryKeyForService');
var opsMgrAPI = new OpsMgrAPI();
var registryKey = opsMgrAPI.ScriptAPI.GetScriptStateKeyPath(this.common.ManagementGroupID);
var key = "HKEY_LOCAL_MACHINE\\" + registryKey + "\\" + serviceName + "\\";
return key;
};

this.ReturnServiceStateToOpsMgr = function (serviceState) {
var opsMgrAPI = new OpsMgrAPI();
this.logger.LogDebug(this.EVENT_ID, "Return to OpsMgr state of service = " + serviceState);
var propertyBag = opsMgrAPI.ScriptAPI.CreatePropertyBag();
propertyBag.AddValue("ServiceState", serviceState);
opsMgrAPI.ScriptAPI.AddItem(propertyBag);
opsMgrAPI.ScriptAPI.ReturnItems();
};
};
//#Include File:DetectServicePackVersion.js

function SQL2014Helper(computerName, service) {
arguments.callee.setAlias('SQL2014Helper');

var serviceName = service;

var wmiProvider = new WMIProvider("root\\Microsoft\\SQLServer\\ComputerManagement12", computerName);

var escapeString = function (str) {
arguments.callee.setAlias('escape');

return str.replace("\\", "\\\\").replace("'", "\\'");
};

var getVersion = function () {
arguments.callee.setAlias('getVersion');

var result = wmiProvider.ExecQuery("SELECT PropertyStrValue FROM SQLServiceAdvancedProperty WHERE ServiceName = '" + escapeString(serviceName) + "' AND PropertyName = 'VERSION'");
if (result.Count &gt; 0) {
// return result.ItemIndex(0).PropertyStrValue; // This code doesn't work in Windows 2003 or less

var e = new Enumerator(result);
e.moveFirst();
return e.item().PropertyStrValue;
} else {
throw new Exception("Can't get SQLServiceAdvancedProperty.VERSION property from WMI.");
}
};

var getServicePack = function () {
arguments.callee.setAlias('getServicePack');

var result = wmiProvider.ExecQuery("SELECT PropertyNumValue FROM SQLServiceAdvancedProperty WHERE ServiceName = '" + escapeString(serviceName) + "' AND PropertyName = 'SPLEVEL'");
if (result.Count &gt; 0) {
// return parseInt(result.ItemIndex(0).PropertyNumValue); // This code doesn't work in Windows 2003 or less

var e = new Enumerator(result);
e.moveFirst();
return parseInt(e.item().PropertyNumValue);
} else {
throw new Exception("Can't get SQLServiceAdvancedProperty.SPLEVEL property from WMI.");
}
};

var version = getVersion();
this.servicePack = getServicePack();

var getMajorAndMinorVersion = function () {
arguments.callee.setAlias('getMajorAndMinorVersion');

var match = /(\d+)\.(\d+)/.exec(version);
return {
major: parseInt(match[1]),
minor: parseInt(match[2])
};
};

this.isSQL2014 = function () {
arguments.callee.setAlias('isSQL2014');

var ver = getMajorAndMinorVersion();
return ver.major === 12 &amp;&amp; (ver.minor &gt;= 0);
};
}

function ServicePackVersionDetector() {
arguments.callee.setAlias('SQL2014Helper');
this.logger = new Logger();
this.MAX_SP_LEVEL = 9;
this.PARAMETER_CHECK_FAILED_EVENT_ID = 4201;

this.NeedSQLServerUpgrade = function (computerName, serviceName, minimalServicePackLevelSQL2014) {
arguments.callee.setAlias('needSQLServerUpgrade');

var sqlHelper = new SQL2014Helper(computerName, serviceName);

if (sqlHelper.isSQL2014()) {
return sqlHelper.servicePack &lt; minimalServicePackLevelSQL2014;
} else {
throw new Exception("Unknown SQL Server version. Expected SQL Server 2014.");
}
};

this.MainFunc = function (computerName, serviceName, minimalServicePackLevelSQL2014) {
arguments.callee.setAlias('main');

var opsMgrAPI = new OpsMgrAPI();

var propertyBag = opsMgrAPI.ScriptAPI.CreatePropertyBag();

var needUpgrade = this.NeedSQLServerUpgrade(computerName, serviceName, minimalServicePackLevelSQL2014) ? "1" : "0";
propertyBag.AddValue("NeedUpgrade", needUpgrade);

opsMgrAPI.ScriptAPI.Return(propertyBag);
};

this.returnStub = function () {
arguments.callee.setAlias('returnStub');

var opsMgrAPI = new OpsMgrAPI();
var propertyBag = opsMgrAPI.ScriptAPI.CreatePropertyBag();
propertyBag.AddValue("NeedUpgrade", "");
opsMgrAPI.ScriptAPI.Return(propertyBag);
};

this.Launch = function (args) {
if (args.Length !== 3) {
WScript.Quit();
}
var serviceName = args(0);
var computerName = args(1);
var minimalServicePackLevelSQL2014 = parseInt(args(2));

if (minimalServicePackLevelSQL2014 &lt; 0 || minimalServicePackLevelSQL2014 &gt; this.MAX_SP_LEVEL) {
this.logger.LogError(this.PARAMETER_CHECK_FAILED_EVENT_ID, "GoodValue parameter must be non-negative and less than " + (this.MAX_SP_LEVEL + 1).toString());
WScript.Quit();
}

try {
this.MainFunc(computerName, serviceName, minimalServicePackLevelSQL2014);
} catch (e) {
this.logger.LogError(4201, e.toString());
this.returnStub();
}
};
}

new ServicePackVersionDetector().Launch(WScript.Arguments);</Script></Contents>
<Unicode>1</Unicode>
</File>
</Files>
</DataSource>
<ConditionDetection TypeID="System!System.ExpressionFilter" ID="GoodFilter">
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="Integer">Property[@Name='NeedUpgrade']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="Integer">0</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</ConditionDetection>
<ConditionDetection TypeID="System!System.ExpressionFilter" ID="BadFilter">
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="Integer">Property[@Name='NeedUpgrade']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="Integer">1</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</ConditionDetection>
</MemberModules>
<RegularDetections>
<RegularDetection MonitorTypeStateID="ValueGood">
<Node ID="GoodFilter">
<Node ID="DataSource"/>
</Node>
</RegularDetection>
<RegularDetection MonitorTypeStateID="ValueBad">
<Node ID="BadFilter">
<Node ID="DataSource"/>
</Node>
</RegularDetection>
</RegularDetections>
</MonitorImplementation>
</UnitMonitorType>