diff --git a/functions/Connect-DbaInstance.ps1 b/functions/Connect-DbaInstance.ps1 index 0a5bbc0086..1d59b524e3 100644 --- a/functions/Connect-DbaInstance.ps1 +++ b/functions/Connect-DbaInstance.ps1 @@ -299,9 +299,6 @@ function Connect-DbaInstance { # Gracefully handle Azure connections if ($instance.ComputerName -match "database\.windows\.net" -and -not $instance.InputObject.ConnectionContext.IsOpen) { - if (-not $Database) { - Stop-Function -Message "You must specify -Database when connecting to a SQL Azure databse" -Continue - } $isAzure = $true # Use available command to build the proper connection string diff --git a/functions/New-DbaConnectionString.ps1 b/functions/New-DbaConnectionString.ps1 index 0c76bf53fd..a6b06bbc39 100644 --- a/functions/New-DbaConnectionString.ps1 +++ b/functions/New-DbaConnectionString.ps1 @@ -201,22 +201,30 @@ function New-DbaConnectionString { process { foreach ($instance in $sqlinstance) { if ($Pscmdlet.ShouldProcess($instance, "Making a new Connection String")) { - if ($instance.ComputerName -match "database\.windows\.net" -and -not $instance.InputObject.ConnectionContext.IsOpen) { - if (-not $Database) { - Stop-Function -Message "You must specify -Database when connecting to a SQL Azure databse" -Continue - } - $isAzure = $true + if ($instance.ComputerName -match "database\.windows\.net") { + if ($instance.InputObject.GetType() -eq [Microsoft.SqlServer.Management.Smo.Server]) { + $connstring = $instance.InputObject.ConnectionContext.ConnectionString + if ($Database) { + $olddb = $connstring -split ';' | Where { $_.StartsWith("Initial Catalog")} + $newdb = "Initial Catalog=$Database" + $connstring = $connstring.Replace($olddb, $newdb) + } + $connstring + continue + } else { + $isAzure = $true - if (-not (Test-Bound -ParameterName ConnectTimeout)) { - $ConnectTimeout = 30 - } + if (-not (Test-Bound -ParameterName ConnectTimeout)) { + $ConnectTimeout = 30 + } - if (-not (Test-Bound -ParameterName ClientName)) { - $ClientName = "dbatools PowerShell module - dbatools.io" + if (-not (Test-Bound -ParameterName ClientName)) { + $ClientName = "dbatools PowerShell module - dbatools.io" + } + $EncryptConnection = $true + $instance = [DbaInstanceParameter]"tcp:$($instance.ComputerName),$($instance.Port)" } - $EncryptConnection = $true - $instance = [DbaInstanceParameter]"tcp:$($instance.ComputerName),$($instance.Port)" } if ($instance.GetType() -eq [Microsoft.SqlServer.Management.Smo.Server]) {