Restore-SqlDatabase等待操作超时

时间:2018-02-07 09:37:09

标签: database powershell timeout wait sqlps

我已经使用Restore-SqlDatabase cmdlet设置了恢复数据库的过程,并将其安排到SQl代理作业中。它一直工作正常,直到我尝试恢复160GB的数据库。它在600s之后基本上失败并出现错误"等待操作超时"。我已经尝试在StatementTimeout中添加0以查看它是否会绕过这个并且还更改SQL Server上的远程查询超时但它仍然在10分钟后出现。有谁知道是否有其他设置我可以更改以增加超时?我运行的代码是:

# Load Module path! its not loading by default in calls from powershell
$env:PSModulePath=$env:PSModulePath + ";" + "C:\Program Files\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\"
## Load module and run functions now

#Import SQL Server PowerShell Provider.
Import-Module "sqlps" -DisableNameChecking

#GEt Around Wait Timeout Error with Remote Connection via PoSh
$server = "THESERVER"
$serverConn = new-object ("Microsoft.SqlServer.Management.Smo.Server") $server
$serverConn.ConnectionContext.StatementTimeout = 0


#Restore Latest Copy of Test Database that was copied over
Restore-SqlDatabase -ServerInstance $server -Database "Test" -BackupFile "H:\SQLBACKUP\DATABASE_Refresh\Test_Latest.BAK" -ReplaceDatabase

出现的错误是:

Restore-SqlDatabase : The wait operation timed out
At H:\SQLBACKUP\_AutoScripts\5_Test_DBRESTORE.ps1:16 char:1
+ Restore-SqlDatabase -ServerInstance $server -Database "Test ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Restore-SqlDatabase], Win32Exception
    + FullyQualifiedErrorId : ExecutionFailed,Microsoft.SqlServer.Management.PowerShell.RestoreSqlDatabaseCommand

1 个答案:

答案 0 :(得分:0)

看起来Restore-SqlDatabase有一个-ConnectionTimeout参数:

  

指定在超时失败之前等待服务器连接的秒数。超时值必须是介于0和65534之间的整数。如果指定为0,则连接尝试不会超时。

所以你的命令可以变成:

Restore-SqlDatabase `
    -ServerInstance $server `
    -Database "Test" `
    -BackupFile "H:\SQLBACKUP\DATABASE_Refresh\Test_Latest.BAK" `
    -ReplaceDatabase `
    -ConnectionTimeout 0