【问题标题】:PowerShell directly vs. SQL Agent PowerShell Job StepsPowerShell 直接与 SQL 代理 PowerShell 作业步骤
【发布时间】:2017-06-22 13:31:26
【问题描述】:

如果我在 Powershell 中运行这 3 行脚本(以管理员身份运行,并且我的域帐户是 MyRemoteServer 上的本地管理员),它会按预期工作(请注意,我必须转义的服务名称中有一个美元符号带有反勾号):

Invoke-Command -ComputerName "MyRemoteServer" -ScriptBlock{Get-Service -Name "AOS60`$01" | Stop-Service -Force}
get-service -Name "AOS60`$01" -ComputerName "MyRemoteServer" | Format-Table -Property MachineName, Status, Name, DisplayName -auto
Invoke-Command -ComputerName "MyRemoteServer" -ScriptBlock{Get-Service -Name "AOS60`$01" | Start-Service}

[第 2 步是提供证据证明服务在第 1 步之后实际上具有“已停止”状态。一旦成功转换为代理作业,第 2 步将实际调用执行一系列数据库恢复的存储过程。]

如果我将这些语句作为作业步骤放入 SQL 代理 (SQL Server 2012 SP2) 并启动作业,则作业会在第一步崩溃(它不仅失败,它还想调用调试器)。据称,运行 SQL 代理服务的域服务帐户已被我们的运营组授予对 MyRemoteServer 的必要权限。

作业脚本遇到以下错误。这些错误并未停止脚本:

未处理的异常:System.FormatException:索引(从零开始)必须 大于或等于零且小于 参数列表。在 System.Text.StringBuilder.AppendFormat(IFormatProvider 提供程序, 字符串格式,Object[] args) 在 System.String.Format(IFormatProvider 提供程序,字符串格式,对象 [] 参数)在 Microsoft.SqlServer.Management.PowerShell.Res.GetString(字符串键, 对象 arg0) 在 Microsoft.SqlServer.Management.PowerShell.SqlPowerShell.GetErrorRecordMessage(错误记录 错误记录)在 Microsoft.SqlServer.Management.PowerShell.SqlPowerShell.HandleAgentJob(运行空间配置 配置)在 Microsoft.SqlServer.Management.PowerShell.SqlPowerShell.Main(字符串 [] 参数)

我编写了以下作业以供参考。请注意,即使我省略了@output_file_name,它也会失败,因此问题不在于写入输出文件,而在于执行步骤本身。

USE [msdb]
GO

