sql server - SQL command executes twice using Invoke-SQLCMD -


i'm fighting issue absolutely blows mind.

i'm writing restore database script executes powershell during deployment.

here powershell script:

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 (this not original file, issue still reproducing)

 print 'the first message not repeating';   use master  go   print 'setting single user mode'  alter database $(db) set single_user rollback immediate  go 

everything looks simple, output of powershell script is:

 warning: names of imported commands module 'sqlps' include unapproved verbs might make them less discoverable. find commands unapproved verbs, run import-module command again verbose   parameter. 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: first message not repeating  verbose: changed database context 'master'.  verbose: setting single user mode  verbose: setting single user mode  invoke-sqlcmd : pipeline has been stopped.  @  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 lock not placed on database 'my_database_staging'. try again later.    alter database statement failed.    @ 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 

as can mention there 2 similar lines in output

verbose: setting single user mode 

i think command calling twice somehow, , reason why lock not placed

how execute script:

  powershell -file script.ps1 -restoredatabase my_db_staging -restoredatabasebackuppath \\storage\backups\backup.bak -restoredatabaseserver servername\sqlexpress -restoredatabaseuser "servername\checkee" 

p.s. works charm ssms

mystery of duplicated lines lead me wrong direction. user rights issue. script works smoothly under different account.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -