From 316a6294fd566fd534ed0b71b3fb5380d01444dd Mon Sep 17 00:00:00 2001 From: Gianluca Sartori Date: Tue, 4 Jul 2017 18:15:29 +0200 Subject: [PATCH 1/3] Added code to script Resource Governor --- src/ScriptSqlConfig/Program.cs | 78 ++++++++++++++++++++-- src/ScriptSqlConfig/ScriptSqlConfig.csproj | 25 ++++--- 2 files changed, 90 insertions(+), 13 deletions(-) diff --git a/src/ScriptSqlConfig/Program.cs b/src/ScriptSqlConfig/Program.cs index 3406666..8d3c57f 100644 --- a/src/ScriptSqlConfig/Program.cs +++ b/src/ScriptSqlConfig/Program.cs @@ -175,7 +175,7 @@ 2. It will use trusted authentication unless both the /username #if DEBUG Console.WriteLine(""); Console.WriteLine("Press any key to continue...."); - Console.ReadLine(); + Console.ReadKey(false); #endif @@ -205,10 +205,10 @@ 2. It will use trusted authentication unless both the /username #if DEBUG Console.WriteLine("Press any key to continue...."); - Console.ReadLine(); + Console.ReadKey(false); #endif - - } + + } private static void SetVersions(string server) { @@ -276,6 +276,7 @@ private static void ScriptInstance(string server, string directory) ScriptEventNotifications(conn, instanceDirectory, so); ScriptOtherObjects(srv, instanceDirectory, so); ScriptDatabaseOptions(srv, instanceDirectory, so); + ScriptResourceGovernor(srv, instanceDirectory, so); WriteMessage("Scripting User Objects and Security in System Databases..."); ScriptDatabase(srv.Name.ToString(), "master", Path.Combine(instanceDirectory, @"Databases\master")); @@ -1434,7 +1435,74 @@ private static void ScriptDatabaseRoles(Database db, string directory, Scripting } - private static void RemoveSqlFiles(string directory) + + private static void ScriptResourceGovernor(Server smoServer, string directory, ScriptingOptions options) + { + if (VERBOSE) + WriteMessage("Resource Governor..."); + + StringCollection sc = new StringCollection(); + + if (smoServer.Edition.Contains("Enterprise") || smoServer.Edition.Contains("Developer")) + { + ResourceGovernor rg = smoServer.ResourceGovernor; + if (rg != null) + { + sc.Add("USE master;"); + string classifierFunction = rg.ClassifierFunction; + + if (!String.IsNullOrEmpty(classifierFunction)) + { + int objId = -1; + using (SqlConnection conn = GetConnection(smoServer.Name, "master")) + { + string sql = "SELECT OBJECT_ID(@name)"; + SqlCommand cmd = new SqlCommand(sql); + cmd.Connection = conn; + cmd.Connection.Open(); + cmd.Parameters.Add(new SqlParameter("@name", System.Data.SqlDbType.NVarChar, 200)); + cmd.Parameters[0].Value = classifierFunction; + objId = (int)(cmd.ExecuteScalar()); + } + + + + foreach (UserDefinedFunction udf in smoServer.Databases["master"].UserDefinedFunctions) + { + if (udf.ID == objId) + { + //if possible, script classifier function + sc.Append(udf.Script(options)); + sc.Add("ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = NULL);"); + sc.Add("ALTER RESOURCE GOVERNOR DISABLE;"); + break; + } + + } + + // script Resource Governor configurations + sc.Append(rg.Script(options)); + + // script Resource Pools + foreach (ResourcePool pool in rg.ResourcePools) + { + sc.Append(pool.Script(options)); + + // script Workload Groups + foreach (WorkloadGroup wg in pool.WorkloadGroups) + { + sc.Append(wg.Script(options)); + } + } + + WriteFile(sc, Path.Combine(directory, "ResourceGovernor.sql"), true); + } + } + } + } + + + private static void RemoveSqlFiles(string directory) { throw new NotImplementedException("RemoveSqlFiles shouldn't be called."); //DirectoryInfo dir = new DirectoryInfo(directory); diff --git a/src/ScriptSqlConfig/ScriptSqlConfig.csproj b/src/ScriptSqlConfig/ScriptSqlConfig.csproj index e2e6ba0..61714c5 100644 --- a/src/ScriptSqlConfig/ScriptSqlConfig.csproj +++ b/src/ScriptSqlConfig/ScriptSqlConfig.csproj @@ -35,15 +35,24 @@ 4 - - - FakesAssemblies\Microsoft.SqlServer.ConnectionInfo.11.0.0.0.Fakes.dll + + False + + + False + + + False + + + False + + + False + + + False - - - - - From e45cab35387e390ea69209f9afd561ea7c5212b8 Mon Sep 17 00:00:00 2001 From: Gianluca Sartori Date: Wed, 5 Jul 2017 10:59:31 +0200 Subject: [PATCH 2/3] Fixed small bug in script. Added correct version of SMO to config --- src/ScriptSqlConfig/Program.cs | 2 +- src/ScriptSqlConfig/Properties/AssemblyInfo.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ScriptSqlConfig/Program.cs b/src/ScriptSqlConfig/Program.cs index 8d3c57f..586d2be 100644 --- a/src/ScriptSqlConfig/Program.cs +++ b/src/ScriptSqlConfig/Program.cs @@ -1472,9 +1472,9 @@ private static void ScriptResourceGovernor(Server smoServer, string directory, S if (udf.ID == objId) { //if possible, script classifier function - sc.Append(udf.Script(options)); sc.Add("ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = NULL);"); sc.Add("ALTER RESOURCE GOVERNOR DISABLE;"); + sc.Append(udf.Script(options)); break; } diff --git a/src/ScriptSqlConfig/Properties/AssemblyInfo.cs b/src/ScriptSqlConfig/Properties/AssemblyInfo.cs index 4123544..97404c2 100644 --- a/src/ScriptSqlConfig/Properties/AssemblyInfo.cs +++ b/src/ScriptSqlConfig/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2012.5")] -[assembly: AssemblyFileVersion("2012.5")] +[assembly: AssemblyVersion("2016.1")] +[assembly: AssemblyFileVersion("2016.1")] From 783958f0e1074a0d7831c98f4b10f5fb51b58ca4 Mon Sep 17 00:00:00 2001 From: Romain Ferraton Date: Fri, 21 Sep 2018 11:49:27 +0200 Subject: [PATCH 3/3] Changes for Ressource Governor reverse script to work --- src/ScriptSqlConfig/Program.cs | 64 ++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/src/ScriptSqlConfig/Program.cs b/src/ScriptSqlConfig/Program.cs index 586d2be..42cef07 100644 --- a/src/ScriptSqlConfig/Program.cs +++ b/src/ScriptSqlConfig/Program.cs @@ -148,22 +148,23 @@ This application generates scripts and configuration information Optional Parameters: ---------------------------------------------------------------- - /v (Verbose Output) - /databases (Script databases) - /noinstance (Don't script instance information) - /user (SQL Server user name. It will use trusted - security unless this option is specified.) - /password (SQL Server password. If /user is specified then - /password is required.) - /scriptdb (Database to script. This will script a single - database in addition to the instance scripts.) - /? (Display this help) - /testsmo (Test loading the SMO libraries) + /v (Verbose Output) + /databases (Script databases) + /noinstance (Don't script instance information) + /user (SQL Server user name. It will use trusted + security unless this option is specified.) + /password (SQL Server password. If /user is specified then + /password is required.) + /scriptdb (Database to script. This will script a single + database in addition to the instance scripts.) + /? (Display this help) + /testsmo (Test loading the SMO libraries) + /hashedpasswords (generate sql logins with HASHED password, else will use ___password___ as password) Sample Usage ---------------------------------------------------------------- - ScriptSqlConfig.EXE /server Srv1\Instance /dir 'C:\MyDir' + ScriptSqlConfig.EXE /server Srv1\Instance /dir 'C:\MyDir' /hashedpasswords Notes ---------------------------------------------------------------- @@ -189,7 +190,7 @@ 2. It will use trusted authentication unless both the /username WriteMessage("Server: " + SERVER); /* Display the SMO version */ - var ver = System.Reflection.Assembly.GetAssembly(typeof(Microsoft.SqlServer.Management.Smo.Server)).GetName().Version; + var ver = Assembly.GetAssembly(typeof(Server)).GetName().Version; WriteMessage("SMO Version: " + ver.ToString()); /* Set the SQL Server version */ @@ -244,6 +245,7 @@ private static void ScriptInstance(string server, string directory) so.IncludeDatabaseRoleMemberships = true; so.AgentNotify = true; so.AgentAlertJob = true; + if (VERBOSE) @@ -1436,13 +1438,13 @@ private static void ScriptDatabaseRoles(Database db, string directory, Scripting } - private static void ScriptResourceGovernor(Server smoServer, string directory, ScriptingOptions options) + private static void ScriptResourceGovernor(Server smoServer, string directory, ScriptingOptions so) { if (VERBOSE) - WriteMessage("Resource Governor..."); + WriteMessage("Scripting Resource Governor..."); StringCollection sc = new StringCollection(); - + if (smoServer.Edition.Contains("Enterprise") || smoServer.Edition.Contains("Developer")) { ResourceGovernor rg = smoServer.ResourceGovernor; @@ -1474,31 +1476,47 @@ private static void ScriptResourceGovernor(Server smoServer, string directory, S //if possible, script classifier function sc.Add("ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = NULL);"); sc.Add("ALTER RESOURCE GOVERNOR DISABLE;"); - sc.Append(udf.Script(options)); + StringCollection udfscr = udf.Script(so); + sc.Append(udfscr); break; } } - + } // script Resource Governor configurations - sc.Append(rg.Script(options)); + StringCollection rgscr = rg.Script(); + sc.Append(rgscr); + // script Resource Pools foreach (ResourcePool pool in rg.ResourcePools) { - sc.Append(pool.Script(options)); + StringCollection poolscr = pool.Script(); + sc.Append(poolscr); + // script Workload Groups foreach (WorkloadGroup wg in pool.WorkloadGroups) { - sc.Append(wg.Script(options)); + StringCollection wgscr = wg.Script(); + sc.Append(wgscr); + } } - WriteFile(sc, Path.Combine(directory, "ResourceGovernor.sql"), true); - } + foreach (ExternalResourcePool expool in rg.ExternalResourcePools) + { + StringCollection expoolscr = expool.Script(so); + sc.Append(expoolscr); + + } + } + } + string fileName = Path.Combine(directory, "RessourceGovernor.sql"); + WriteFile(sc, fileName, true); + }