Skip to content
Permalink
Browse files

Invokedbaquery updates (#5205)

Connect-DbaInstance enhancements
  • Loading branch information...
potatoqualitee committed Mar 15, 2019
1 parent d21cdc2 commit 4fcfeac5faa362f1fed2fffa7a5223e576a961a6
Showing with 37 additions and 8 deletions.
  1. +19 −4 functions/Connect-DbaInstance.ps1
  2. +8 −4 functions/New-DbaConnectionString.ps1
  3. +10 −0 tests/Connect-DbaInstance.Tests.ps1
@@ -290,6 +290,7 @@ function Connect-DbaInstance {
$Fields201x_Login = $Fields200x_Login + @('PasswordHashAlgorithm')
}
process {

if (Test-FunctionInterrupt) { return }

foreach ($instance in $SqlInstance) {
@@ -298,7 +299,16 @@ function Connect-DbaInstance {
#endregion Safely convert input into instance parameters

# Gracefully handle Azure connections
if ($instance.ComputerName -match "database\.windows\.net" -and -not $instance.InputObject.ConnectionContext.IsOpen) {
if ($instance.ComputerName -match "database\.windows\.net" -or $instance.InputObject.ComputerName -match "database\.windows\.net") {
# so far, this is not evaluating
if ($instance.InputObject.ConnectionContext.IsOpen) {
$currentdb = $instance.InputObject.ConnectionContext.ExecuteScalar("select db_name()")
if (($Database -and ($Database -eq $currentdb))) {
$instance.InputObject
continue
}
}

$isAzure = $true

# Use available command to build the proper connection string
@@ -314,14 +324,17 @@ function Connect-DbaInstance {
}
# Build connection string
$azureconnstring = New-DbaConnectionString @boundparams

try {
# this is the way, as recommended by Microsoft
# https://docs.microsoft.com/en-us/sql/relational-databases/security/encryption/configure-always-encrypted-using-powershell?view=sql-server-2017
$sqlconn = New-Object System.Data.SqlClient.SqlConnection $azureconnstring
$serverconn = New-Object Microsoft.SqlServer.Management.Common.ServerConnection $sqlconn
$null = $serverconn.Connect()
$server = New-Object Microsoft.SqlServer.Management.Smo.Server $serverconn
# Make ComputerName easily available in the server object
Add-Member -InputObject $server -NotePropertyName ComputerName -NotePropertyValue $instance.ComputerName -Force
Add-Member -InputObject $server -NotePropertyName NetPort -NotePropertyValue $instance.Port -Force -Passthru
continue
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_ -Continue
}
@@ -389,7 +402,8 @@ function Connect-DbaInstance {
} else {
$parsedcomputername = $server.NetName
}
Add-Member -InputObject $server -NotePropertyName ComputerName -NotePropertyValue $parsedcomputername -Force
Add-Member -InputObject $server -NotePropertyName ComputerName -NotePropertyValue $instance.ComputerName -Force
Add-Member -InputObject $server -NotePropertyName NetPort -NotePropertyValue $instance.Port -Force
}
if ($MinimumVersion -and $server.VersionMajor) {
if ($server.versionMajor -lt $MinimumVersion) {
@@ -616,7 +630,8 @@ function Connect-DbaInstance {
} else {
$parsedcomputername = $server.NetName
}
Add-Member -InputObject $server -NotePropertyName ComputerName -NotePropertyValue $parsedcomputername -Force
Add-Member -InputObject $server -NotePropertyName ComputerName -NotePropertyValue $instance.ComputerName -Force
Add-Member -InputObject $server -NotePropertyName NetPort -NotePropertyValue $instance.Port -Force
}
}

@@ -199,15 +199,19 @@ function New-DbaConnectionString {
)

process {
foreach ($instance in $sqlinstance) {
foreach ($instance in $SqlInstance) {
if ($Pscmdlet.ShouldProcess($instance, "Making a new Connection String")) {
if ($instance.ComputerName -match "database\.windows\.net") {
if ($instance.ComputerName -match "database\.windows\.net" -or $instance.InputObject.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")}
$olddb = $connstring -split ';' | Where-Object { $_.StartsWith("Initial Catalog")}
$newdb = "Initial Catalog=$Database"
$connstring = $connstring.Replace($olddb, $newdb)
if ($olddb) {
$connstring = $connstring.Replace("$olddb", "$newdb")
} else {
$connstring = "$connstring;$newdb;"
}
}
$connstring
continue
@@ -24,6 +24,16 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
$s.Name | Should -match $script:azureserver
$s.DatabaseEngineType | Should -Be 'SqlAzureDatabase'
}

It -Skip "Should keep the same database context" {
$results = Invoke-DbaQuery -SqlInstance $s -Query "select db_name()"
$results | Should -Be 'test'
}

It -Skip "Should keep the same database context" {
$server = Connect-DbaInstance -SqlInstance $s
$server.Query("select db_name()") | Should -Be 'test'
}
}
}

0 comments on commit 4fcfeac

Please sign in to comment.
You can’t perform that action at this time.