diff --git a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs index 48f1bd6..2c1c537 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs @@ -281,6 +281,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.CommandLineParserNS public static class RestoreDataBase { public const string KEY = "RES"; public const string CMD_RESTOREFIRST = "-RESTOREFIRST"; } public static class RelocatePhysicalFiles { public const string KEY = "COP"; } + public static class ShrinkDB { public const string KEY = "SHR"; } } } diff --git a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools.cs b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools.cs index 36fcf62..28ad7e0 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools.cs @@ -210,27 +210,46 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS Process.Start(startinfo); } public enum ShutDownMode { Sleep, Hibernation,Lock,Logoff,Restart,ShutDown,} - public static string HKLM_GetString(string path, string key) + private static string HKLM_GetString(string path, string key) { try { - RegistryKey rk = Registry.LocalMachine.OpenSubKey(path); - if (rk == null) { return ""; } + Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path); + if (rk == null) return ""; return (string)rk.GetValue(key); } - catch { return ""; } + catch { return null; } } - public static string OSFriendlyName() + public static string GetOSFriendlyNameA() { string ProductName = HKLM_GetString(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName"); string CSDVersion = HKLM_GetString(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CSDVersion"); - if (ProductName != "") + if (!string.IsNullOrWhiteSpace(ProductName)) { - return (ProductName.StartsWith("Microsoft") ? "" : "Microsoft ") + ProductName + - (CSDVersion != "" ? " " + CSDVersion : ""); + return (ProductName.StartsWith("Microsoft") ? "" : "Microsoft ") + ProductName + (CSDVersion != "" ? " " + CSDVersion : ""); } - return ""; + return null; + } + + public static string GetOSFriendlyNameB() + { + string result = string.Empty; + ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem"); + foreach (ManagementObject os in searcher.Get()) { result = os["Caption"].ToString(); break; } + return string.IsNullOrWhiteSpace(result)? null : result; + } + public static string GetOSFriendlyNameC() + { + return new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName; + } + public static string GetOSType() + { + string os = null; + IEnumerable list64 = System.IO.Directory.GetDirectories(Environment.GetEnvironmentVariable("SystemRoot")).Where(s => s.Equals(@"C:\Windows\SysWOW64")); + IEnumerable list32 = System.IO.Directory.GetDirectories(Environment.GetEnvironmentVariable("SystemRoot")).Where(s => s.Equals(@"C:\Windows\System32")); + if (list32.Count() > 0) { if (list64.Count() > 0) { os = "64bit"; } else { os = "32bit"; } } + return os; } } diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - InstallManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - InstallManager.cs index d669021..629347b 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - InstallManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - InstallManager.cs @@ -111,12 +111,12 @@ namespace Vrh.Log4Pro.MaintenanceConsole.InstallManagerNS } public static InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values? GetOsCode() { - string osfrn = Tools.OSFriendlyName(); + string osfrn = Tools.GetOSFriendlyNameB(); var defaultvalue = osfrn.StartsWith("Microsoft Windows 10") ? InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values.WS2016.ToString() : osfrn.StartsWith("Microsoft Windows Server 2016") ? InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values.WS2016.ToString() : osfrn.StartsWith("Microsoft Windows Server 2012") ? InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values.WS2016.ToString() - : null; + : ""; var oslist = Enum.GetNames(typeof(InstallManagerXmlParser.XmlStructure.EnvironmentInstall.WindowsFeatureList.Attributes.OS.Values)).ToList(); string answer = ColorConsole.ReadLine($"Enter os code (friendly name is: '{osfrn}')!", ConsoleColor.Green, validitylist: oslist, defaultvalue: defaultvalue.ToString(), required: true); if (answer.ToLower() == "ex") { return null; } diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs index 0097e58..5302112 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - SQLDataBaseManager.cs @@ -48,6 +48,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.CreateDataScripts.KEY, "Create data scripts", CreateDataScripts, ep)) .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.RestoreDataBase.KEY, "Restore database backup", RestoreDataBase, ep)) .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.RelocatePhysicalFiles.KEY, "Copy database and or relocate its physical files", RelocatePhysicalFiles, ep)) + .AddMenuItem(new Menu.Item(CLP.Module.SQLDataBaseManager.Function.ShrinkDB.KEY, "Shrink database", ShrinkDB, ep)) .SetSelectionMode(Menu.SelectionMode.Single) .SetMenuHeaderDisplayer(DataBaseListDisplayer); menufunctions.ExecuteMenu(functionkey); @@ -214,6 +215,49 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS } return o; } + private static object ShrinkDB(object parameter, object o) + { + var config = (parameter as Menu.ExecutorParameter).GetConfig(); + var args = (parameter as Menu.ExecutorParameter).Args; + + var selectedtaskindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.ScheduledTaskManager.Function.CMD_TASKS); + + var menufolders = DisplaySQLDataBaseMenu(config, $"Select the SQL database(s) to manage with function '{nameof(ShrinkDB)}'!", silent: true); + + Menu.Selection sr = menufolders.Select(selectedtaskindexes); + if (sr.Result == Menu.SelectionResult.Exit) { return o; } + else if (sr.Result == Menu.SelectionResult.None) { return o; } + else if (sr.Result == Menu.SelectionResult.Error) { return o; } + else if (sr.Result == Menu.SelectionResult.Ok) { } + else { } + foreach (var p in sr.SelectedParameterList) + { + SQLDataBase sqld = p.Parameters as SQLDataBase; + try + { + ColorConsole.WriteLine(prefix: $"Enter the shrink options. Format:", bracket:"()", text:"OPTION,FreeSpacePercent:", f:ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix:" ", text:"Default", bracket: "[]", suffix: $": (default) compacts the data and removes free space from the DB. Use togethere with FreeSpacePercent.", f:ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix: " ", text: "NoTruncate",bracket:"[]", suffix: $": compacts the data, but does not remove free space from the DB. FreeSpacePercent is not applicable.", f: ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix: " ", text: "TruncateOnly", bracket: "[]", suffix: $": does not compact the data, but removes free space from the DB. Use togethere with FreeSpacePercent.", f: ConsoleColor.Yellow); + ColorConsole.WriteLine(prefix: " ", text: "FreeSpacePercent", bracket: "[]", suffix: $": an integer number between 0-100,default=10; indicates the percentage of free space in the shrinked DB.", f: ConsoleColor.Yellow); + var shrinkoptions = ColorConsole.ReadLine($"EX=exit.", ConsoleColor.Yellow, suffix: " --> "); + if (shrinkoptions == "EX") { continue; } + else if (string.IsNullOrWhiteSpace(shrinkoptions)) { shrinkoptions = "Default,10"; } + string option, fsp = null; + int fspint = 10; + option = shrinkoptions.Split(new char[] { ',' })[0]; + if (shrinkoptions.IndexOf(',') >= 0) { fsp = shrinkoptions.Split(new char[] { ',' })[1]; } + if (string.IsNullOrWhiteSpace(option) || !"Default,NoTruncate,TruncateOnly".Contains(option)) { option = "Default"; } + var shrinkmethod = (ShrinkMethod)Enum.Parse(typeof(ShrinkMethod), option); + if (string.IsNullOrWhiteSpace(fsp) || !int.TryParse(fsp, out fspint)) { fspint = 10; } + + SQLDataBaseManagerCore.ShrinkDB(sqld.SQLCS, shrinkmethod, fspint); + ColorConsole.WriteLine($"Database shrinked. Name:{sqld.DBName}", ConsoleColor.Green); + } + catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); } + } + return o; + } private static object RelocatePhysicalFiles(object parameter, object o) { var config = (parameter as Menu.ExecutorParameter).GetConfig(); @@ -312,6 +356,13 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS ColorConsole.WriteLine(" "); return " "; } + else if (lineix == 4) + { + var dbsize = SQLDataBaseManagerCore.GetSize(st.SQLCS); + ColorConsole.Write(dbsize, ConsoleColor.Yellow, prefix: "DB size:", suffix: " "); + ColorConsole.WriteLine(" "); + return " "; + } return null; } #endregion private method: DisplayDataBaseInfo @@ -329,6 +380,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS sqld.PhysicalFilesDirectoryPath = GetPhysicalFilesLocation(sqld.SQLCS); sqld.DBName = GetDBName(sqld.SQLCS); sqld.DataSource = GetDataSource(sqld.SQLCS); + sqld.SizeString = GetSize(sqld.SQLCS); return sqld; } #endregion public CollectDataBaseInfo @@ -539,6 +591,47 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS } catch (Exception ex) { throw ex; } } + + public static void ShrinkDB(string sqlconnectionstring, ShrinkMethod shrinkmethod,int freespacepercent) + { + var sqlc = new Microsoft.Data.SqlClient.SqlConnection(sqlconnectionstring); + var sc = new ServerConnection(sqlc); + var srvr = new Server(sc); + var db = new Database(srvr, GetDBName(sqlconnectionstring)); + db.Shrink(freespacepercent, shrinkmethod); + } + + #region GetSize + /// + /// Visszadja a megadott adatbázis méretét + /// + /// + /// + public static string GetSize(string sqlconnectionstring) + { + var sqlc = new Microsoft.Data.SqlClient.SqlConnection(sqlconnectionstring); + var dbname = GetDBName(sqlconnectionstring); + + var getsizecmd = new Microsoft.Data.SqlClient.SqlCommand ("sp_spaceused", sqlc); + getsizecmd.CommandType = System.Data.CommandType.StoredProcedure; + sqlc.Open(); + var reader = getsizecmd.ExecuteReader(); + + if (reader.HasRows) + { + while (reader.Read()) + { + var dbn = reader["database_name"]; + var dbs = reader["database_size"]; + if (Convert.ToString(dbn) == dbname) { return Convert.ToString(dbs); }; + } + } + sqlc.Close(); + sqlc.Dispose(); + return "N/A"; + } + #endregion GetSize + #region GetPhysicalFilesLocation /// /// Returns the physical path to the directory that holds the files of the database @@ -564,7 +657,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS #region DBSubstitution public enum DBSubstitutionName { - DATABASE,DATASOURCE,DBOTYPE,DBONAME,DBDATAGROUP,BACKUPTS, + DATABASE,DATASOURCE,DBOTYPE,DBONAME,DBDATAGROUP,BACKUPTS,SHRINKOPTION,SHRINKFREESPACEPERCENT, } #endregion DBSubstitution #region ExecuteSQLScript @@ -615,6 +708,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS SQLBatchTxt += scriptline + "\r\n"; } } + sqlc.Close(); if (DataSet != null && DataSet.Tables != null) { @@ -628,6 +722,12 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS } else { return new ReturnInfoJSON() { ReturnValue = 0, ReturnMessage = null, }; } } + + + + + + private static System.Data.DataSet ExecuteSQLScriptBatch(Microsoft.Data.SqlClient.SqlConnection sqlc, string sqlbatchtxt, int commandtimeout) { var sqlcommand = sqlc.CreateCommand(); @@ -1005,6 +1105,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.SQLDataBaseManagerNS public string DataSource; public SQLDataBaseManagerCore.SQLDBStatus Status; public string PhysicalFilesDirectoryPath; + public string SizeString; public string SQLCS { get { return XmlProcessing.ConnectionStringStore.GetSQL(this.Xml_SQLConnectionString); } diff --git a/Vrh.Log4Pro.MaintenanceConsole/Program.cs b/Vrh.Log4Pro.MaintenanceConsole/Program.cs index 659f0c8..1553f62 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Program.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Program.cs @@ -35,7 +35,6 @@ namespace Vrh.Log4Pro.MaintenanceConsole { static void Main(string[] args) { - var forcedmodulekey = CommandLine.GetCommandLineArgument(args, CLP.CMD_MODULE); var commandmode = !string.IsNullOrEmpty(forcedmodulekey); var silentmode = commandmode && !string.IsNullOrEmpty(CommandLine.GetCommandLineArgument(args, CLP.CMD_SILENT, switchtype: true)); @@ -113,7 +112,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole // processor architecture ColorConsole.Write("Processors: "); - ColorConsole.Write(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"), ConsoleColor.Yellow, bracket: "[]", prefix: "Architecture: ",suffix:", "); + ColorConsole.Write(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"), ConsoleColor.Yellow, bracket: "[]", prefix: "Architecture: "); + ColorConsole.Write(Tools.GetOSType() ?? "N/A", ConsoleColor.Yellow, bracket: "[]", suffix: ", "); foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get()) { ColorConsole.Write(item["NumberOfProcessors"].ToString(), ConsoleColor.Yellow, bracket: "[]", prefix: "Physical:",suffix:", "); @@ -134,7 +134,13 @@ namespace Vrh.Log4Pro.MaintenanceConsole ColorConsole.Write(System.Environment.OSVersion.ServicePack, ConsoleColor.Yellow, bracket: "[]", prefix: "Service pack:", suffix: ","); ColorConsole.Write(System.Environment.OSVersion.VersionString, ConsoleColor.Yellow, bracket: "[]", prefix: "Version string:"); ColorConsole.WriteLine(""); - ColorConsole.Write(Tools.OSFriendlyName(), ConsoleColor.Yellow, bracket: "[]", prefix: " Friendly name:"); + + ColorConsole.Write("", ConsoleColor.Yellow, prefix: "OS Friendly name versions:"); + string fnA = Tools.GetOSFriendlyNameA(); if (!string.IsNullOrWhiteSpace(fnA)) ColorConsole.Write(fnA, ConsoleColor.Yellow, bracket: "[]"); + string fnB = Tools.GetOSFriendlyNameB(); if (!string.IsNullOrWhiteSpace(fnB) && fnB!=fnA) ColorConsole.Write(fnB, ConsoleColor.Yellow, bracket: "[]"); + string fnC = Tools.GetOSFriendlyNameC(); if (!string.IsNullOrWhiteSpace(fnC) && fnC!=fnA && fnC!=fnB) ColorConsole.Write(fnC, ConsoleColor.Yellow, bracket: "[]"); + if (string.IsNullOrWhiteSpace(fnA) && string.IsNullOrWhiteSpace(fnB) && string.IsNullOrWhiteSpace(fnC)) ColorConsole.Write("N/A", ConsoleColor.Yellow, bracket: "[]"); + ColorConsole.WriteLine(""); ColorConsole.WriteLine(""); ColorConsole.WriteLine(""); } diff --git a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs index 2cc3c83..3c8c6eb 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // 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("1.3.0.0")] -[assembly: AssemblyFileVersion("1.3.0.0")] +[assembly: AssemblyVersion("1.4.0.0")] +[assembly: AssemblyFileVersion("1.4.0.0")] diff --git a/Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj b/Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj index b7d3e26..9e8bada 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj +++ b/Vrh.Log4Pro.MaintenanceConsole/Vrh.Log4Pro.MaintenanceConsole.csproj @@ -157,6 +157,7 @@ ..\packages\Microsoft.SqlServer.SqlManagementObjects.161.46041.41\lib\net462\Microsoft.SqlServer.WmiEnum.dll + ..\packages\Microsoft.Web.Administration.11.1.0\lib\netstandard1.5\Microsoft.Web.Administration.dll -- libgit2 0.21.2