【问题标题】:Run script (.bat) from ASP.Net with elevated privileges使用提升的权限从 ASP.Net 运行脚本 (.bat)
【发布时间】:2018-04-21 03:39:55
【问题描述】:

我有一个 ASP.Net 应用程序,我想从中运行几个需要提升权限的 (BAT) 脚本,因为它必须启动和停止服务、复制文件等......

我尝试过采用不同的方法,但找不到一种可行的方法。

这是来自 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();

这是 ServiceTest.bat:

NET START MSSQL$SQLEXPRESS

它不起作用:如果我删除“p.StartInfo.CreateNoWindow”和“p.StartInfo.RedirectStandardOutput”行,控制台窗口会显示“访问被拒绝”错误执行“NET START MSSQL$SQLEXPRESS”。

我尝试使用 Filename "cmd.exe" 和 "/c "+Path.Combine(appPath, "Scripts", "ServiceTest.bat") 作为论据,以及几种方法,但它不起作用。

有什么建议吗?

感谢您的宝贵时间!

【问题讨论】:

  • 你的 Asp.net App 有什么权限?通常这还不够……
  • 我使用的用户没有足够的权限启动 SQL 服务。使用来自@unniks 响应的 PsExec,我可以更改用户,但任何用户都可以启动 SQL 服务。
  • 转到应用程序池并从您的应用程序池中打开高级设置。将“身份”更改为具有更高权限的用户。

标签: c# asp.net batch-file service elevated-privileges


【解决方案1】:

您必须使用不同的凭据。为此使用 RunAs 或 PSExec

https://docs.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>"

【讨论】:

  • 使用 PsExe 我可以与其他用户一起执行脚本。它可以工作,但我需要启动 SQL Server 服务,并且我有同样的“拒绝访问”错误。
  • PsExec \\ -u 管理员 -p "C:\Windows\System32\Net.exe" START MSSQLSERVER
  • 命令 psexec \\myComputer -u myDomain\administrador -p adminPass "C:\windows\system32\net.exe" START MSSQL$SQLEXPRESS 结尾C:\windows\system32\net.exe 退出,错误代码为 2。。我使用域管理员帐户。
  • 检查如下,1.net.exe的路径是否正确,2.Domain Admin是否有权限启动该进程。要检查访问权限,请打开管理员命令提示符并键入 c:\windows\system32\net.exe start
  • 只有以管理员权限打开 CMD.EXE 才能启动服务。
猜你喜欢
  • 2013-05-31
  • 1970-01-01
  • 2021-04-17
  • 2021-01-09
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多