this.toString = function() {
arguments.callee.setAlias('Exception.toStringFull');
var parts = [];
parts.push(this.message);
parts.push('\n');
if (this.innerException) {
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;
}
}
// ##### Scripts\DiscoverSQL2012Files.js
//#Include File:DiscoverSQL2012FileGroupsAndFiles.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\\ComputerManagement11");
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) {
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 {
dbMasterConnection = new ADODB("Server=" + EscapeConnStringValue(serverName) + ";Database=master;Trusted_Connection=yes", "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 = " + DatabaseState.NORMAL + " OR state = " + DatabaseState.STANDBY + ") and source_database_id is 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() != 5)
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);