From abd026076b7b3cf90edcf9d4942af0924ebf8a2f Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Sun, 17 Mar 2019 19:02:07 +0100 Subject: [PATCH] fixes #5025 --- functions/Get-DbaCmsRegServerStore.ps1 | 2 +- .../functions/Connect-ConnstringInstance.ps1 | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 internal/functions/Connect-ConnstringInstance.ps1 diff --git a/functions/Get-DbaCmsRegServerStore.ps1 b/functions/Get-DbaCmsRegServerStore.ps1 index 4a879f30b5..2f099175c7 100644 --- a/functions/Get-DbaCmsRegServerStore.ps1 +++ b/functions/Get-DbaCmsRegServerStore.ps1 @@ -53,7 +53,7 @@ function Get-DbaCmsRegServerStore { process { foreach ($instance in $SqlInstance) { try { - $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $sqlcredential + $server = Connect-ConnstringInstance -SqlInstance $instance -SqlCredential $SqlCredential } catch { Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue } diff --git a/internal/functions/Connect-ConnstringInstance.ps1 b/internal/functions/Connect-ConnstringInstance.ps1 new file mode 100644 index 0000000000..790dec40ae --- /dev/null +++ b/internal/functions/Connect-ConnstringInstance.ps1 @@ -0,0 +1,63 @@ +function Connect-ConnstringInstance { + <# + .SYNOPSIS + Internal function to establish smo connections using a connstring + + .DESCRIPTION + Internal function to establish smo connections using a connstring + + Can interpret any of the following types of information: + - String + - Smo Server objects + + .PARAMETER SqlInstance + The SQL Server instance to restore to. + + .PARAMETER SqlCredential + Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. + + .PARAMETER AzureUnsupported + Throw if Azure is detected but not supported + + .PARAMETER MinimumVersion + The minimum version that the calling command will support + + .PARAMETER StatementTimeout + Sets the number of seconds a statement is given to run before failing with a timeout error. + + .EXAMPLE + Connect-SqlInstance -SqlInstance sql2014 + + Connect to the Server sql2014 with native credentials. + #> + [CmdletBinding()] + param ( + [Parameter(Mandatory)] + [DbaInstanceParameter]$SqlInstance, + [PSCredential]$SqlCredential, + [int]$StatementTimeout, + [int]$MinimumVersion, + [switch]$AzureUnsupported, + [switch]$NonPooled + ) + if ($SqlInstance.InputObject.GetType().Name -eq 'Server') { + $SqlInstance.InputObject.Refresh() + return $SqlInstance.InputObject + } else { + $boundparams = $PSBoundParameters + [object[]]$connstringcmd = (Get-Command New-DbaConnectionString).Parameters.Keys + [object[]]$connectcmd = (Get-Command Connect-ConnstringInstance).Parameters.Keys + + foreach ($key in $connectcmd) { + if ($key -notin $connstringcmd -and $key -ne "SqlCredential") { + $null = $boundparams.Remove($key) + } + } + # Build connection string + $connstring = New-DbaConnectionString @boundparams -ClientName "dbatools PowerShell module - dbatools.io" + $sqlconn = New-Object System.Data.SqlClient.SqlConnection $connstring + $serverconn = New-Object Microsoft.SqlServer.Management.Common.ServerConnection $sqlconn + $null = $serverconn.Connect() + New-Object Microsoft.SqlServer.Management.Smo.Server $serverconn + } +} \ No newline at end of file