SQL命令使用Invoke-SQLCMD执行两次

时间:2013-10-07 15:33:22

标签: sql-server powershell

我正在与一个绝对让我大开眼界的问题作斗争。

我正在编写还原数据库脚本,该脚本在部署期间从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

正如您所说,输出中有两条相似的行

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"

P.S。一切都像ssms的魅力

1 个答案:

答案 0 :(得分:0)

重复线条之谜导致我的方向错误。这只是用户权限问题。脚本在不同的帐户下运行顺畅。