Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 使用powershell命令从powershell脚本创建代理作业_Sql Server_Powershell_Sql Agent - Fatal编程技术网

Sql server 使用powershell命令从powershell脚本创建代理作业

Sql server 使用powershell命令从powershell脚本创建代理作业,sql-server,powershell,sql-agent,Sql Server,Powershell,Sql Agent,我尝试使用power shell脚本自动执行SQL Server 2016安装过程。当我使用file参数执行Invoke SqlCmd时: Invoke-SqlCmd -InputFile "..\res\test.sql" -ServerInstance "(local)" -Database "master" -ErrorAction Stop 其中包括带有powershell命令的代理作业创建脚本: EXEC @ReturnCode = msdb.dbo.sp_add_jobstep

我尝试使用power shell脚本自动执行SQL Server 2016安装过程。当我使用file参数执行Invoke SqlCmd时:

Invoke-SqlCmd -InputFile "..\res\test.sql" -ServerInstance "(local)" -Database "master" -ErrorAction Stop 
其中包括带有powershell命令的代理作业创建脚本:

EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Erase Phantom System Health Records.', 
    @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'if (''$(ESCAPE_SQUOTE(INST))'' -eq ''MSSQLSERVER'') {$a = ''\DEFAULT''} ELSE {$a = ''''};
       (Get-Item SQLSERVER:\SQLPolicy\$(ESCAPE_NONE(SRVR))$a).EraseSystemHealthPhantomRecords()', 
        @database_name=N'master', 
        @flags=0
它因错误而停止:

Invoke-SqlCmd : 'INST' scripting variable not defined

显然,这是$character,我需要在创建之前转义它。有人知道怎么做吗?我搜索了,但找不到任何有效的块。

PowerShell正在尝试解析您的
$(ESCAPE_xxxx(yyyy))
块,因此您需要在将其发送到调用SqlCmd之前对其进行转义。最简单的方法是:


显然,您不可能只转义SQL命令块中的每一个括号,但您可以做一些实验来找出这种替换需要有多窄或多宽。

禁用InvokeSQLcmd的变量


调用SqlCmd-InputFile.\res\test.sql“-ServerInstance”(local)”-数据库“master”-ErrorAction Stop-DisableVariables

我看不出该脚本有任何错误。SQL解释器将两个连续的单引号转换为单引号,这将阻止PowerShell解释
$
符号。
ESCAPE_SQUOTE
也是正确的,它将用两个单引号替换实例名称中的任何单引号,然后PowerShell将其转换回单引号。因此,如果sql脚本中的
EXEC
命令没有任何其他更改,我只能得出这样的结论:不知何故,您的作业代理不知道
INST
变量-该变量自sql 2005年起就存在了…问题在于sql cmd本身。如果我们在mmc中执行脚本-一切正常,如果我们在SqlCmd模式(通过调用SqlCmd使用的模式)中切换脚本-它将失败,并出现相同的错误。
$script = Get-Content "..\res\test.sql"
$script -replace '\(ESCAPE_(\w+)\((.+)\)\)','`(ESCAPE_$1`($2`)`)'
Invoke-SqlCmd -InputFile $script -ServerInstance "(local)" -Database "master" -ErrorAction Stop