使用 SMO.Agent 检索 SQL 作业执行状态 - 安全问题
我有一个 C# 程序,它使用 SQL Server 管理对象 (SMO) 接口。它看起来像:
Server ssis_server = new Server(
new ServerConnection(SERVER_NAME, SERVER_USERNAME, SERVER_PASSWORD)
);
var agent = ssis_server.JobServer;
var ssis_job = agent.Jobs[job_name];
var current_status = ssis_job.CurrentRunStatus;
if (current_status == JobExecutionStatus.Idle)
{
ssis_job.Start();
OnSuccess("Job started: " + job_name);
}
else
{
OnError("Job is already running or is not ready.");
}
我正在使用 SQL服务器身份验证在这一点上简化了事情,同时我解决了这个问题。
现在,我的问题是,除非 SERVER_USERNAME
是“sysadmin”dbo 角色的一部分,否则 ssis_job.CurrentRunStatus
始终为“Idle
” - 甚至当我知道作业正在运行时。它不会出错,只是总是报告空闲。
如果用户是管理员,则会按预期返回状态。
你说的是角色成员资格?
好吧,我将 SERVER_USERNAME
SQL Server 登录名添加到 msdb 角色 SQLAgentOperatorRole,但这似乎没有帮助。
该作业的所有者是系统管理员帐户 - 如果这是问题,我不知道如何解决它。
有什么想法吗?
I've got a C# program that fires off SQL Server Agent jobs using the SQL Server Management Objects (SMO) interfaces. It looks something like:
Server ssis_server = new Server(
new ServerConnection(SERVER_NAME, SERVER_USERNAME, SERVER_PASSWORD)
);
var agent = ssis_server.JobServer;
var ssis_job = agent.Jobs[job_name];
var current_status = ssis_job.CurrentRunStatus;
if (current_status == JobExecutionStatus.Idle)
{
ssis_job.Start();
OnSuccess("Job started: " + job_name);
}
else
{
OnError("Job is already running or is not ready.");
}
I'm using SQL Server Authentication at this point to simplfy things whilst I work this out.
Now, my problem is that unless the SERVER_USERNAME
is part of the 'sysadmin' dbo role, ssis_job.CurrentRunStatus
is always 'Idle
' - even when I know the job is running. It doesn't error out, just always reports idle.
If the user is an administrator, then the status is returned as expected.
Role membership you say?
Well, I added the SERVER_USERNAME
SQL Server login to the msdb Role SQLAgentOperatorRole, that didn't seem to help.
The job's owner is a system administrator account - if that's the issue I'm not sure how to work around it.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在检查状态之前,您需要通过调用 ssis_job 上的 Refresh() 方法来刷新作业,然后您将获得正确的信息。
You need to refresh the job by calling the Refresh() method on ssis_job before checking the status, then you will get the correct information.