December 13, 2021 at 2:12 pm
Hello,
I am not able to figure out why invoke-sqlcmd is not working. It’s giving login failed error..This user has sysadmin permissions.
When the azure release pipeline runs, if the database does not exist then it restores the new database from the .bak file....and i am using invoke-sqlcmd command for that......when i try to run the select statement using invoke-sqlcmd it works fine but while doing the db restore it does not seem to work and i cannot figure out why.....
The error is below -:
“Invoke-sqlcmd - cannot open database requested by login. The login failed”
I googled the error but nothing seems to be working.
Any help will be appreciated.
Thanks
December 13, 2021 at 2:14 pm
What is the default database for the login which you are using? Is it master?
Can you give us the full command you are trying, sanitised if necessary?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 13, 2021 at 2:48 pm
Thank you for your reply Phil.
Yes, i am using master as default. below is the code -:
$sql = @"
USE [master]
RESTORE DATABASE $dbname FROM DISK = N'$fullpath' WITH FILE = 1, MOVE N'NRI_Data' TO N'G:\Database\$dbname.mdf', MOVE N'NRI_Log' TO N'L:\Log\$dbname.ldf', NOUNLOAD, STATS = 5
GO
"@
invoke-sqlcmd -ServerInstance $server -Database $dbname -Username $sqlusername -Password $sqlpassword -Query $sql
Thank you
December 13, 2021 at 2:51 pm
Try this command instead:
invoke-sqlcmd -ServerInstance $server -Database master -Username $sqlusername -Password $sqlpassword -Query $sql
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 13, 2021 at 3:07 pm
Hi Phil,
That worked like a charm. Thank you so much, really appreciate that.
So, when i used $dbname instead of Master, does it assume that the database should exist? is that why it was failing?
Thank you
December 13, 2021 at 3:16 pm
So, when i used $dbname instead of Master, does it assume that the database should exist? is that why it was failing?
Thank you
Correct. It was trying to run the RESTORE command in the context of $dbname, a bit like doing this
USE $dbname
RESTORE $dbname
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 13, 2021 at 5:47 pm
Hi Phil,
In my release pipeline i am trying to run the powershell script using "File path". In the arguments section i have passed what was defined in the variables section -databasename $(databasename) -dbpassword $(dbpassword) and i also added these parameters in the powershell script. A portion of the script is below -:
[CmdletBinding()]
param (
$databasename,
$dbpassword,
$buildnumber
)
#Looping through the database names specified in variables
$databaseinput='$(databasename)'
$multipledb=$databaseinput.Split(",")
foreach ($db in $multipledb)
{
#Declare and define variables
$server = 'nrisql2'
$dbname=$db
$sqlusername = 'davedutch'
$sqlpassword = '$(dbpassword)'
$buildnumber = '$(build.buildnumber)'
#Split the variable so it finds the hyphen from the string and use the left side of the string
$partialbuildnumbersplit = $buildnumber.split('-')[-2]
#Always skip first 4 characters NRI_
$partialbuildnumber= $partialbuildnumbersplit.Substring(4)
but when i try to run the pipeline it gives me the following error -:
"You cannot call a method on a null-valued expression"
Not sure what i am doing wrong. Can you advise?
Thank you
December 13, 2021 at 5:56 pm
Why are you putting parentheses around some of your variable names? I'm not sure that they're needed.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 13, 2021 at 6:04 pm
while using file path instead of inline, does it behave differently?
December 13, 2021 at 6:22 pm
use $($variablename) to see if it solves your issue.
December 13, 2021 at 7:22 pm
do you mean this way? -:
[CmdletBinding()]
param (
$($databasename),
$($dbpassword),
$($buildnumber)
)
December 13, 2021 at 7:29 pm
Why are you doing this in PowerShell?
--Jeff Moden
Change is inevitable... Change for the better is not.
December 13, 2021 at 8:48 pm
Hi Jeff,
Thanks for your reply. The script i included here is just a part of the long script. I did not see any other way to achieve what i was trying to do.
I wanted to restore a database from the backup file and run another powershell script which accepts parameters.
Did you have any other suggestion in mind which could have made it easier?
Thank you
December 13, 2021 at 8:51 pm
I was able to resolve the issue.
I had to use $env variable for this to work.
$databaseinput=$env:DATABASENAME
$sqlpassword=$evn:DBPASSWORD
$buildnumber=$env:BUILD_BUILDNUMBER
Thank you
December 14, 2021 at 12:51 am
Hi Jeff,
Thanks for your reply. The script i included here is just a part of the long script. I did not see any other way to achieve what i was trying to do.
I wanted to restore a database from the backup file and run another powershell script which accepts parameters.
Did you have any other suggestion in mind which could have made it easier?
Thank you
What's wrong with a simple stored procedure on the server in question?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply