Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 104 additions & 18 deletions src/ScriptSqlConfig/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------------------------------------------------
Expand All @@ -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


Expand All @@ -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 */
Expand All @@ -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)
{
Expand Down Expand Up @@ -244,6 +245,7 @@ private static void ScriptInstance(string server, string directory)
so.IncludeDatabaseRoleMemberships = true;
so.AgentNotify = true;
so.AgentAlertJob = true;



if (VERBOSE)
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/ScriptSqlConfig/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
25 changes: 17 additions & 8 deletions src/ScriptSqlConfig/ScriptSqlConfig.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,24 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.SqlServer.ConnectionInfo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" />
<Reference Include="Microsoft.SqlServer.ConnectionInfo.11.0.0.0.Fakes">
<HintPath>FakesAssemblies\Microsoft.SqlServer.ConnectionInfo.11.0.0.0.Fakes.dll</HintPath>
<Reference Include="Microsoft.SqlServer.ConnectionInfo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Microsoft.SqlServer.ConnectionInfoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Microsoft.SqlServer.SqlEnum, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Microsoft.SqlServer.ConnectionInfoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" />
<Reference Include="Microsoft.SqlServer.Management.Sdk.Sfc, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" />
<Reference Include="Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" />
<Reference Include="Microsoft.SqlServer.SmoExtended, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" />
<Reference Include="Microsoft.SqlServer.SqlEnum, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down