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:DiscoverSQL2008FileGroupsAndFiles.js
//' Parameters that should be passed to this script
//' 0 MPElement ID ($MPElement$)
//' 1 Target Id for ME this rule is running against ($Target/Id$)
//' 2 Computer (FQDN) that SQL instance is hosted on
//' 3 SQL Connection String for the instance that the DBs are being discovered on
//' 4 SQL Instance that this rule is being run for
function EscapeWQLString(wqlString) {
return wqlString.replace(/'/g, "\\'");
}
function IsRunningInstanceSQLServer(sqlInstanceName) {
arguments.callee.setAlias('CheckSQLServerState');
var isRunning = false;
try {
var wmiProvider = new WMIProvider("root\\Microsoft\\SqlServer\\ComputerManagement10");
var sqlService = wmiProvider.ExecQuery("select state from SqlService WHERE (ServiceName = '" + EscapeWQLString(sqlInstanceName) + "' OR ServiceName = 'MSSQL$" + EscapeWQLString(sqlInstanceName) + "') and SQLServiceType ='1'");
if (sqlService.Count > 0) {
var e = new Enumerator(sqlService);
e.moveFirst();
var sqlServiceObject = e.item();
function GetDatabaseExcludeList(InstanceName) {
arguments.callee.setAlias('GetDatabaseExcludeList');
var databaseExcludeList = "";
try {
var opsMgrAPI = new OpsMgrAPI();
var registryKey = opsMgrAPI.ScriptAPI.GetScriptStateKeyPath(ManagementGroupID);
logger.LogDebug(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 < excludeDatabases.length; i++) {
excludeDatabases[i] = excludeDatabases[i].replace(/(^\s+)|(\s+$)/g, "").split("'").join("''");
}
databaseExcludeList = "'" + excludeDatabases.join("','") + "'";
}
}
}
catch (e) {
throw new Exception("Can't read database exclude list from registry.", e);
}
return databaseExcludeList;
}
function Main(SourceID, ManagedEntityId, TargetComputer, serverName, instanceName, tcpPort) {
arguments.callee.setAlias('Main');
var dbMasterConnection;
var opsMgrAPI = new OpsMgrAPI();
var oSQLDiscoveryData = null;
try {
oSQLDiscoveryData = opsMgrAPI.ScriptAPI.CreateDiscoveryData(0, SourceID, ManagedEntityId);
}
catch (e) {
throw new Exception("Can't create discovery data.", e);
}
try {
var connectionString = BuildConnectionStringWithPort(serverName, "master", tcpPort);
try {
dbMasterConnection = new ADODB(connectionString, "sqloledb", 10);
} catch (e) {
connectionString = BuildConnectionString(serverName, "master");
dbMasterConnection = new ADODB(connectionString, "sqloledb", 30);
}
var databaseExcludeList = GetDatabaseExcludeList(instanceName);
if (databaseExcludeList == '*') {
opsMgrAPI.ScriptAPI.Return(oSQLDiscoveryData);
return;
}
var conditionDatabaseExcludeList = '';
if (databaseExcludeList != '') {
conditionDatabaseExcludeList = " AND name NOT IN (" + databaseExcludeList + ") ";
}
var dbSet = dbMasterConnection.ExecuteQuery("select database_id, name from sys.databases WHERE state = 0 and source_database_id is null and collation_name is not null " + conditionDatabaseExcludeList);
var ii = 0;
for (dbSet.MoveFirst() ; !dbSet.EOF; dbSet.MoveNext()) {
var databaseName = dbSet("name").Value;
var databaseID = dbSet("database_id").Value;
try {
var dbFileSet = dbMasterConnection.ExecuteQuery(" SELECT f.data_space_id as fileGroupId, " +
" f.file_id as fileId, " +
" f.name as fileName, " +
" f.physical_name as filePath, " +
" f.type as fileType " +
" FROM sys.master_files f " +
" WHERE f.type < 2 " +
" AND f.database_id = " + databaseID);
for (dbFileSet.MoveFirst() ; !dbFileSet.EOF; dbFileSet.MoveNext()) {
var oFileInstance = null;
var EVENT_ID = 4001;
var logger = new Logger();
logger.LogDebug(EVENT_ID, "Start...")
if (WScript.Arguments.Count() != 6)
WScript.Quit();
var SourceID = WScript.Arguments(0);
var ManagedEntityId = WScript.Arguments(1);
var TargetComputer = WScript.Arguments(2);
var ServerName = WScript.Arguments(3);
var InstanceName = WScript.Arguments(4);
var TcpPort = WScript.Arguments(5);