diff --git a/src/ScriptSqlConfig/Program.cs b/src/ScriptSqlConfig/Program.cs index 3406666..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 ---------------------------------------------------------------- @@ -175,7 +176,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 @@ -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 */ @@ -205,10 +206,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) { @@ -244,6 +245,7 @@ private static void ScriptInstance(string server, string directory) so.IncludeDatabaseRoleMemberships = true; so.AgentNotify = true; so.AgentAlertJob = true; + if (VERBOSE) @@ -276,6 +278,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 +1437,90 @@ private static void ScriptDatabaseRoles(Database db, string directory, Scripting } - private static void RemoveSqlFiles(string directory) + + private static void ScriptResourceGovernor(Server smoServer, string directory, ScriptingOptions so) + { + if (VERBOSE) + WriteMessage("Scripting 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.Add("ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = NULL);"); + sc.Add("ALTER RESOURCE GOVERNOR DISABLE;"); + StringCollection udfscr = udf.Script(so); + sc.Append(udfscr); + break; + } + + } + } + // script Resource Governor configurations + StringCollection rgscr = rg.Script(); + sc.Append(rgscr); + + + // script Resource Pools + foreach (ResourcePool pool in rg.ResourcePools) + { + StringCollection poolscr = pool.Script(); + sc.Append(poolscr); + + + // script Workload Groups + foreach (WorkloadGroup wg in pool.WorkloadGroups) + { + StringCollection wgscr = wg.Script(); + sc.Append(wgscr); + + } + } + + 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); + + } + + + private static void RemoveSqlFiles(string directory) { throw new NotImplementedException("RemoveSqlFiles shouldn't be called."); //DirectoryInfo dir = new DirectoryInfo(directory); 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")] 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 - - - - -