Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Don’t forget to include:
- using System.Data;
- using System.Data.SqlClient;
- /// <summary>
- /// Finds the SQL instances for a User. This gets it's list of servers where the passed SQL Auth user login exists
- /// </summary>
- /// <param name="strLoginName">User Account you are trying to find the instances for</param>
- /// <returns>Returns DataTable</returns>
- public DataTable getUserSQLInstances(string strLoginName, string strServerType)
- {
- DataTable dt_SQLServersForLogin = new DataTable();
- dt_SQLServersForLogin.Columns.Add("Server", typeof(string));
- DataTable sqlservers = getServers(strServerType);
- Int32 db_count = 0;
- foreach (DataRow sqlserver in sqlservers.Rows)
- {
- string instance = sqlserver[0].ToString().Trim();
- string sqlconn = "Data Source=" + instance + ";Initial Catalog=master;Integrated Security=True;";
- SqlConnection conn = new SqlConnection(sqlconn);
- SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM [sys].[syslogins] WHERE [loginname] = '" + strLoginName.ToLower() + "' OR [loginname] = '" + strLoginName.ToUpper() + "'", conn);
- SqlDataAdapter saas_adaptor = new SqlDataAdapter(cmd);
- try
- {
- conn.Open();
- db_count = (Int32)cmd.ExecuteScalar();
- if (db_count == 1)
- {
- dt_SQLServersForLogin.Rows.Add(instance);
- }
- }
- catch (Exception ex)
- {
- string strException = "Error in getUserSQLInstances() Instance: " + instance + " Message: " + ex.Message + " StackTrace: " + ex.StackTrace;
- //LogException("System", "System", strException);
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- }
- }
- return dt_SQLServersForLogin;
- }
- /// <summary>
- /// Get servers matching passed servertype from the saasparam table
- /// </summary>
- /// <param name="strServerType">Server Type</param>
- /// <returns>Returns DataTable</returns>
- public DataTable getServers(string strServerType)
- {
- DataTable dt_sqlservers = new DataTable();
- SqlConnection conn = new SqlConnection(strCloudAdminConnection);
- try
- {
- SqlCommand cmd = new SqlCommand("SELECT [paramval] as [Hostname],[paramvar3] as IPV4address FROM saasparam WHERE paramtype=@strServerType ORDER BY paramval asc", conn);
- SqlDataAdapter saas_adaptor = new SqlDataAdapter(cmd);
- // Set param types
- cmd.Parameters.Add("@strServerType", SqlDbType.VarChar, 50);
- // Set Values
- cmd.Parameters["@strServerType"].Value = strServerType;
- saas_adaptor.Fill(dt_sqlservers);
- }
- catch (Exception ex)
- {
- string strException = "Error in getServers() Message: " + ex.Message + " StackTrace: " + ex.StackTrace;
- //LogException("System", "System", strException);
- }
- finally
- {
- conn.Close();
- conn.Dispose();
- }
- return dt_sqlservers;
- }
- Query to execute on each server where the user account is found:
- There is a couple ways to get if they are locked.
- 1) SELECT LOGINPROPERTY('loginname', 'IsLocked')
- 2) SELECT hasaccess FROM [sys].[syslogins]
- I believe this is the correct value. You might have to test
- TO unlock:
- ALTER LOGIN [UserName] WITHPASSWORD='****'UNLOCK;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement