Skip to content

Instantly share code, notes, and snippets.

@idkaaa
Created November 21, 2016 21:57
Show Gist options
  • Save idkaaa/861a642b5fd84db98707dc9107f04b42 to your computer and use it in GitHub Desktop.
Save idkaaa/861a642b5fd84db98707dc9107f04b42 to your computer and use it in GitHub Desktop.
Elevates process using UAC
/// <summary>
/// Adds Create Database permission for the built-in Windows Authenticated Users group if it can.
/// </summary>
public static void AddPermissionAuthenticatedUsersCreateDb(string Server)
{
Log.Trace($"Trying to add create database permission for authenticated users group on server: {Server}");
string SqlcmdLogFile = @"${specialfolder:CommonApplicationData}\" +
Application.CompanyName + @"\" +
Application.ProductName + @"\" +
@"Data\SqlCmd.log";
string SqlCmdPrefix = $@"-S {Server} -o {SqlcmdLogFile} -E -Q ";
string CreateLoginSqlScript = $@"CREATE LOGIN [NT AUTHORITY\Authenticated Users] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english];";
string AddPermissionSqlScript = $@"ALTER SERVER ROLE [dbcreator] ADD MEMBER [NT AUTHORITY\Authenticated Users];";
string CommandAddPermission = $@"{SqlCmdPrefix}""{CreateLoginSqlScript}{AddPermissionSqlScript}""";
string ProcessArgumentsString = $@"{CommandAddPermission}";
Log.Trace($"Process arguments string: {ProcessArgumentsString}");
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "sqlcmd.exe";
info.Arguments = ProcessArgumentsString;
info.UseShellExecute = true;
info.Verb = "runas";
Log.Trace($"Starting Process.");
Process AddPermissionProcess = Process.Start(info);
Log.Trace($"Waiting for Process to finish.");
AddPermissionProcess.WaitForExit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment