如果我在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代理服务的域服务帐户被我们的Operations组授予了对MyRemoteServer的必要权限。
作业脚本遇到以下错误。这些错误并未阻止脚本:
未处理的异常:System.FormatException:索引(基于零)必须 大于或等于零且小于大小 参数列表。在 System.Text.StringBuilder.AppendFormat(IFormatProvider提供程序, 字符串格式,Object [] args)at System.String.Format(IFormatProvider提供程序,String格式,Object [] args)at Microsoft.SqlServer.Management.PowerShell.Res.GetString(String key, 对象arg0)at Microsoft.SqlServer.Management.PowerShell.SqlPowerShell.GetErrorRecordMessage(ErrorRecord errorRecord)at Microsoft.SqlServer.Management.PowerShell.SqlPowerShell.HandleAgentJob(RunspaceConfiguration 配置) 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 Agent作业正常运行?
答案 0 :(得分:1)
我强烈建议不要在SQL Agent中使用PoSH作业步骤。这是因为执行的PoSH版本可能不是您期望执行的版本。
为了调用PoSH脚本,我只使用调用PoSH可执行文件的CmdExec作业步骤。我的脚本也使用PoSH远程处理,我没有任何问题。
答案 1 :(得分:1)
有两个问题:
1)注意&#34;据称&#34;在我的原始帖子中的这句话中:&#34;运营SQL代理服务的域服务帐户据称被我们的运营小组授予MyRemoteServer必要的权限。&#34;事实证明,所有必需的权限都没有授予运行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}"
这些结合起来同步停止然后启动服务。然后,我在其间插入了一个作业步骤,用于从生产备份中恢复此测试实例中的数据库。