RBr*_*own 5 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
Run Code Online (Sandbox Code Playgroud)
这是带有Invoke-SqlCmd的.ps1脚本的第5行:
Invoke-Sqlcmd -ServerInstance "$serverInstance" -InputFile "C:\Program Files\DBBackups\dbfull_backups.sql" -Variable $sqlVariable -Verbose *> $outputFile
Run Code Online (Sandbox Code Playgroud)
这是来自SQL的BACKUP命令:
BACKUP DATABASE FrameStore TO DISK = @framestore_backup_file;
Run Code Online (Sandbox Code Playgroud)
通过Powershell / Invoke-Sqlcmd运行时的超时与从SSMS运行时的超时不同吗?在SSMS中运行时,备份需要不到3分钟的时间,因此与我在docs.microsoft文档中看到的10分钟的数字不太接近。这是在SSMS中运行时的备份输出:
BACKUP DATABASE successfully processed 467003 pages in 176.917 seconds (20.622 MB/sec).
Run Code Online (Sandbox Code Playgroud)
版本信息:
操作系统:Windows Server 2012 R2
MS SQL Server Express 2017
PSVersion 4.0
(如果在另一个SO页面中已经解决了这个问题,我深表歉意。尽管看起来很基本,但我找不到此特定问题。)
编辑:
我继续尝试添加-QueryTimeout参数设置为3600秒。这样可以成功备份而不会出现错误。那么,默认值是什么?
根据Microsoft文档页面的Invoke-Sqlcmd -QueryTimeout参数:
指定查询超时之前的秒数。如果未指定超时值,则查询不会超时。
所以...如果是真的,但我未指定超时,为什么会超时呢?从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 /
长问题短答案您需要使用参数-Querytimeout X,其中 X 是以秒为单位的时间
有问题的查询将像这样工作,其中 0 无限超时,我们可以使用 1 到 65535 之间的整数值。
Invoke-Sqlcmd -ServerInstance "$serverInstance" -Querytimeout 0 -InputFile "C:\Program Files\DBBackups\dbfull_backups.sql" -Variable $sqlVariable -Verbose *> $outputFile
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6281 次 |
| 最近记录: |