3

我正在与一个绝对让我大吃一惊的问题作斗争。

我正在编写在部署期间从 powershell 执行的还原数据库脚本。

这是我的powershell脚本:

Param($RestoreDatabase, $RestoreDatabaseBackupPath, $RestoreDatabaseServer, $RestoreDatabaseUser)

Import-Module "SQLPS"                                   

$sqlcmd="C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd.exe"
$variables = ("db=$RestoreDatabase","BackupPath=$RestoreDatabaseBackupPath","user=${RestoreDatabaseUser}")
$inputFile = Join-Path (Split-Path $MyInvocation.MyCommand.Path -Parent) restore.sql

Write-Output "Executing SQLCMD: inputFile=$inputFile, Variables=$variables, ServerInstance=$RestoreDatabaseServer"

Invoke-SQLCmd -InputFile $inputFile -Variable $variables -ServerInstance $RestoreDatabaseServer -AbortOnError -Verbose -QueryTimeout 65535

Restore.sql(这不是原始文件,但问题仍在重现)

 print 'The first message is not repeating';

 use master
 GO

 print 'Setting the single user mode'
 alter database $(db) set single_user with rollback immediate
 GO

一切看起来都很简单,但是 powershell 脚本的输出是:

 WARNING: The names of some imported commands from the module 'SQLPS' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose 
 parameter. For a list of approved verbs, type Get-Verb.
 Executing SQLCMD: inputFile= C:\Path\To\Powershell\restore.sql, Variables=db=MY_DB_STAGING BackupPath=\\Storage\Backups\BACKUP.bak user=SERVERNAME\Checkee, ServerInstance=SERVERNAME\SQLEXPRESS
 VERBOSE: The first message is not repeating
 VERBOSE: Changed database context to 'master'.
 VERBOSE: Setting the single user mode
 VERBOSE: Setting the single user mode
 Invoke-SQLCmd : The pipeline has been stopped.
 At  C:\Path\To\Powershell\Script.ps1:11 char:1
 + Invoke-SQLCmd -InputFile $inputFile -Variable $variables -ServerInstance $Restor ...
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], PipelineStoppedException
       + FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

   Invoke-SQLCmd : ALTER DATABASE failed because a lock could not be placed on database 'MY_DATABASE_STAGING'. Try again later.
   ALTER DATABASE statement failed.
   At C:\Path\To\Powershell\Script.ps1 :11 char:1
   + Invoke-SQLCmd -InputFile $inputFile -Variable $variables -ServerInstance $Restor ...
   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
     + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

正如您所提到的,输出中有 2 条类似的行

VERBOSE: Setting the single user mode

我认为该命令以某种方式调用了两次,这就是无法放置锁的原因

我如何执行脚本:

  powershell -file Script.ps1 -RestoreDatabase MY_DB_STAGING -RestoreDatabaseBackupPath \\Storage\Backups\BACKUP.bak -RestoreDatabaseServer SERVERNAME\SQLEXPRESS -RestoreDatabaseUser "SERVERNAME\Checkee"

PS一切都像ssms的魅力一样

4

1 回答 1

0

重复线的奥秘使我走向错误的方向。这只是用户权限问题。脚本在不同的帐户下运行顺利。

于 2013-10-08T07:40:08.033 回答