Here below I am pasting my commands what I used in ms-dos batch file and successfully run that batch file also and got admin privilege for the user.
set "params=%*"
(Below total command in single line only)
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del
"%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul
2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) :
UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "",
"runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit
/B )
(Above total command in single line only)
net start MSSQL$SQL2016
I tried Below code in c#
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = "/all";
process.Start();
process.StandardInput.WriteLine("set " "params=% *" "");
process.StandardInput.WriteLine("cd /d " " % ~dp0" " && ( if exist " " % temp % \" getadmin.vbs" " del " " % temp %\"getadmin.vbs" " ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^(" "Shell.Application" "^) : UAC.ShellExecute " "cmd.exe" ", " " / k cd " " % ~sdp0" " && % ~s0 %params% " ", " "," "runas" ", 1 >> " " % temp %" "\" " "getadmin.vbs" " && " " % temp %\"getadmin.vbs" " && exit /B )");
process.StandardInput.WriteLine("net start MSSQL$SQL2016");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.Read();
when I ran this file in c# no result and showing no error also. So I did some mistakes. So maybe you people can help me to get rid of this.
CodePudding user response:
It looks like you are trying to execute PowerShell commands -- CreateObject, UAC.ShellExecute -- with cmd.exe. The process.StartInfo.FileName field should probably be set to powershell.exe or pwsh.exe.
CodePudding user response:
It looks like the real question is how to start/stop the SQL Server service. One way to do this is using the ServiceController class.
var name="MSSQL$SQL2016";
var service=ServiceController.GetServices()
.First(svc=>svc.ServiceName==name);
service.Start();
Another way is to use the SQL Server Management Objects. These classes can be used to start/stop services, create databases, backups etc.
var mc = new ManagedComputer();
svc = mc.Services("MSSQL$SQL2016");
svc.Start();