如何在ASP.NET MVC中从我的应用程序运行xp_cmdshell

时间:2014-09-28 09:00:59

标签: asp.net tsql xp-cmdshell

在我的应用程序中,我将数据读取到数据库,然后我想启动Integration Serivces将数据导入到多维数据集中。这是命令,我用它从SSMS启动它:

execute master..xp_cmdshell 'dtexec /DTS "\MSDB\B_Costs" /SERVER . /CHECKPOINTING OFF /REPORTING V'

但是如何从我的应用程序中使用它?我在开头就遇到了这个错误:
对象' xp_cmdshell',数据库' mssqlsystemresource',架构' sys'

上的EXECUTE权限被拒绝

使用此命令后:

GRANT EXECUTE ON xp_cmdshell TO [IIS APPPOOL\DefaultAppPool]

我还有另一个错误:
无法检索xp_cmdshell代理帐户信息或该信息无效。验证' ## xp_cmdshell_proxy_account ##'凭证存在且包含有效信息。

当我检查时,我在数据库引擎中没有凭据 - >安全 - >凭证

我想用这样的命令:

EXEC sp_xp_cmdshell_proxy_account 'IIS APPPOOL\DefaultAppPool', 'password'

但我不知道我应该在密码中写什么。有什么建议吗?

这是我的CubeController:

public ActionResult Index()
{            
    string sql = "execute master..xp_cmdshell 'dtexec /DTS \"\\MSDB\\B_Costs\" /SERVER . /CHECKPOINTING OFF /REPORTING V'";
    try
    {
        MyEntity.Database.ExecuteSqlCommand(sql);
    }
    catch (Exception e)
    {
        string error = "There was an error: <br />" + e.Message;
        TempData["Message"] = error;
    }

    return RedirectToAction("Index","Home");
}

1 个答案:

答案 0 :(得分:0)

希望以下链接能够为您提供帮助。如果有效,请告诉我。

http://www.databasejournal.com/features/mssql/xpcmdshell-for-non-system-admin-individuals.html

After you set up a proxy account your non-sysadmin logins might still not be able to use xp_cmdshell.    If you have not granted your non-sysadmin user EXECUTE permissions on xp_cmdshell you will get this error:

Msg 229, Level 14, State 5, Procedure xp_cmdshell, Line 1
The EXECUTE permission was denied on the object 'xp_cmdshell', database 'mssqlsystemresource', schema 'sys'.
To overcome this error all you have to do is make sure the non-sysadmin user has a database user in the master database and then GRANT your non-sysadmin user the rights to execute xp_cmdshell.  In order to do that all you have to do is run the following TSL code:

USE master;
GO
CREATE USER [MyDomain\Dorothy] FOR LOGIN [MyDomain\Dorothy];
GRANT EXECUTE ON xp_cmdshell TO [MyDomain\Dorothy];

就我而言,我尝试过:

GRANT EXECUTE ON xp_cmdshell TO [IIS APPPOOL\DefaultAppPool]

我正在授予

  

默认应用

请参阅屏幕截图供您参考: http://postimg.org/image/yqcf3vya1/

结果是成功的。我使用我的sa或管理员帐户通过SQL Server Management Studio登录我的数据库来执行GRANT EXECUTE。请尝试让我知道结果。感谢