public void TestStringConstructor() { var dbaInstanceParamater = new DbaInstanceParameter("someMachine"); Assert.AreEqual("someMachine", dbaInstanceParamater.FullName); Assert.IsFalse(dbaInstanceParamater.IsLocalHost); }
public void TestDotHostname() { var dbaInstanceParamater = new DbaInstanceParameter("."); Assert.AreEqual(".", dbaInstanceParamater.FullName); Assert.IsTrue(dbaInstanceParamater.IsLocalHost); }
public void TestLocalhostNamedInstance() { var dbaInstanceParamater = new DbaInstanceParameter("localhost\\sql2008r2sp2"); Assert.AreEqual("localhost\\sql2008r2sp2", dbaInstanceParamater.FullName); Assert.IsTrue(dbaInstanceParamater.IsLocalHost); }
public void TestLocalhostIPConstructor() { var dbaInstanceParamater = new DbaInstanceParameter(IPAddress.Loopback); Assert.AreEqual("127.0.0.1", dbaInstanceParamater.FullName); Assert.IsTrue(dbaInstanceParamater.IsLocalHost); }
public void TestStringConstructor() { var dbaInstanceParamater = new DbaInstanceParameter("someMachine"); Assert.AreEqual("someMachine", dbaInstanceParamater.FullName); Assert.AreEqual("someMachine", dbaInstanceParamater.FullSmoName); Assert.AreEqual(SqlConnectionProtocol.Any, dbaInstanceParamater.NetworkProtocol); Assert.IsFalse(dbaInstanceParamater.IsLocalHost); Assert.IsFalse(dbaInstanceParamater.IsConnectionString); }
public void TestLocalhost(string localhost) { var dbaInstanceParamater = new DbaInstanceParameter(localhost); Assert.AreEqual(localhost, dbaInstanceParamater.FullName); Assert.AreEqual('[' + localhost + ']', dbaInstanceParamater.SqlFullName); Assert.AreEqual(localhost, dbaInstanceParamater.FullSmoName); Assert.AreEqual(localhost, dbaInstanceParamater.ToString()); Assert.AreEqual(SqlConnectionProtocol.Any, dbaInstanceParamater.NetworkProtocol); Assert.IsTrue(dbaInstanceParamater.IsLocalHost); }
public void TestIpAddressConstructor(string ipStr) { var ip = IPAddress.Parse(ipStr); var dbaInstanceParamater = new DbaInstanceParameter(ip); Assert.AreEqual(ip.ToString(), dbaInstanceParamater.FullName); Assert.AreEqual('[' + ip.ToString() + ']', dbaInstanceParamater.SqlFullName); Assert.AreEqual(ip.ToString(), dbaInstanceParamater.FullSmoName); Assert.AreEqual(ip.ToString(), dbaInstanceParamater.ToString()); Assert.AreEqual(SqlConnectionProtocol.Any, dbaInstanceParamater.NetworkProtocol); }
public void TestDotHostname() { var dbaInstanceParamater = new DbaInstanceParameter("."); Assert.AreEqual(".", dbaInstanceParamater.FullName); Assert.IsTrue(dbaInstanceParamater.IsLocalHost); Assert.AreEqual("NP:.", dbaInstanceParamater.FullSmoName); Assert.AreEqual(SqlConnectionProtocol.NP, dbaInstanceParamater.NetworkProtocol); Assert.IsTrue(dbaInstanceParamater.IsLocalHost); Assert.IsFalse(dbaInstanceParamater.IsConnectionString); }
public void TestLocalhostNamedInstance() { var dbaInstanceParamater = new DbaInstanceParameter("localhost\\sql2008r2sp2"); Assert.AreEqual("localhost\\sql2008r2sp2", dbaInstanceParamater.FullName); Assert.IsTrue(dbaInstanceParamater.IsLocalHost); Assert.AreEqual("localhost\\sql2008r2sp2", dbaInstanceParamater.FullSmoName); Assert.AreEqual(SqlConnectionProtocol.Any, dbaInstanceParamater.NetworkProtocol); Assert.IsTrue(dbaInstanceParamater.IsLocalHost); Assert.IsFalse(dbaInstanceParamater.IsConnectionString); }
public void TestEmptyString(string whitespace) { try { var dbaInstanceParamater = new DbaInstanceParameter(whitespace); } catch (BloodyHellGiveMeSomethingToWorkWithException ex) { Assert.AreEqual("DbaInstanceParameter", ex.ParameterClass); throw; } }
public void TestLocalDbConnectionString() { var dbaInstanceParamater = new DbaInstanceParameter(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet-MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Movies.mdf"); Assert.AreEqual("localhost", dbaInstanceParamater.ComputerName); Assert.AreEqual("[localhost]", dbaInstanceParamater.SqlComputerName); Assert.AreEqual(@"localhost\MSSQLLocalDB", dbaInstanceParamater.FullName); Assert.AreEqual(@"localhost\MSSQLLocalDB", dbaInstanceParamater.FullSmoName); Assert.AreEqual(SqlConnectionProtocol.Any, dbaInstanceParamater.NetworkProtocol);; Assert.IsTrue(dbaInstanceParamater.IsLocalHost); Assert.IsTrue(dbaInstanceParamater.IsConnectionString); }
public void TestLocalDb() { var dbaInstanceParamater = new DbaInstanceParameter(@"(LocalDb)\MSSQLLocalDB"); Assert.AreEqual("localhost", dbaInstanceParamater.ComputerName); Assert.AreEqual("[localhost]", dbaInstanceParamater.SqlComputerName); Assert.AreEqual(@"localhost\MSSQLLocalDB", dbaInstanceParamater.FullName); Assert.AreEqual(@"localhost\MSSQLLocalDB", dbaInstanceParamater.FullSmoName); Assert.AreEqual(@"localhost\MSSQLLocalDB", dbaInstanceParamater.InstanceName); Assert.AreEqual(@"localhost\MSSQLLocalDB", dbaInstanceParamater.SqlInstanceName); Assert.AreEqual(SqlConnectionProtocol.Any, dbaInstanceParamater.NetworkProtocol); Assert.IsTrue(dbaInstanceParamater.IsLocalHost); Assert.IsTrue(dbaInstanceParamater.IsConnectionString); }
/// <summary> /// Creates a DBA Instance Parameter from an established SQL Connection /// </summary> /// <param name="Connection">The connection to reuse</param> public DbaInstanceParameter(System.Data.SqlClient.SqlConnection Connection) { InputObject = Connection; DbaInstanceParameter tempParam = new DbaInstanceParameter(Connection.DataSource); _ComputerName = tempParam.ComputerName; if (tempParam.InstanceName != "MSSQLSERVER") { _InstanceName = tempParam.InstanceName; } if (tempParam.Port != 1433) { _Port = tempParam.Port; } _NetworkProtocol = tempParam.NetworkProtocol; }
public void TestConnectionString() { var dbaInstanceParamater = new DbaInstanceParameter("Server=tcp:server.database.windows.net;Database=myDataBase;User ID =[LoginForDb]@[serverName]; Password = myPassword; Trusted_Connection = False;Encrypt = True; "); Assert.IsTrue(dbaInstanceParamater.IsConnectionString); }
public void TestEmptyString() { var dbaInstanceParamater = new DbaInstanceParameter("\t"); }
/// <summary> /// Creates a DBA Instance parameter from any object /// </summary> /// <param name="Input">Object to parse</param> public DbaInstanceParameter(object Input) { InputObject = Input; PSObject tempInput = new PSObject(Input); string typeName = ""; try { typeName = tempInput.TypeNames[0].ToLower(); } catch { throw new PSArgumentException("Failed to interpret input as Instance: " + Input); } typeName = typeName.Replace("Deserialized.", ""); switch (typeName) { case "microsoft.sqlserver.management.smo.server": try { if (tempInput.Properties["ServerType"] != null && (string)tempInput.Properties["ServerType"].Value.ToString() == "SqlAzureDatabase") { _ComputerName = (new DbaInstanceParameter((string)tempInput.Properties["Name"].Value)).ComputerName; } else { if (tempInput.Properties["NetName"] != null) { _ComputerName = (string)tempInput.Properties["NetName"].Value; } else { _ComputerName = (new DbaInstanceParameter((string)tempInput.Properties["DomainInstanceName"].Value)).ComputerName; } } _InstanceName = (string)tempInput.Properties["InstanceName"].Value; PSObject tempObject = new PSObject(tempInput.Properties["ConnectionContext"].Value); string tempConnectionString = (string)tempObject.Properties["ConnectionString"].Value; tempConnectionString = tempConnectionString.Split(';')[0].Split('=')[1].Trim().Replace(" ", ""); if (Regex.IsMatch(tempConnectionString, @",\d{1,5}$") && (tempConnectionString.Split(',').Length == 2)) { try { Int32.TryParse(tempConnectionString.Split(',')[1], out _Port); } catch (Exception e) { throw new PSArgumentException("Failed to parse port number on connection string: " + tempConnectionString, e); } if (_Port > 65535) { throw new PSArgumentException("Failed to parse port number on connection string: " + tempConnectionString); } } } catch (Exception e) { throw new PSArgumentException("Failed to interpret input as Instance: " + Input + " : " + e.Message, e); } break; case "microsoft.sqlserver.management.smo.linkedserver": try { _ComputerName = (string)tempInput.Properties["Name"].Value; } catch (Exception e) { throw new PSArgumentException("Failed to interpret input as Instance: " + Input, e); } break; case "microsoft.activedirectory.management.adcomputer": try { _ComputerName = (string)tempInput.Properties["Name"].Value; // We prefer using the dnshostname whenever possible if (tempInput.Properties["DNSHostName"].Value != null) { if (!String.IsNullOrEmpty((string)tempInput.Properties["DNSHostName"].Value)) { _ComputerName = (string)tempInput.Properties["DNSHostName"].Value; } } } catch (Exception e) { throw new PSArgumentException("Failed to interpret input as Instance: " + Input, e); } break; case "microsoft.sqlserver.management.registeredservers.registeredserver": try { //Pass the ServerName property of the SMO object to the string constrtuctor, //so we don't have to re-invent the wheel on instance name / port parsing DbaInstanceParameter parm = new DbaInstanceParameter((string)tempInput.Properties["ServerName"].Value); _ComputerName = parm.ComputerName; if (parm.InstanceName != "MSSQLSERVER") { _InstanceName = parm.InstanceName; } if (parm.Port != 1433) { _Port = parm.Port; } } catch (Exception e) { throw new PSArgumentException("Failed to interpret input as Instance: " + Input, e); } break; default: throw new PSArgumentException("Failed to interpret input as Instance: " + Input); } }
/// <summary> /// Creates a DBA Instance Parameter from string /// </summary> /// <param name="Name">The name of the instance</param> public DbaInstanceParameter(string Name) { InputObject = Name; if (string.IsNullOrWhiteSpace(Name)) { throw new BloodyHellGiveMeSomethingToWorkWithException("Please provide an instance name", "DbaInstanceParameter"); } if (Name == ".") { _ComputerName = Name; _NetworkProtocol = SqlConnectionProtocol.NP; return; } string tempString = Name.Trim(); tempString = Regex.Replace(tempString, @"^\[(.*)\]$", "$1"); if (UtilityHost.IsLike(tempString, @".\*")) { _ComputerName = Name; _NetworkProtocol = SqlConnectionProtocol.NP; string instanceName = tempString.Substring(2); if (!Utility.Validation.IsValidInstanceName(instanceName)) { throw new ArgumentException(String.Format("Failed to interpret instance name: '{0}' is not a legal name!", instanceName)); } _InstanceName = instanceName; return; } if (UtilityHost.IsLike(tempString, "*.WORKGROUP")) { tempString = Regex.Replace(tempString, @"\.WORKGROUP$", "", RegexOptions.IgnoreCase); } // Named Pipe path notation interpretation if (Regex.IsMatch(tempString, @"^\\\\[^\\]+\\pipe\\([^\\]+\\){0,1}sql\\query$", RegexOptions.IgnoreCase)) { try { _NetworkProtocol = SqlConnectionProtocol.NP; _ComputerName = Regex.Match(tempString, @"^\\\\([^\\]+)\\").Groups[1].Value; if (Regex.IsMatch(tempString, @"\\MSSQL\$[^\\]+\\", RegexOptions.IgnoreCase)) { _InstanceName = Regex.Match(tempString, @"\\MSSQL\$([^\\]+)\\", RegexOptions.IgnoreCase).Groups[1].Value; } } catch (Exception e) { throw new ArgumentException(String.Format("Failed to interpret named pipe path notation: {0} | {1}", InputObject, e.Message), e); } return; } // Connection String interpretation try { System.Data.SqlClient.SqlConnectionStringBuilder connectionString = new System.Data.SqlClient.SqlConnectionStringBuilder(tempString); DbaInstanceParameter tempParam = new DbaInstanceParameter(connectionString.DataSource); _ComputerName = tempParam.ComputerName; if (tempParam.InstanceName != "MSSQLSERVER") { _InstanceName = tempParam.InstanceName; } if (tempParam.Port != 1433) { _Port = tempParam.Port; } _NetworkProtocol = tempParam.NetworkProtocol; if (UtilityHost.IsLike(tempString, @"(localdb)\*")) { _NetworkProtocol = SqlConnectionProtocol.NP; } IsConnectionString = true; return; } catch (ArgumentException ex) { string name = "unknown"; try { name = ex.TargetSite.GetParameters()[0].Name; } catch { } if (name == "keyword") { throw; } } catch (FormatException) { throw; } catch { } // Handle and clear protocols. Otherwise it'd make port detection unneccessarily messy if (Regex.IsMatch(tempString, "^TCP:", RegexOptions.IgnoreCase)) //TODO: Use case insinsitive String.BeginsWith() { _NetworkProtocol = SqlConnectionProtocol.TCP; tempString = tempString.Substring(4); } if (Regex.IsMatch(tempString, "^NP:", RegexOptions.IgnoreCase)) // TODO: Use case insinsitive String.BeginsWith() { _NetworkProtocol = SqlConnectionProtocol.NP; tempString = tempString.Substring(3); } // Case: Default instance | Instance by port if (tempString.Split('\\').Length == 1) { if (Regex.IsMatch(tempString, @"[:,]\d{1,5}$") && !Regex.IsMatch(tempString, RegexHelper.IPv6) && ((tempString.Split(':').Length == 2) || (tempString.Split(',').Length == 2))) { char delimiter; if (Regex.IsMatch(tempString, @"[:]\d{1,5}$")) { delimiter = ':'; } else { delimiter = ','; } try { Int32.TryParse(tempString.Split(delimiter)[1], out _Port); if (_Port > 65535) { throw new PSArgumentException("Failed to parse instance name: " + tempString); } tempString = tempString.Split(delimiter)[0]; } catch { throw new PSArgumentException("Failed to parse instance name: " + Name); } } if (Utility.Validation.IsValidComputerTarget(tempString)) { _ComputerName = tempString; } else { throw new PSArgumentException("Failed to parse instance name: " + Name); } } // Case: Named instance else if (tempString.Split('\\').Length == 2) { string tempComputerName = tempString.Split('\\')[0]; string tempInstanceName = tempString.Split('\\')[1]; if (Regex.IsMatch(tempComputerName, @"[:,]\d{1,5}$") && !Regex.IsMatch(tempComputerName, RegexHelper.IPv6)) { char delimiter; if (Regex.IsMatch(tempComputerName, @"[:]\d{1,5}$")) { delimiter = ':'; } else { delimiter = ','; } try { Int32.TryParse(tempComputerName.Split(delimiter)[1], out _Port); if (_Port > 65535) { throw new PSArgumentException("Failed to parse instance name: " + Name); } tempComputerName = tempComputerName.Split(delimiter)[0]; } catch { throw new PSArgumentException("Failed to parse instance name: " + Name); } } else if (Regex.IsMatch(tempInstanceName, @"[:,]\d{1,5}$") && !Regex.IsMatch(tempInstanceName, RegexHelper.IPv6)) { char delimiter; if (Regex.IsMatch(tempString, @"[:]\d{1,5}$")) { delimiter = ':'; } else { delimiter = ','; } try { Int32.TryParse(tempInstanceName.Split(delimiter)[1], out _Port); if (_Port > 65535) { throw new PSArgumentException("Failed to parse instance name: " + Name); } tempInstanceName = tempInstanceName.Split(delimiter)[0]; } catch { throw new PSArgumentException("Failed to parse instance name: " + Name); } } // LocalDBs mostly ignore regular Instance Name rules, so that validation is only relevant for regular connections if (UtilityHost.IsLike(tempComputerName, "(localdb)") || (Utility.Validation.IsValidComputerTarget(tempComputerName) && Utility.Validation.IsValidInstanceName(tempInstanceName, true))) { if (UtilityHost.IsLike(tempComputerName, "(localdb)")) { _ComputerName = "(localdb)"; } else { _ComputerName = tempComputerName; } if ((tempInstanceName.ToLower() != "default") && (tempInstanceName.ToLower() != "mssqlserver")) { _InstanceName = tempInstanceName; } } else { throw new PSArgumentException(string.Format("Failed to parse instance name: {0}. Computer Name: {1}, Instance {2}", Name, tempComputerName, tempInstanceName)); } } // Case: Bad input else { throw new PSArgumentException("Failed to parse instance name: " + Name); } }
/// <summary> /// Creates a new DbaCmConnectionParameter based on an instance parameter /// </summary> /// <param name="Instance">The instance to interpret</param> public DbaCmConnectionParameter(DbaInstanceParameter Instance) : this(Instance.ComputerName) { }
/// <summary> /// Creates a DBA Instance Parameter from string /// </summary> /// <param name="Name">The name of the instance</param> public DbaInstanceParameter(string Name) { InputObject = Name; if (Name == "") { throw new BloodyHellGiveMeSomethingToWorkWithException("Please provide an instance name", "DbaInstanceParameter"); } if (Name == ".") { _ComputerName = Name; _NetworkProtocol = SqlConnectionProtocol.NP; return; } string tempString = Name.Trim(); tempString = Regex.Replace(tempString, @"^\[(.*)\]$", "$1"); // Named Pipe path notation interpretation if (Regex.IsMatch(tempString, @"^\\\\[^\\]+\\pipe\\([^\\]+\\){0,1}sql\\query$", RegexOptions.IgnoreCase)) { try { _NetworkProtocol = SqlConnectionProtocol.NP; _ComputerName = Regex.Match(tempString, @"^\\\\([^\\]+)\\").Groups[1].Value; if (Regex.IsMatch(tempString, @"\\MSSQL\$[^\\]+\\", RegexOptions.IgnoreCase)) { _InstanceName = Regex.Match(tempString, @"\\MSSQL\$([^\\]+)\\", RegexOptions.IgnoreCase).Groups[1].Value; } } catch (Exception e) { throw new ArgumentException(String.Format("Failed to interpret named pipe path notation: {0} | {1}", InputObject, e.Message), e); } return; } // Connection String interpretation try { System.Data.SqlClient.SqlConnectionStringBuilder connectionString = new System.Data.SqlClient.SqlConnectionStringBuilder(tempString); DbaInstanceParameter tempParam = new DbaInstanceParameter(connectionString.DataSource); _ComputerName = tempParam.ComputerName; if (tempParam.InstanceName != "MSSQLSERVER") { _InstanceName = tempParam.InstanceName; } if (tempParam.Port != 1433) { _Port = tempParam.Port; } _NetworkProtocol = tempParam.NetworkProtocol; IsConnectionString = true; return; } catch { } // Handle and clear protocols. Otherwise it'd make port detection unneccessarily messy if (Regex.IsMatch(tempString, "^TCP:", RegexOptions.IgnoreCase)) //TODO: Use case insinsitive String.BeginsWith() { _NetworkProtocol = SqlConnectionProtocol.TCP; tempString = tempString.Substring(4); } if (Regex.IsMatch(tempString, "^NP:", RegexOptions.IgnoreCase)) // TODO: Use case insinsitive String.BeginsWith() { _NetworkProtocol = SqlConnectionProtocol.NP; tempString = tempString.Substring(3); } // Case: Default instance | Instance by port if (tempString.Split('\\').Length == 1) { if (Regex.IsMatch(tempString, @"[:,]\d{1,5}$") && !Regex.IsMatch(tempString, RegexHelper.IPv6) && ((tempString.Split(':').Length == 2) || (tempString.Split(',').Length == 2))) { char delimiter; if (Regex.IsMatch(tempString, @"[:]\d{1,5}$")) { delimiter = ':'; } else { delimiter = ','; } try { Int32.TryParse(tempString.Split(delimiter)[1], out _Port); if (_Port > 65535) { throw new PSArgumentException("Failed to parse instance name: " + tempString); } tempString = tempString.Split(delimiter)[0]; } catch { throw new PSArgumentException("Failed to parse instance name: " + Name); } } if (Utility.Validation.IsValidComputerTarget(tempString)) { _ComputerName = tempString; } else { throw new PSArgumentException("Failed to parse instance name: " + Name); } } // Case: Named instance else if (tempString.Split('\\').Length == 2) { string tempComputerName = tempString.Split('\\')[0]; string tempInstanceName = tempString.Split('\\')[1]; if (Regex.IsMatch(tempComputerName, @"[:,]\d{1,5}$") && !Regex.IsMatch(tempComputerName, RegexHelper.IPv6)) { char delimiter; if (Regex.IsMatch(tempComputerName, @"[:]\d{1,5}$")) { delimiter = ':'; } else { delimiter = ','; } try { Int32.TryParse(tempComputerName.Split(delimiter)[1], out _Port); if (_Port > 65535) { throw new PSArgumentException("Failed to parse instance name: " + Name); } tempComputerName = tempComputerName.Split(delimiter)[0]; } catch { throw new PSArgumentException("Failed to parse instance name: " + Name); } } else if (Regex.IsMatch(tempInstanceName, @"[:,]\d{1,5}$") && !Regex.IsMatch(tempInstanceName, RegexHelper.IPv6)) { char delimiter; if (Regex.IsMatch(tempString, @"[:]\d{1,5}$")) { delimiter = ':'; } else { delimiter = ','; } try { Int32.TryParse(tempInstanceName.Split(delimiter)[1], out _Port); if (_Port > 65535) { throw new PSArgumentException("Failed to parse instance name: " + Name); } tempInstanceName = tempInstanceName.Split(delimiter)[0]; } catch { throw new PSArgumentException("Failed to parse instance name: " + Name); } } if (Utility.Validation.IsValidComputerTarget(tempComputerName) && Utility.Validation.IsValidInstanceName(tempInstanceName)) { _ComputerName = tempComputerName; _InstanceName = tempInstanceName; } else { throw new PSArgumentException(string.Format("Failed to parse instance name: {0}. Computer Name: {1}, Instance {2}", Name, tempComputerName, tempInstanceName)); } } // Case: Bad input else { throw new PSArgumentException("Failed to parse instance name: " + Name); } }