/****** Object:  Job [Test stopping and restarting AX]    Script Date: 6/21/2017 1:49:30 PM ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object:  JobCategory [[Uncategorized (Local)]]    Script Date: 6/21/2017 1:49:30 PM ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback

END

DECLARE @jobId BINARY(16)
EXEC @ReturnCode =  msdb.dbo.sp_add_job @job_name=N'Test stopping and restarting AX', 
        @enabled=1, 
        @notify_level_eventlog=0, 
        @notify_level_email=0, 
        @notify_level_netsend=0, 
        @notify_level_page=0, 
        @delete_level=0, 
        @description=N'No description available.', 
        @category_name=N'[Uncategorized (Local)]', 
        @owner_login_name=N'ROGUE\amfreeman', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [Synchronously Stop AX Service on DEVAOS2]    Script Date: 6/21/2017 1:49:30 PM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Synchronously Stop AX Service on DEVAOS2', 
        @step_id=1, 
        @cmdexec_success_code=0, 
        @on_success_action=3, 
        @on_success_step_id=0, 
        @on_fail_action=2, 
        @on_fail_step_id=0, 
        @retry_attempts=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=N'PowerShell', 
        @command=N'Invoke-Command -ComputerName "MyRemoteServer" -ScriptBlock{Get-Service -Name "AOS60`$01" | Stop-Service -Force}', 
        @database_name=N'master', 
        @output_file_name=N'E:\Microsoft SQL Server\MSSQL11.RELEASE\MSSQL\Log\Test_stopping_and_restarting_AX_$(ESCAPE_SQUOTE(STEPID))_Synchronously_Stop_AX_Service_on_DEVAOS2.txt', 
        @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [Get status of  AX Service on DEVAOS2]    Script Date: 6/21/2017 1:49:30 PM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Get status of  AX Service on DEVAOS2', 
        @step_id=2, 
        @cmdexec_success_code=0, 
        @on_success_action=3, 
        @on_success_step_id=0, 
        @on_fail_action=2, 
        @on_fail_step_id=0, 
        @retry_attempts=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=N'PowerShell', 
        @command=N'get-service -Name "AOS60`$01" -ComputerName "MyRemoteServer" | Format-Table -Property MachineName, Status, Name, DisplayName -auto', 
        @database_name=N'master', 
        @output_file_name=N'E:\Microsoft SQL Server\MSSQL11.RELEASE\MSSQL\Log\Test_stopping_and_restarting_AX_$(ESCAPE_SQUOTE(STEPID))_Get_status_of__AX_Service_on_DEVAOS2.txt', 
        @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object:  Step [Synchronously Start AX Service on MyRemoteServer]    Script Date: 6/21/2017 1:49:30 PM ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Synchronously Start AX Service on MyRemoteServer', 
        @step_id=3, 
        @cmdexec_success_code=0, 
        @on_success_action=1, 
        @on_success_step_id=0, 
        @on_fail_action=2, 
        @on_fail_step_id=0, 
        @retry_attempts=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=N'PowerShell', 
        @command=N'Invoke-Command -ComputerName "MyRemoteServer" -ScriptBlock{Get-Service -Name "AOS60`$01" | Start-Service}', 
        @database_name=N'master', 
        @output_file_name=N'E:\Microsoft SQL Server\MSSQL11.RELEASE\MSSQL\Log\Test_stopping_and_restarting_AX_$(ESCAPE_SQUOTE(STEPID))_Synchronously_Start_AX_Service_on_MyRemoteServer.txt', 
        @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:

如何让 SQL 代理作业正常运行?

【问题讨论】:

  • 我会先验证您的脚本是否使用服务帐户在 Powershell 2.0 版中运行,然后再将其投入工作。见travisgan.com/2013/05/sql-server-agent-job-and-powershell.html
  • @SteveB:好主意!当我将 Powershell 作为服务帐户运行时,它无权查看该服务。所以它必须需要以管理员身份运行,但我不知道如何以另一个用户身份运行和同时以管理员身份运行。我正在运行它的机器是带有 SP1 的 2008 R2 服务器,它似乎正在运行 PS 1.0。但我不认为这是 1.0 与 2.0 的问题。

标签: sql-server powershell sql-server-2012 jobs sql-job


【解决方案1】:

我强烈建议不要在 SQL 代理中使用 PoSH 作业步骤。那是因为执行的 PoSH 版本可能不是您期望执行的版本。

为了调用 PoSH 脚本,我只使用调用 PoSH 可执行文件的 CmdExec 作业步骤。我的脚本也使用 PoSH 远程处理,我没有任何问题。

【讨论】:

    【解决方案2】:

    有两个问题:

    1) 请注意我原来帖子中这句话中的“据称”一词:“运行 SQL 代理服务的域服务帐户据称已被我们的运营组授予对 MyRemoteServer 的必要权限。”事实证明,所有必需的权限都没有授予运行 SQL Server 代理服务的域帐户。

    2) 我接受了@NedOtter(以及其他各种博客和 Stack 帖子)的建议,并通过 CmdExec 转换为使用 PowerShell。我不知道这是否有必要,但它似乎肯定是对前进道路的共识。

    我使用了两个作业步骤,@command 参数中包含以下内容:

    C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe "Invoke-Command -ComputerName ''MyRemoteServer'' -ScriptBlock{Get-Service -Name ''AOS60$01'' | Stop-Service -Force}"
    C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe "Invoke-Command -ComputerName ''MyRemoteServer'' -ScriptBlock{Get-Service -Name ''AOS60$01'' | Start-Service}"
    

    这些结合起来同步停止然后启动服务。然后,我在其中插入了一个作业步骤,从我的生产备份中恢复此测试实例中的数据库。

    【讨论】:

    • PowerShell 作业步骤的问题仅部分与您所在的 PS 版本有关,如果您使用的是最新版本的 SQL Server,此问题已得到修复。主要问题是作业步骤在 SQLPS(SQL Server 的提供程序)的上下文中运行。与PowerShell.exe宿主的环境不一样,所以你可以根据你在做什么得到不同的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多