【发布时间】:2016-12-03 04:22:24
【问题描述】:
如何从存储过程中执行 PowerShell 文件?
我没有任何运气从存储过程中执行 PowerShell 文件。
exec xp_cmdshell 'powershell ""E:\PowershellScripts\todayErrorLog.ps1""'
当我运行存储过程时,我得到文件上的访问被拒绝错误。我知道 ps1 文件本身很好,因为我可以在命令行上和作为代理作业执行它。所以我认为这是给存储过程足够的权限的问题。
之前,我确保使用此脚本启用xp_cmdshell
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
为了从存储过程执行 PowerShell 脚本文件,我还需要做什么?
这是存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[RefreshErrorQuery]
-- No parameters
AS
BEGIN
SET NOCOUNT ON;
-- Step 1
exec xp_cmdshell 'powershell ""E:\PowershellScripts\todayErrorLog.ps1""'
-- Step 2: TODO: Run SSIS package
END
【问题讨论】:
-
您是否考虑过从存储过程中执行您提到的那个 SQLAgent 作业?我从来没有尝试过直接从 proc 运行 powershell,但是我已经从 proc 中执行了代理作业,这里有很多例子和在线帮助。
-
都是因为这个:stackoverflow.com/questions/38624228/…。一个建议是我将我的代理作业转换为存储过程,以便我的 MVC 控制器可以知道该过程何时完成。
标签: sql-server powershell stored-procedures