Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
从PowerShell脚本中的Sqlcmd获取错误_Powershell_Sqlcmd - Fatal编程技术网

从PowerShell脚本中的Sqlcmd获取错误

从PowerShell脚本中的Sqlcmd获取错误,powershell,sqlcmd,Powershell,Sqlcmd,我为一个政府客户工作,我们的部署由另一个承包商控制,他要求我们提供powershell脚本来运行数据库脚本。不幸的是,安全性很严格,我没有权限添加powershell管理单元,所以我必须使用好的旧sqlcmd,而不是调用sqlcmd。我在获取运行要传递回powershell的脚本时生成的错误时遇到问题 例如,假设在下面的场景中,“02 test2.sql”有一个错误,导致它无法运行。我希望这样可以终止脚本,将消息记录到控制台,并将sqlcmd的输出记录到文件中。无论我尝试什么,我似乎都无法让Po

我为一个政府客户工作,我们的部署由另一个承包商控制,他要求我们提供powershell脚本来运行数据库脚本。不幸的是,安全性很严格,我没有权限添加powershell管理单元,所以我必须使用好的旧sqlcmd,而不是调用sqlcmd。我在获取运行要传递回powershell的脚本时生成的错误时遇到问题

例如,假设在下面的场景中,“02 test2.sql”有一个错误,导致它无法运行。我希望这样可以终止脚本,将消息记录到控制台,并将sqlcmd的输出记录到文件中。无论我尝试什么,我似乎都无法让PowerShell识别sqlcmd故障

function TerminateScript {
    ECHO "Please email `"Production.log.txt`" to address@mail.com"
    Read-Host -Prompt "Press Enter to exit"
    exit 1
}

ECHO "Running `"01 test1.sql`"" | tee -a Production.log.txt
sqlcmd -r -b /S serverName /d dbName -i "01 test1.sql" >> Production.log.txt
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error occured while running `"01 test1.sql`". Terminating script."
    TerminateScript
}

ECHO "Running `"02 test2.sql`"" | tee -a Production.log.txt
sqlcmd -r -b /S serverName /d dbName -i "02 test2.sql" >> Production.log.txt
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error occured while running `"02 test2.sql`". Terminating script."
    TerminateScript
}

ECHO "Running `"03 test3.sql`"" | tee -a Production.log.txt
sqlcmd -r -b /S serverName /d dbName -i "03 test3.sql" >> Production.log.txt
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error occured while running `"03 test3.sql`". Terminating script."
    TerminateScript
}

不确定
Invoke Sqlcmd
是否引发异常,但如果是,您可以捕获并记录它:

try {
   Invoke-Sqlcmd ... -ErrorAction Stop -Verbose *>> Production.log
} catch {
   Write-Error $_
   Write-Error "Exception: $($_.Exception)"
}
:

通过指定Verbose参数,可以显示SQL Server消息输出,例如SQL PRINT语句产生的消息输出

通过
*>>
我们重定向所有PowerShell并将其附加到一个文件


希望能有所帮助。

如果您只是将正确的输出流重定向到您的文件,那么您的方法应该可以工作<代码>>>默认情况下仅重定向成功流。错误流是
2
2>
)。所有流都是
*
*>
)。如果只想重定向错误流和成功流,可以将错误流发送到成功流,然后通过
2>&1>>Production.log.txt
将成功流发送到您的文件<如果查询产生错误,code>$LastExitCode仍将提供
1

示例:重定向所有流

ECHO "Running `"02 test2.sql`"" | tee -a Production.log.txt
sqlcmd -r -b /S serverName /d dbName -i "02 test2.sql" *>> Production.log.txt
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error occured while running `"02 test2.sql`". Terminating script."
    TerminateScript
}
sqlcmd -r -b /S serverName /d dbName -i "02 test2.sql" 2>&1 >> Production.log.txt
示例:重定向错误和成功流

ECHO "Running `"02 test2.sql`"" | tee -a Production.log.txt
sqlcmd -r -b /S serverName /d dbName -i "02 test2.sql" *>> Production.log.txt
if ($LASTEXITCODE -ne 0) {
    Write-Error "Error occured while running `"02 test2.sql`". Terminating script."
    TerminateScript
}
sqlcmd -r -b /S serverName /d dbName -i "02 test2.sql" 2>&1 >> Production.log.txt
我知道这不完全是你想要的,但这是一种选择。您可以使用
Start Process
调用
sqlcmd
并重定向错误和成功流。请记住,输出文件在每次运行时都会被覆盖。如果没有错误,
error.txt
文件将为空

Start-Process sqlcmd -ArgumentList "-r -b /S poc-vtxrtndb2 /d test -i `"02 test2.sql`"" -NoNewWindow -RedirectStandardError error.txt -RedirectStandardoutput log.txt
if (Get-Content error.txt) {
    "There was an error in the script"
}

有关PowerShell输出重定向的更多信息,请参阅。

不幸的是,我认为这不起作用,但正如我所说的,我不能使用Invoke Sqlcmd,因为这需要我的脚本添加管理单元以使该cmdlet可用,而我没有权限这样做,因为我工作的地方安全性很强。我正试图找到一种只使用sqlcmd的方法。