Advertisement
dr0vvNs

Untitled

Mar 5th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. //Don’t forget to include:
  2.  
  3. using System.Data;
  4. using System.Data.SqlClient;
  5.  
  6.  
  7.  
  8.         /// <summary>
  9.         /// Finds the SQL instances for a User.  This gets it's list of servers where the passed SQL Auth user login exists
  10.         /// </summary>
  11.         /// <param name="strLoginName">User Account you are trying to find the instances for</param>
  12.         /// <returns>Returns DataTable</returns>
  13.         public DataTable getUserSQLInstances(string strLoginName, string strServerType)
  14.         {
  15.             DataTable dt_SQLServersForLogin = new DataTable();
  16.             dt_SQLServersForLogin.Columns.Add("Server", typeof(string));
  17.             DataTable sqlservers = getServers(strServerType);
  18.  
  19.  
  20.             Int32 db_count = 0;
  21.             foreach (DataRow sqlserver in sqlservers.Rows)
  22.             {
  23.                 string instance = sqlserver[0].ToString().Trim();
  24.                 string sqlconn = "Data Source=" + instance + ";Initial Catalog=master;Integrated Security=True;";
  25.                 SqlConnection conn = new SqlConnection(sqlconn);
  26.                 SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM [sys].[syslogins] WHERE [loginname] = '" + strLoginName.ToLower() + "' OR [loginname] = '" + strLoginName.ToUpper() + "'", conn);
  27.                 SqlDataAdapter saas_adaptor = new SqlDataAdapter(cmd);
  28.  
  29.                 try
  30.                 {
  31.                     conn.Open();
  32.                     db_count = (Int32)cmd.ExecuteScalar();
  33.  
  34.                     if (db_count == 1)
  35.                     {
  36.                         dt_SQLServersForLogin.Rows.Add(instance);
  37.                     }
  38.  
  39.                 }
  40.                 catch (Exception ex)
  41.                 {
  42.                     string strException = "Error in getUserSQLInstances()   Instance: " + instance + "    Message: " + ex.Message + "      StackTrace: " + ex.StackTrace;
  43.                     //LogException("System", "System", strException);
  44.                 }
  45.                 finally
  46.                 {
  47.                     conn.Close();
  48.                     conn.Dispose();
  49.                 }
  50.             }
  51.  
  52.             return dt_SQLServersForLogin;
  53.         }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.         /// <summary>
  61.         ///  Get servers matching passed servertype from the saasparam table
  62.         /// </summary>
  63.         /// <param name="strServerType">Server Type</param>
  64.         /// <returns>Returns DataTable</returns>
  65.         public DataTable getServers(string strServerType)
  66.         {
  67.             DataTable dt_sqlservers = new DataTable();
  68.             SqlConnection conn = new SqlConnection(strCloudAdminConnection);
  69.  
  70.             try
  71.             {
  72.                 SqlCommand cmd = new SqlCommand("SELECT [paramval] as [Hostname],[paramvar3] as IPV4address FROM saasparam WHERE paramtype=@strServerType ORDER BY paramval asc", conn);
  73.                 SqlDataAdapter saas_adaptor = new SqlDataAdapter(cmd);
  74.  
  75.                 // Set param types
  76.                 cmd.Parameters.Add("@strServerType", SqlDbType.VarChar, 50);
  77.  
  78.  
  79.                 // Set Values
  80.                 cmd.Parameters["@strServerType"].Value = strServerType;
  81.  
  82.  
  83.                 saas_adaptor.Fill(dt_sqlservers);
  84.  
  85.  
  86.             }
  87.             catch (Exception ex)
  88.             {
  89.                 string strException = "Error in getServers()      Message: " + ex.Message + "      StackTrace: " + ex.StackTrace;
  90.                 //LogException("System", "System", strException);
  91.  
  92.             }
  93.             finally
  94.             {
  95.                 conn.Close();
  96.                 conn.Dispose();
  97.  
  98.             }
  99.  
  100.             return dt_sqlservers;
  101.         }
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. Query to execute on each server where the user account is found:
  109.  
  110.  
  111. There is a couple ways to get if they are locked.
  112.  
  113. 1)  SELECT LOGINPROPERTY('loginname', 'IsLocked')
  114. 2)    SELECT hasaccess FROM [sys].[syslogins]
  115. I believe this is the correct value.  You might have to test
  116.  
  117.  
  118.  
  119. TO unlock:
  120. ALTER LOGIN [UserName] WITHPASSWORD='****'UNLOCK;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement