|
| 1 | +using System; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.Data; |
| 4 | +using System.Management.Automation; |
| 5 | +using Dataplat.Dbatools.Parameter; |
| 6 | + |
| 7 | +namespace Dataplat.Dbatools.Commands |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Identifies deprecated SQL Server features currently in use with their usage counts from performance counters. |
| 11 | + /// Queries sys.dm_os_performance_counters to find which deprecated features have been used on the instance. |
| 12 | + /// </summary> |
| 13 | + [Cmdlet("Get", "DbaDeprecatedFeature")] |
| 14 | + public class GetDbaDeprecatedFeatureCommand : DbaInstanceCmdlet |
| 15 | + { |
| 16 | + private static readonly ScriptBlock ConnectScript = |
| 17 | + ScriptBlock.Create("param($i) Connect-DbaInstance -SqlInstance $i -MinimumVersion 9"); |
| 18 | + private static readonly ScriptBlock ConnectWithCredScript = |
| 19 | + ScriptBlock.Create("param($i, $c) Connect-DbaInstance -SqlInstance $i -SqlCredential $c -MinimumVersion 9"); |
| 20 | + private static readonly ScriptBlock QueryScript = |
| 21 | + ScriptBlock.Create("param($s, $q) $s.Query($q)"); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The SQL query to retrieve deprecated features with usage counts greater than zero. |
| 25 | + /// </summary> |
| 26 | + internal static readonly string DeprecatedFeatureQuery = |
| 27 | + "SELECT LTRIM(RTRIM(instance_name)) AS DeprecatedFeature, cntr_value AS UsageCount FROM sys.dm_os_performance_counters WHERE object_name LIKE '%SQL%Deprecated Features%' AND cntr_value > 0"; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Processes each SQL Server instance, querying for deprecated feature usage. |
| 31 | + /// </summary> |
| 32 | + protected override void ProcessRecord() |
| 33 | + { |
| 34 | + if (TestFunctionInterrupt()) { return; } |
| 35 | + |
| 36 | + foreach (DbaInstanceParameter instance in SqlInstance) |
| 37 | + { |
| 38 | + object server; |
| 39 | + try |
| 40 | + { |
| 41 | + server = ConnectInstance(instance); |
| 42 | + if (server == null) |
| 43 | + { |
| 44 | + StopFunction( |
| 45 | + String.Format("Failed to connect to {0}", instance), |
| 46 | + target: instance, |
| 47 | + isContinue: true, |
| 48 | + category: ErrorCategory.ConnectionError); |
| 49 | + TestFunctionInterrupt(); |
| 50 | + continue; |
| 51 | + } |
| 52 | + } |
| 53 | + catch (Exception ex) |
| 54 | + { |
| 55 | + StopFunction( |
| 56 | + "Failure", |
| 57 | + errorRecord: new ErrorRecord(ex, "GetDbaDeprecatedFeature_ConnectionError", ErrorCategory.ConnectionError, instance), |
| 58 | + target: instance, |
| 59 | + isContinue: true, |
| 60 | + category: ErrorCategory.ConnectionError); |
| 61 | + TestFunctionInterrupt(); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + try |
| 66 | + { |
| 67 | + string computerName = GetServerProperty(server, "ComputerName"); |
| 68 | + string serviceName = GetServerProperty(server, "ServiceName"); |
| 69 | + string domainInstanceName = GetServerProperty(server, "DomainInstanceName"); |
| 70 | + |
| 71 | + Collection<PSObject> rows = ExecuteQuery(server, DeprecatedFeatureQuery); |
| 72 | + if (rows != null) |
| 73 | + { |
| 74 | + foreach (PSObject row in rows) |
| 75 | + { |
| 76 | + if (row == null) continue; |
| 77 | + |
| 78 | + PSObject output = new PSObject(); |
| 79 | + output.Properties.Add(new PSNoteProperty("ComputerName", computerName)); |
| 80 | + output.Properties.Add(new PSNoteProperty("InstanceName", serviceName)); |
| 81 | + output.Properties.Add(new PSNoteProperty("SqlInstance", domainInstanceName)); |
| 82 | + output.Properties.Add(new PSNoteProperty("DeprecatedFeature", GetRowValue(row, "DeprecatedFeature"))); |
| 83 | + output.Properties.Add(new PSNoteProperty("UsageCount", GetRowValue(row, "UsageCount"))); |
| 84 | + |
| 85 | + WriteObject(output); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + catch (Exception ex) |
| 90 | + { |
| 91 | + StopFunction( |
| 92 | + String.Format("Failed to retrieve deprecated features from {0}", instance), |
| 93 | + exception: ex, |
| 94 | + target: instance, |
| 95 | + isContinue: true); |
| 96 | + TestFunctionInterrupt(); |
| 97 | + continue; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + #region Helpers |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Connects to a SQL Server instance via Connect-DbaInstance with minimum version 9. |
| 106 | + /// </summary> |
| 107 | + private object ConnectInstance(DbaInstanceParameter instance) |
| 108 | + { |
| 109 | + Collection<PSObject> results; |
| 110 | + if (SqlCredential != null) |
| 111 | + { |
| 112 | + results = InvokeCommand.InvokeScript(false, ConnectWithCredScript, null, new object[] { instance, SqlCredential }); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + results = InvokeCommand.InvokeScript(false, ConnectScript, null, new object[] { instance }); |
| 117 | + } |
| 118 | + |
| 119 | + if (results != null && results.Count > 0) |
| 120 | + return results[0].BaseObject; |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Executes a SQL query against the server object using $server.Query(). |
| 126 | + /// </summary> |
| 127 | + private Collection<PSObject> ExecuteQuery(object server, string sql) |
| 128 | + { |
| 129 | + return InvokeCommand.InvokeScript(false, QueryScript, null, new object[] { server, sql }); |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Gets a property value from a server object using PSObject property access. |
| 134 | + /// </summary> |
| 135 | + internal static string GetServerProperty(object server, string propertyName) |
| 136 | + { |
| 137 | + if (server == null) return String.Empty; |
| 138 | + try |
| 139 | + { |
| 140 | + PSObject pso = PSObject.AsPSObject(server); |
| 141 | + PSPropertyInfo prop = pso.Properties[propertyName]; |
| 142 | + if (prop != null && prop.Value != null) |
| 143 | + return prop.Value.ToString(); |
| 144 | + } |
| 145 | + catch (Exception) |
| 146 | + { |
| 147 | + // Property may not exist on this object type |
| 148 | + } |
| 149 | + return String.Empty; |
| 150 | + } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Gets a value from a query result row. Handles both DataRow and PSObject. |
| 154 | + /// </summary> |
| 155 | + internal static object GetRowValue(PSObject row, string columnName) |
| 156 | + { |
| 157 | + if (row == null) return null; |
| 158 | + |
| 159 | + object baseObj = row.BaseObject; |
| 160 | + if (baseObj is DataRow dataRow) |
| 161 | + { |
| 162 | + try |
| 163 | + { |
| 164 | + object val = dataRow[columnName]; |
| 165 | + if (val == DBNull.Value) return null; |
| 166 | + return val; |
| 167 | + } |
| 168 | + catch (Exception) |
| 169 | + { |
| 170 | + return null; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // Fallback: try PSObject property |
| 175 | + try |
| 176 | + { |
| 177 | + PSPropertyInfo prop = row.Properties[columnName]; |
| 178 | + if (prop != null) |
| 179 | + { |
| 180 | + object val = prop.Value; |
| 181 | + if (val == DBNull.Value) return null; |
| 182 | + return val; |
| 183 | + } |
| 184 | + } |
| 185 | + catch (Exception) |
| 186 | + { |
| 187 | + // Ignore |
| 188 | + } |
| 189 | + return null; |
| 190 | + } |
| 191 | + |
| 192 | + #endregion Helpers |
| 193 | + } |
| 194 | +} |
0 commit comments