function ConcatinateServerName(server, tcpPort) {
var dataSource = server;
if ((tcpPort != "") && (tcpPort != "0")) {
dataSource = dataSource + "," + tcpPort;
}
return dataSource;
}
function BuildServerName(strServer) {
var tcp = "";
var ip = "";
var pathArray = strServer.split("\\");
var instanceName = "MSSQLSERVER";
if (pathArray.length > 1) {
instanceName = pathArray[1];
}
var serverName = strServer;
var oWMI = new WMIProvider("root\\Microsoft\\SqlServer\\" + SQL_WMI_NAMESPACE);
var oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + EscapeWQLString(instanceName) + "' AND PropertyName = 'ListenOnAllIPs'");
if (oQuery.Count > 0) {
var e = new Enumerator(oQuery);
e.moveFirst();
var isListenAll = e.item();
if (isListenAll.PropertyNumVal == 1) {
oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + EscapeWQLString(instanceName) + "' AND IPAddressName = 'IPAll' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal != ''");
if (oQuery.Count > 0) {
e = new Enumerator(oQuery);
e.moveFirst();
tcp = e.item().PropertyStrVal;
if ((tcp != "0") && (tcp != "")) {
serverName = serverName + "," + tcp;
}
}
}
else {
oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + EscapeWQLString(instanceName) + "' AND IPAddressName != '' AND PropertyName = 'Enabled' AND PropertyNumVal = 1");
if (oQuery.Count > 0) {
var ipAddressName = oQuery.ItemIndex(0).IPAddressName;
oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + EscapeWQLString(instanceName) + "' AND IPAddressName = '" + EscapeWQLString(ipAddressName) + "' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal != ''");
if (oQuery.Count > 0) {
var e = new Enumerator(oQuery);
e.moveFirst();
tcp = e.item().PropertyStrVal;
}
oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '" + EscapeWQLString(instanceName) + "' AND IPAddressName = '" + EscapeWQLString(ipAddressName) + "' AND PropertyName = 'IpAddress' AND PropertyStrVal != ''");
if (oQuery.Count > 0) {
var e = new Enumerator(oQuery);
e.moveFirst();
ip = e.item().PropertyStrVal;
}
if (ip != "") {
serverName = ip;
}
if (tcp != "") {
serverName = serverName + "," + tcp;
}
}
}
}
return serverName;
}
Function.prototype.setAlias = function (name) {
this.getAlias = function () {
return name;
}
}
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 & 0xFFFF);
parts.push('\n');
parts.push("Win32 Facility : ");
parts.push((this.number >> 16 & 0x1FFF));
parts.push('\n');
}
if (this.description) {
parts.push("Error Description : ");
parts.push(this.description);
}
parts.push('\n');
return parts.join('');
}
else {
this.toString();
}
}
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 < caller.length; i++) {
if (typeof (caller[i]) != 'undefined') {
parts.push(caller[i] != null ? caller[i].toString() : 'null');
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);
}
this.toString = function () {
arguments.callee.setAlias('Exception.toStringFull');
var parts = [];
parts.push(this.message);
parts.push('\n');
if (this.innerException) {
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 = false;
try {
value = 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;
}
}
//'#Include File:GetSQL2005DBWinServState.js
var IsServiceUnavailable = "ServiceUnavailable";
function GetServiceUnavailableRegistryKey(ServiceName) {
arguments.callee.setAlias('GetRegistryKeyForService');
key = "";
var opsMgrAPI = new OpsMgrAPI();
var registryKey = opsMgrAPI.ScriptAPI.GetScriptStateKeyPath(ManagementGroupID);
key = "HKEY_LOCAL_MACHINE\\" + registryKey + "\\" + ServiceName + "\\";
return key;
}
function ReturnServiceStateToOpsMgr(serviceState) {
var opsMgrAPI = new OpsMgrAPI();
logger.LogDebug(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();
}
function IsServiceRunningState(ServiceName) {
arguments.callee.setAlias('IsServiceRunningState');
var regKey = GetServiceUnavailableRegistryKey(ServiceName) + IsServiceUnavailable;
var registry = new Registry();
if (registry.IsKeyExists(regKey))
return false;
return true;
}
function Main(ServiceName) {
arguments.callee.setAlias('Main');
try {
if (!IsServiceRunningState(ServiceName)) {
logger.LogDebug(EVENT_ID, "ServiceName = " + ServiceName + "; Service is not running...");
ReturnServiceStateToOpsMgr("NotRunning")
}
else {
logger.LogDebug(EVENT_ID, "ServiceName = " + ServiceName + "; Service is running...");
ReturnServiceStateToOpsMgr("Running")
}
}
catch (e) {
throw new Exception("Script '" + WScript.ScriptName + "' failed.", e);
}
}
var EVENT_ID = 4000;
var logger = new Logger();
logger.LogDebug(EVENT_ID, "Start...")
if (WScript.Arguments.Count() != 1)
WScript.Quit();