Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/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
Sql server Powershell还原脚本超时_Sql Server_Powershell_Sql Server 2008 R2 - Fatal编程技术网

Sql server Powershell还原脚本超时

Sql server Powershell还原脚本超时,sql-server,powershell,sql-server-2008-r2,Sql Server,Powershell,Sql Server 2008 R2,我有一个Powershell脚本,我一直在使用它来自动化我的数据库恢复,它已经工作了一段时间,没有任何问题了 最近它开始失败并抛出超时错误 要突出显示该示例中的代码块,请执行以下操作: ############################################################################ # Restore db Script block ######################################################

我有一个Powershell脚本,我一直在使用它来自动化我的数据库恢复,它已经工作了一段时间,没有任何问题了

最近它开始失败并抛出超时错误

要突出显示该示例中的代码块,请执行以下操作:

############################################################################
# Restore db Script block
############################################################################
[ScriptBlock] $global:RestoreDBSMO = {
    param([string] $newDBName, [string] $backupFilePath, [bool] $isNetworkPath = $true, [string] $newDataPath, [string] $newLogPath)
# http://www.codeproject.com/Articles/110908/SQL-DB-Restore-using-PowerShell
    try
    {
        # Load assemblies
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
        [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
        [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

        # Create sql server object
        $server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") "(local)"

        # Copy database locally if backup file is on a network share
        if($isNetworkPath)
        {
            $fileName = [IO.Path]::GetFileName($backupFilePath)
            $localPath = Join-Path -Path $server.DefaultFile -ChildPath $fileName
            Copy-Item $backupFilePath $localPath
            $backupFilePath = $localPath
        }

        # Create restore object and specify its settings
        $smoRestore = new-object("Microsoft.SqlServer.Management.Smo.Restore")
        $smoRestore.Database = $newDBName
        $smoRestore.NoRecovery = $false;
        $smoRestore.ReplaceDatabase = $true;
        $smoRestore.Action = "Database"

        # Create location to restore from
        $backupDevice = New-Object("Microsoft.SqlServer.Management.Smo.BackupDeviceItem") ($backupFilePath, "File") 
        $smoRestore.Devices.Add($backupDevice)

        # Give empty string a nice name
        $empty = ""

        # Specify new data file (mdf)

           $smoRestoreDataFile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile")
            if (($newDataPath -eq $null) -or ($newDataPath -eq $empty)) { #use exixting path
           $defaultData = $server.DefaultFile
            if (($defaultData -eq $null) -or ($defaultData -eq $empty))
            {
                $defaultData = $server.MasterDBPath
            }
        } Else {$defaultData = $newDataPath}
        $fullPath = Join-Path -Path $defaultData -ChildPath ($newDBName + "_Data.mdf")
        $smoRestoreDataFile.PhysicalFileName = $fullPath

        # Specify new log file (ldf)
        $smoRestoreLogFile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile")
        if (($newLogPath -eq $null) -or ($newLogPath -eq $empty)) { #use exixting path
            $defaultLog = $server.DefaultLog
            if (($defaultLog -eq $null) -or ($defaultLog -eq $empty))
            {
                $defaultLog = $server.MasterDBLogPath
            } 
        } Else {$defaultLog = $newLogPath}
        $fullPath = Join-Path -Path $defaultLog -ChildPath ($newDBName + "_Log.ldf")
        $smoRestoreLogFile.PhysicalFileName = $fullPath

        # Get the file list from backup file
        $dbFileList = $smoRestore.ReadFileList($server)

        # The logical file names should be the logical filename stored in the backup media
        $smoRestoreDataFile.LogicalFileName = $dbFileList.Select("Type = 'D'")[0].LogicalName
        $smoRestoreLogFile.LogicalFileName = $dbFileList.Select("Type = 'L'")[0].LogicalName

        # Add the new data and log files to relocate to
        $smoRestore.RelocateFiles.Add($smoRestoreDataFile)
        $smoRestore.RelocateFiles.Add($smoRestoreLogFile)

        # Restore the database
        $smoRestore.SqlRestore($server)

        "Database restore completed successfully"
    }
    catch [Exception]
    {
        "Database restore failed:`n`n " + $_.Exception
    }
    finally
    {
        # Clean up copied backup file after restore completes successfully
        if($isNetworkPath)
        {
            Remove-Item $backupFilePath
        }
    }
}
在执行实际还原时,会抛出以下错误:

Restoring DB...
0
1
Database restore failed:

 Microsoft.SqlServer.Management.Smo.FailedOperationException: Restore failed for Server 'SRV_MIC_DEV'.  ---> Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch. ---> System.
Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
The backup or restore was aborted.
10 percent processed.
20 percent processed.
30 percent processed.
40 percent processed.
50 percent processed.
60 percent processed.
   at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException)
   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
   --- End of inner exception stack trace ---
   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
   at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection sqlCommands, ExecutionTypes executionType)
   at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection queries)
   at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server server, StringCollection queries)
   at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)
   --- End of inner exception stack trace ---
   at Microsoft.SqlServer.Management.Smo.Restore.SqlRestore(Server srv)
   at SqlRestore(Object , Object[] )
   at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[] originalArguments)
我尝试查看了Powershell文档和其他链接,注意到一些网站提到Powershell cmdlet有600秒的默认超时

我正试图从Powershell或SQL Server中找到一种解决方案来增加此超时时间,但我在文档或常规web搜索方面没有什么运气

我的脚本是否有任何快速修复方法来增加超时或简单的替代方法


非常感谢您的帮助。

您在脚本中使用invoke-sqlcmd命令吗?@Venkatakrishnan我刚刚在谷歌上搜索了
invoke-sqlcmd
,现在我想我对
cmdlet
是什么感到有些困惑。我没有在Powershell脚本中使用
invoke sqlcmd
,所以我想我的问题不是默认的600秒超时。但这仍然是一个超时问题。您知道扩展此功能的任何解决方案吗?如果您在脚本中使用invoke-sqlcmd,则默认情况下需要600秒的超时时间。因为invoke-sqlcmd是一个内置命令,它是SQLPS模块的一部分。如果这是条件,我可以为您提供一个函数,您可以在其中更改超时value@Venkatakrishnan实际上,我正在使用这个方法来实际恢复我的数据库。没有从我的Powershell脚本执行任何SQL命令。您可以检查我的
try
块中的最后一行代码。如果不介意,请发布用于还原的完整脚本