Reputation: 1642
I have a ASP.Net application from which I want to run several (BAT) scripts that require elevated privileges, because it must start and stop services, copy files, etc ...
I've tried to apply different approaches, but I can't find one that works.
This is the code from ASP.Net c#:
var securePass= new SecureString();
string password = "AdminPassword";
for (int x = 0; x < password.Length; x++)
{
pass.AppendChar(password[x]);
}
Process p = new Process();
p.StartInfo.FileName = Path.Combine(appPath, "Scripts", "ServiceTest.bat");
p.StartInfo.Arguments = "";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UserName = adminUserName;
p.StartInfo.Domain = adminDomain;
p.StartInfo.Password = securePass;
p.StartInfo.Verb = "runas";
p.Start();
p.WaitForExit();
This is the ServiceTest.bat:
NET START MSSQL$SQLEXPRESS
It not works: If I remove "p.StartInfo.CreateNoWindow" and "p.StartInfo.RedirectStandardOutput" lines, the console window shows "Access denied" error executing "NET START MSSQL$SQLEXPRESS".
I tried with Filename "cmd.exe", and "/c "+Path.Combine(appPath, "Scripts", "ServiceTest.bat") as arguments, and several ways but it not work.
Any suggestion?
Thanks for your time!
Upvotes: 0
Views: 888
Reputation: 166
You have to use different credentials. For that use RunAs or PSExec
https://learn.microsoft.com/en-us/sysinternals/downloads/psexec
https://techtorials.me/windows/using-runas-with-a-password/
E.g.
PsExec64.exe \\<local machine Name> -u Domain\Administrator -p <Password> "<Script Name>"
Upvotes: 1