dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

Property DatabaseName cannot be changed or read after a connection string has been set.

Open OwainEsau opened this issue 7 years ago • 6 comments

Environmental information

#### PowerShell version:

Name                           Value                                                                                                                                                                                             
----                           -----                                                                                                                                                                                             
PSVersion                      5.1.17758.1                                                                                                                                                                                       
PSEdition                      Desktop                                                                                                                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                           
BuildVersion                   10.0.17758.1                                                                                                                                                                                      
CLRVersion                     4.0.30319.42000                                                                                                                                                                                   
WSManStackVersion              3.0                                                                                                                                                                                               
PSRemotingProtocolVersion      2.3                                                                                                                                                                                               
SerializationVersion           1.1.0.1                                                                                                                                                                                           




#### dbatools Module version:


Name    : dbatools
Path    : C:\Program Files\WindowsPowerShell\Modules\dbatools\1.0.0\dbatools.psd1
Version : 1.0.0

Name    : dbatools
Path    : C:\Program Files\WindowsPowerShell\Modules\dbatools\0.9.834\dbatools.psd1
Version : 0.9.834

Name    : dbatools
Path    : C:\Program Files\WindowsPowerShell\Modules\dbatools\0.9.804\dbatools.psd1
Version : 0.9.804

Name    : dbatools
Path    : C:\Program Files\WindowsPowerShell\Modules\dbatools\0.9.531\dbatools.psd1
Version : 0.9.531

Name    : dbatools
Path    : C:\Program Files\WindowsPowerShell\Modules\dbatools\0.9.487\dbatools.psd1
Version : 0.9.487

LCID             Name             DisplayName                                                                                                                                                                                    
----             ----             -----------                                                                                                                                                                                    
3081             en-AU            English (Australia)                                                                                                                                                                            

SQL Server:

Microsoft SQL Azure (RTM) - 12.0.2000.8   May  2 2019 20:11:13   Copyright (C) 2019 Microsoft Corporation 
us_english

Report

The error message i have seen before but i thought it was fixed?

It is on Invoke-DBAQuery when trying to use SQLCredentials:

WARNING: [11:54:33][Invoke-DbaQuery] [SERVERADDRESS] Failed during execution | Property DatabaseName cannot be changed or read after a connection string has been set.

This pops up after adding the -Database parameter to the command.

Host used

  • [x] powershell.exe
  • [ ] ISE
  • [ ] VS Code
  • [ ] Other (please specify)

If anything other than powershell.exe was used, please confirm that you can duplicate the issue with powershell.exe

  • [ ] Still buggy in powershell.exe

Errors Received



writeErrorStream      : True
PSMessageDetails      : 
Exception             : System.Exception: Property DatabaseName cannot be changed or read after a connection string has been set. ---> System.Management.Automation.SetValueInvocationException: Exception setting 
                        "DatabaseName": "Property DatabaseName cannot be changed or read after a connection string has been set." ---> Microsoft.SqlServer.Management.Common.PropertyNotAvailableException: Property 
                        DatabaseName cannot be changed or read after a connection string has been set.
                           at Microsoft.SqlServer.Management.Common.ConnectionSettings.ThrowIfConnectionStringIsSet(String propertyName)
                           at Microsoft.SqlServer.Management.Common.ConnectionSettings.set_DatabaseName(String value)
                           at CallSite.Target(Closure , CallSite , Object , String )
                           --- End of inner exception stack trace ---
                           at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
                           at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
                           --- End of inner exception stack trace ---
TargetObject          : SERVERADDRESS
CategoryInfo          : NotSpecified: (SERVERADDRESS:PSObject) [Write-Error], Exception
FullyQualifiedErrorId : dbatools_Invoke-DbaQuery,Stop-Function
ErrorDetails          : Property DatabaseName cannot be changed or read after a connection string has been set.
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Stop-Function, C:\Program Files\WindowsPowerShell\Modules\dbatools\1.0.0\allcommands.ps1: line 79692
                        at Invoke-DbaQuery<Process>, C:\Program Files\WindowsPowerShell\Modules\dbatools\1.0.0\allcommands.ps1: line 44647
                        at <ScriptBlock>, <No file>: line 2
PipelineIterationInfo : {0, 1}

Steps to Reproduce

Run Invoke-DBAQuery with SQLCredential parameter and without the database parameter. Add the database parameter after running initially.

Expected Behavior

for me to be able to add the database parameter.

Actual Behavior

Have to restart PC to add the database parameter.

OwainEsau avatar Jul 09 '19 02:07 OwainEsau

So you are calling the invoke command successfully without the database parameter. Then making a second call with the database parameter and received the error?

Are you connecting to the same server?

Can you update the module to the latest release to verify if the but persists beyond the latest version you are running?

wsmelton avatar Jul 09 '19 09:07 wsmelton

@wsmelton I am on the latest release yes. Server address remains the same i omitted the database name and was trying to just specify the database in the Query FROM [database].dbo.database etc. Didnt realise i couldn't do that with an Azure SQL server so i added the database parameter which is when i got this error. Restarting the PowerShell session doesnt resolve the error

OwainEsau avatar Jul 10 '19 00:07 OwainEsau

Confirmed latest release, 1.0.20, includes this bug...

image

wsmelton avatar Jul 11 '19 00:07 wsmelton

Anyone have time to fix this one? It seems important and I'm knee deep in another project.

potatoqualitee avatar Jul 11 '19 06:07 potatoqualitee

thought it was a problem on invoke-dbaquery but it seems is originated from connect-dbainstance in the first place....

try

$server = connect-dbainstance -sqlinstance azure -databasename 'whatever' $server.connectioncontext.executescalar("select db_name()")

niphlod avatar Jul 15 '19 16:07 niphlod

@wsmelton , @OwainEsau , @potatoqualitee : would you please give #5898 a try ?

niphlod avatar Jul 15 '19 22:07 niphlod