sql-server - 获取 "Execution Timeout Expired"通过 Invoke-Sqlcmd 运行 SQL Server 备份

标签 sql-server powershell

免责声明:我是 Powershell 新手。

我正在尝试使用从 Powershell 运行的 SQL 脚本编写两个 SQL Server Express 数据库的备份脚本。当我从 SSMS 运行 SQL 时,它运行良好,没有超时。当我从 Powershell 脚本运行 SQL 时,小型数据库备份成功,但大型数据库备份失败并出现以下错误:

Invoke-Sqlcmd : Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
At C:\Program Files\DBBackups\dbfull_backup.ps1:5 char:1
+ Invoke-Sqlcmd -ServerInstance "$serverInstance" -InputFile "C:\Program Files\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
    + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

这是 .ps1 脚本的第 5 行,其中包含 Invoke-SqlCmd:

Invoke-Sqlcmd -ServerInstance "$serverInstance" -InputFile "C:\Program Files\DBBackups\dbfull_backups.sql" -Variable $sqlVariable -Verbose *> $outputFile

这是来自 SQL 的 BACKUP 命令:

BACKUP DATABASE FrameStore TO DISK = @framestore_backup_file;

通过 Powershell/Invoke-Sqlcmd 运行时的超时与通过 SSMS 运行时的超时是否不同?在 SSMS 中运行时,备份需要不到 3 分钟的时间,因此与我在 docs.microsoft 文档中看到的 10 分钟数字相差甚远。以下是在 SSMS 中运行时的备份输出:

BACKUP DATABASE successfully processed 467003 pages in 176.917 seconds (20.622 MB/sec).

版本信息:
操作系统:Windows Server 2012 R2
MS SQL Server Express 2017
PS版本4.0

(如果这个问题已经在另一个 SO 页面中得到解决,我深表歉意。我找不到这个具体问题,尽管它看起来很基本。)

编辑:
我继续尝试将 -QueryTimeout 参数设置为 3600 秒。这使得备份成功且没有错误。那么,默认值是什么?

根据Microsoft Docs page for Invoke-Sqlcmd -QueryTimeout parameter :

Specifies the number of seconds before the queries time out. If a timeout value is not specified, the queries do not time out.

那么...如果这是真的并且我没有指定超时,为什么会超时?这是否从 PS 版本 4 更改为记录的版本?在备份上设置超时似乎是一种不好的做法吗?我对对此的看法很感兴趣。它现在可以工作了,但我仍然对我的解决方案没有信心(解决方法?)。

最佳答案

所以您已经达到了 SMO 对象著名的 30 秒超时。

Grant Fritchey 早在 2010 年就写过相关内容。在这里阅读:https://www.scarydba.com/2010/04/16/powershell-smo-problem/

如果您将其设置为 0 - 您将告诉它无限运行。

最近一篇关于相同内容的博客文章:https://fbrazeal.wordpress.com/2015/06/29/sqlps-tips/

已编辑:

根据您运行的版本,您还应该阅读以下内容:https://blogs.technet.microsoft.com/heyscriptingguy/2013/05/06/10-tips-for-the-sql-server-powershell-scripter/

关于sql-server - 获取 "Execution Timeout Expired"通过 Invoke-Sqlcmd 运行 SQL Server 备份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52545013/

相关文章:

powershell - 重复的命令行参数

powershell - 如果您只是要编写过程代码,那么MSBUILD/NANT有什么意义? Powershell更好吗?

powershell - 通过 .cmd 文件安装 "azure-powershell.0.8.7.msi"时出现错误

SQL查询-如何按列值获取每个组的列的最大值

sql-server - 具有定义范围的 ASCII 增量

windows - 在 Windows 上通过别名将 git hist 设置为 pretty-print 日志

azure - 无法在 PowerShell Runbook 中从 Azure 存储下载模板文件

c# - 如何获取表和主键,如果表没有主键则显示null?

Sql varchar 每次都会转到 else

sql-server - 将数据从列转置为行