forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnect-DbaInstance.ps1
519 lines (429 loc) · 28 KB
/
Connect-DbaInstance.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
function Connect-DbaInstance {
<#
.SYNOPSIS
Creates a robust SMO SQL Server object.
.DESCRIPTION
This command is robust because it initializes properties that do not cause enumeration by default. It also supports both Windows and SQL Server authentication methods, and detects which to use based upon the provided credentials.
By default, this command also sets the connection's ApplicationName property to "dbatools PowerShell module - dbatools.io - custom connection". If you're doing anything that requires profiling, you can look for this client name.
Alternatively, you can pass in whichever client name you'd like using the -ClientName parameter. There are a ton of other parameters for you to explore as well.
See https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
and https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx,
and https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx
To execute SQL commands, you can use $server.ConnectionContext.ExecuteReader($sql) or $server.Databases['master'].ExecuteNonQuery($sql)
.PARAMETER SqlInstance
The target SQL Server instance or instances. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.
.PARAMETER SqlCredential
Credential object used to connect to the SQL Server Instance as a different user. This can be a Windows or SQL Server account. Windows users are determined by the existence of a backslash, so if you are intending to use an alternative Windows connection instead of a SQL login, ensure it contains a backslash.
.PARAMETER Database
The database(s) to process. This list is auto-populated from the server.
.PARAMETER AccessToken
Gets or sets the access token for the connection.
.PARAMETER AppendConnectionString
Appends to the current connection string. Note that you cannot pass authentication information using this method. Use -SqlInstance and optionally -SqlCredential to set authentication information.
.PARAMETER ApplicationIntent
Declares the application workload type when connecting to a server.
Valid values are "ReadOnly" and "ReadWrite".
.PARAMETER BatchSeparator
A string to separate groups of SQL statements being executed. By default, this is "GO".
.PARAMETER ClientName
By default, this command sets the client's ApplicationName property to "dbatools PowerShell module - dbatools.io - custom connection" if you're doing anything that requires profiling, you can look for this client name. Using -ClientName allows you to set your own custom client application name.
.PARAMETER ConnectTimeout
The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.
Valid values are integers between 0 and 2147483647.
When opening a connection to a Azure SQL Database, set the connection timeout to 30 seconds.
.PARAMETER EncryptConnection
If this switch is enabled, SQL Server uses SSL encryption for all data sent between the client and server if the server has a certificate installed.
For more information, see Connection String Syntax. https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-string-syntax
Beginning in .NET Framework 4.5, when TrustServerCertificate is false and Encrypt is true, the server name (or IP address) in a SQL Server SSL certificate must exactly match the server name (or IP address) specified in the connection string. Otherwise, the connection attempt will fail. For information about support for certificates whose subject starts with a wildcard character (*), see Accepted wildcards used by server certificates for server authentication. https://support.microsoft.com/en-us/help/258858/accepted-wildcards-used-by-server-certificates-for-server-authenticati
.PARAMETER FailoverPartner
The name of the failover partner server where database mirroring is configured.
If the value of this key is "" (an empty string), then Initial Catalog must be present in the connection string, and its value must not be "".
The server name can be 128 characters or less.
If you specify a failover partner but the failover partner server is not configured for database mirroring and the primary server (specified with the Server keyword) is not available, then the connection will fail.
If you specify a failover partner and the primary server is not configured for database mirroring, the connection to the primary server (specified with the Server keyword) will succeed if the primary server is available.
.PARAMETER LockTimeout
Sets the time in seconds required for the connection to time out when the current transaction is locked.
.PARAMETER MaxPoolSize
Sets the maximum number of connections allowed in the connection pool for this specific connection string.
.PARAMETER MinPoolSize
Sets the minimum number of connections allowed in the connection pool for this specific connection string.
.PARAMETER MultipleActiveResultSets
If this switch is enabled, an application can maintain multiple active result sets (MARS).
If this switch is not enabled, an application must process or cancel all result sets from one batch before it can execute any other batch on that connection.
.PARAMETER MultiSubnetFailover
If this switch is enabled, and your application is connecting to an AlwaysOn availability group (AG) on different subnets, detection of and connection to the currently active server will be faster. For more information about SqlClient support for Always On Availability Groups, see https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/sqlclient-support-for-high-availability-disaster-recovery
.PARAMETER NetworkProtocol
Explicitly sets the network protocol used to connect to the server.
Valid values are "TcpIp","NamedPipes","Multiprotocol","AppleTalk","BanyanVines","Via","SharedMemory" and "NWLinkIpxSpx"
.PARAMETER NonPooledConnection
If this switch is enabled, a non-pooled connection will be requested.
.PARAMETER PacketSize
Sets the size in bytes of the network packets used to communicate with an instance of SQL Server. Must match at server.
.PARAMETER PooledConnectionLifetime
When a connection is returned to the pool, its creation time is compared with the current time and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online.
A value of zero (0) causes pooled connections to have the maximum connection timeout.
.PARAMETER SqlExecutionModes
The SqlExecutionModes enumeration contains values that are used to specify whether the commands sent to the referenced connection to the server are executed immediately or saved in a buffer.
Valid values include "CaptureSql", "ExecuteAndCaptureSql" and "ExecuteSql".
.PARAMETER StatementTimeout
Sets the number of seconds a statement is given to run before failing with a timeout error.
.PARAMETER TrustServerCertificate
When this switch is enabled, the channel will be encrypted while bypassing walking the certificate chain to validate trust.
.PARAMETER WorkstationId
Sets the name of the workstation connecting to SQL Server.
.PARAMETER SqlConnectionOnly
Instead of returning a rich SMO server object, this command will only return a SqlConnection object when setting this switch.
.PARAMETER AzureUnsupported
Throw if Azure is detected but not supported
.PARAMETER MinimumVersion
Throw if the target SQL Server instance version does not meet version requirements
.PARAMETER DisableException
By default in most of our commands, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This command, however, gifts you with "sea of red" exceptions, by default, because it is useful for advanced scripting.
Using this switch turns our "nice by default" feature on which makes errors into pretty warnings.
.NOTES
Tags: Connect, Connection
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Connect-DbaInstance
.EXAMPLE
PS C:\> Connect-DbaInstance -SqlInstance sql2014
Creates an SMO Server object that connects using Windows Authentication
.EXAMPLE
PS C:\> $wincred = Get-Credential ad\sqladmin
PS C:\> Connect-DbaInstance -SqlInstance sql2014 -SqlCredential $wincred
Creates an SMO Server object that connects using alternative Windows credentials
.EXAMPLE
PS C:\> $sqlcred = Get-Credential sqladmin
PS C:\> $server = Connect-DbaInstance -SqlInstance sql2014 -SqlCredential $sqlcred
Login to sql2014 as SQL login sqladmin.
.EXAMPLE
PS C:\> $server = Connect-DbaInstance -SqlInstance sql2014 -ClientName "my connection"
Creates an SMO Server object that connects using Windows Authentication and uses the client name "my connection". So when you open up profiler or use extended events, you can search for "my connection".
.EXAMPLE
PS C:\> $server = Connect-DbaInstance -SqlInstance sql2014 -AppendConnectionString "Packet Size=4096;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;User Instance=true;"
Creates an SMO Server object that connects to sql2014 using Windows Authentication, then it sets the packet size (this can also be done via -PacketSize) and other connection attributes.
.EXAMPLE
PS C:\> $server = Connect-DbaInstance -SqlInstance sql2014 -NetworkProtocol TcpIp -MultiSubnetFailover
Creates an SMO Server object that connects using Windows Authentication that uses TCP/IP and has MultiSubnetFailover enabled.
.EXAMPLE
PS C:\> $server = Connect-DbaInstance sql2016 -ApplicationIntent ReadOnly
Connects with ReadOnly ApplicationIntent.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[Alias("ServerInstance", "SqlServer")]
[DbaInstanceParameter[]]$SqlInstance,
[Alias("Credential")]
[PSCredential]$SqlCredential,
[string]$Database,
[string]$AccessToken,
[ValidateSet('ReadOnly', 'ReadWrite')]
[string]$ApplicationIntent,
[switch]$AzureUnsupported,
[string]$BatchSeparator,
[string]$ClientName = "dbatools PowerShell module - dbatools.io - custom connection",
[int]$ConnectTimeout = ([Sqlcollaborative.Dbatools.Connection.ConnectionHost]::SqlConnectionTimeout),
[switch]$EncryptConnection,
[string]$FailoverPartner,
[int]$LockTimeout,
[int]$MaxPoolSize,
[int]$MinPoolSize,
[int]$MinimumVersion,
[switch]$MultipleActiveResultSets,
[switch]$MultiSubnetFailover,
[ValidateSet('TcpIp', 'NamedPipes', 'Multiprotocol', 'AppleTalk', 'BanyanVines', 'Via', 'SharedMemory', 'NWLinkIpxSpx')]
[string]$NetworkProtocol,
[switch]$NonPooledConnection,
[int]$PacketSize,
[int]$PooledConnectionLifetime,
[ValidateSet('CaptureSql', 'ExecuteAndCaptureSql', 'ExecuteSql')]
[string]$SqlExecutionModes,
[int]$StatementTimeout,
[switch]$TrustServerCertificate,
[string]$WorkstationId,
[string]$AppendConnectionString,
[switch]$SqlConnectionOnly,
[switch]$DisableException
)
begin {
if ($DisableException) {
$EnableException = $false
} else {
$EnableException = $true
}
Test-DbaDeprecation -DeprecatedOn "1.0.0" -EnableException:$false -Alias Connect-DbaServer
Test-DbaDeprecation -DeprecatedOn "1.0.0" -EnableException:$false -Alias Get-DbaInstance
$loadedSmoVersion = [AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {
$_.Fullname -like "Microsoft.SqlServer.SMO,*"
}
if ($loadedSmoVersion) {
$loadedSmoVersion = $loadedSmoVersion | ForEach-Object {
if ($_.Location -match "__") {
((Split-Path (Split-Path $_.Location) -Leaf) -split "__")[0]
} else {
((Get-ChildItem -Path $_.Location).VersionInfo.ProductVersion)
}
}
}
if ($MinimumVersion -and $server.VersionMajor) {
if ($server.versionMajor -lt $MinimumVersion) {
throw "SQL Server version $MinimumVersion required - $server not supported."
}
}
if ($AzureUnsupported -and $server.DatabaseEngineType -eq "SqlAzureDatabase") {
throw "Azure SQL Database not supported"
}
#'PrimaryFilePath' seems the culprit for slow SMO on databases
$Fields2000_Db = 'Collation', 'CompatibilityLevel', 'CreateDate', 'ID', 'IsAccessible', 'IsFullTextEnabled', 'IsSystemObject', 'IsUpdateable', 'LastBackupDate', 'LastDifferentialBackupDate', 'LastLogBackupDate', 'Name', 'Owner', 'ReadOnly', 'RecoveryModel', 'ReplicationOptions', 'Status', 'Version'
$Fields200x_Db = $Fields2000_Db + @('BrokerEnabled', 'DatabaseSnapshotBaseName', 'IsMirroringEnabled', 'Trustworthy')
$Fields201x_Db = $Fields200x_Db + @('ActiveConnections', 'AvailabilityDatabaseSynchronizationState', 'AvailabilityGroupName', 'ContainmentType', 'EncryptionEnabled')
$Fields2000_Login = 'CreateDate', 'DateLastModified', 'DefaultDatabase', 'DenyWindowsLogin', 'IsSystemObject', 'Language', 'LanguageAlias', 'LoginType', 'Name', 'Sid', 'WindowsLoginAccessType'
$Fields200x_Login = $Fields2000_Login + @('AsymmetricKey', 'Certificate', 'Credential', 'ID', 'IsDisabled', 'IsLocked', 'IsPasswordExpired', 'MustChangePassword', 'PasswordExpirationEnabled', 'PasswordPolicyEnforced')
$Fields201x_Login = $Fields200x_Login + @('PasswordHashAlgorithm')
}
process {
foreach ($instance in $SqlInstance) {
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
$boundparams = $PSBoundParameters
[object[]]$connstringcmd = (Get-Command New-DbaConnectionString).Parameters.Keys
[object[]]$connectcmd = (Get-Command Connect-DbaInstance).Parameters.Keys
foreach ($key in $connectcmd) {
if ($key -notin $connstringcmd -and $key -ne "SqlCredential") {
$null = $boundparams.Remove($key)
}
}
$azureconnstring = New-DbaConnectionString @boundparams
try {
$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
} catch {
Stop-Function -Message "Failure" -ErrorRecord $_ -Continue
}
}
#region Safely convert input into instance parameters
if ($instance.GetType() -eq [Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter]) {
[DbaInstanceParameter]$ConvertedSqlInstance = $instance
if ($ConvertedSqlInstance.Type -like "SqlConnection") {
[DbaInstanceParameter]$ConvertedSqlInstance = New-Object Microsoft.SqlServer.Management.Smo.Server($ConvertedSqlInstance.InputObject)
}
} else {
[DbaInstanceParameter]$ConvertedSqlInstance = [DbaInstanceParameter]($instance | Select-Object -First 1)
if ($instance.Count -gt 1) {
Write-Message -Continue -Level Warning -EnableException:$EnableException -Message "More than on server was specified when calling Connect-SqlInstance from $((Get-PSCallStack)[1].Command)"
}
}
#endregion Safely convert input into instance parameters
if ($instance.Type -like "Server" -or ($isAzure -and $instance.InputObject.ConnectionContext.IsOpen)) {
if ($instance.InputObject.ConnectionContext.IsOpen -eq $false) {
$instance.InputObject.ConnectionContext.Connect()
}
if ($SqlConnectionOnly) {
return $instance.InputObject.ConnectionContext.SqlConnectionObject
} else {
return $instance.InputObject
}
}
if ($instance.Type -like "SqlConnection") {
$server = New-Object Microsoft.SqlServer.Management.Smo.Server($instance.InputObject)
if ($server.ConnectionContext.IsOpen -eq $false) {
$server.ConnectionContext.Connect()
}
if ($SqlConnectionOnly) {
return $server.ConnectionContext.SqlConnectionObject
} else {
if (-not $server.ComputerName) {
if (-not $server.NetName -or $SqlInstance -match '\.') {
$parsedcomputername = $ConvertedSqlInstance.ComputerName
} else {
$parsedcomputername = $server.NetName
}
Add-Member -InputObject $server -NotePropertyName ComputerName -NotePropertyValue $parsedcomputername -Force
}
return $server
}
}
if ($instance.IsConnectionString) {
$server = New-Object Microsoft.SqlServer.Management.Smo.Server($instance.InputObject)
} elseif (-not $isAzure) {
$server = New-Object Microsoft.SqlServer.Management.Smo.Server $instance.FullSmoName
}
if ($AppendConnectionString) {
$connstring = $server.ConnectionContext.ConnectionString
$server.ConnectionContext.ConnectionString = "$connstring;$appendconnectionstring"
$server.ConnectionContext.Connect()
} elseif (-not $isAzure) {
$server.ConnectionContext.ApplicationName = $ClientName
if (Test-Bound -ParameterName 'AccessToken') {
$server.ConnectionContext.AccessToken = $AccessToken
}
if (Test-Bound -ParameterName 'BatchSeparator') {
$server.ConnectionContext.BatchSeparator = $BatchSeparator
}
if (Test-Bound -ParameterName 'ConnectTimeout') {
$server.ConnectionContext.ConnectTimeout = $ConnectTimeout
}
if (Test-Bound -ParameterName 'Database') {
$server.ConnectionContext.DatabaseName = $Database
}
if (Test-Bound -ParameterName 'EncryptConnection') {
$server.ConnectionContext.EncryptConnection = $true
}
if (Test-Bound -ParameterName 'LockTimeout') {
$server.ConnectionContext.LockTimeout = $LockTimeout
}
if (Test-Bound -ParameterName 'MaxPoolSize') {
$server.ConnectionContext.MaxPoolSize = $MaxPoolSize
}
if (Test-Bound -ParameterName 'MinPoolSize') {
$server.ConnectionContext.MinPoolSize = $MinPoolSize
}
if (Test-Bound -ParameterName 'MultipleActiveResultSets') {
$server.ConnectionContext.MultipleActiveResultSets = $true
}
if (Test-Bound -ParameterName 'NetworkProtocol') {
$server.ConnectionContext.NetworkProtocol = $NetworkProtocol
}
if (Test-Bound -ParameterName 'NonPooledConnection') {
$server.ConnectionContext.NonPooledConnection = $true
}
if (Test-Bound -ParameterName 'PacketSize') {
$server.ConnectionContext.PacketSize = $PacketSize
}
if (Test-Bound -ParameterName 'PooledConnectionLifetime') {
$server.ConnectionContext.PooledConnectionLifetime = $PooledConnectionLifetime
}
if (Test-Bound -ParameterName 'StatementTimeout') {
$server.ConnectionContext.StatementTimeout = $StatementTimeout
}
if (Test-Bound -ParameterName 'SqlExecutionModes') {
$server.ConnectionContext.SqlExecutionModes = $SqlExecutionModes
}
if (Test-Bound -ParameterName 'TrustServerCertificate') {
$server.ConnectionContext.TrustServerCertificate = $true
}
if (Test-Bound -ParameterName 'WorkstationId') {
$server.ConnectionContext.WorkstationId = $WorkstationId
}
if (Test-Bound -ParameterName 'ApplicationIntent') {
$server.ConnectionContext.ApplicationIntent = $ApplicationIntent
}
$connstring = $server.ConnectionContext.ConnectionString
if (Test-Bound -ParameterName 'MultiSubnetFailover') {
$connstring = "$connstring;MultiSubnetFailover=True"
}
if (Test-Bound -ParameterName 'FailoverPartner') {
$connstring = "$connstring;Failover Partner=$FailoverPartner"
}
if ($connstring -ne $server.ConnectionContext.ConnectionString) {
$server.ConnectionContext.ConnectionString = $connstring
}
try {
if ($null -ne $SqlCredential.UserName -and -not $isAzure) {
$username = ($SqlCredential.UserName).TrimStart("\")
# support both ad\username and username@ad
if ($username -like "*\*" -or $username -like "*@*") {
if ($username -like "*\*") {
$domain, $login = $username.Split("\")
$authtype = "Windows Authentication with Credential"
if ($domain) {
$formatteduser = "$login@$domain"
} else {
$formatteduser = $username.Split("\")[1]
}
} else {
$formatteduser = $SqlCredential.UserName
}
$server.ConnectionContext.LoginSecure = $true
$server.ConnectionContext.ConnectAsUser = $true
$server.ConnectionContext.ConnectAsUserName = $formatteduser
$server.ConnectionContext.ConnectAsUserPassword = ($SqlCredential).GetNetworkCredential().Password
} else {
$authtype = "SQL Authentication"
$server.ConnectionContext.LoginSecure = $false
$server.ConnectionContext.set_Login($username)
$server.ConnectionContext.set_SecurePassword($SqlCredential.Password)
}
}
if ($NonPooled) {
$server.ConnectionContext.Connect()
} elseif ($authtype -eq "Windows Authentication with Credential") {
# Make it connect in a natural way, hard to explain.
$null = $server.Information.Version
if ($server.ConnectionContext.IsOpen -eq $false) {
$server.ConnectionContext.Connect()
}
} else {
if (-not $isAzure) {
$server.ConnectionContext.SqlConnectionObject.Open()
}
}
} catch {
$originalException = $_.Exception
try {
$message = $originalException.InnerException.InnerException.ToString()
} catch {
$message = $originalException.ToString()
}
$message = ($message -Split '-->')[0]
$message = ($message -Split 'at System.Data.SqlClient')[0]
$message = ($message -Split 'at System.Data.ProviderBase')[0]
Stop-Function -Message "Can't connect to $instance" -ErrorRecord $_ -Continue
}
}
if ($loadedSmoVersion -ge 11 -and -not $isAzure) {
if ($server.VersionMajor -eq 8) {
# 2000
$initFieldsDb = New-Object System.Collections.Specialized.StringCollection
[void]$initFieldsDb.AddRange($Fields2000_Db)
$initFieldsLogin = New-Object System.Collections.Specialized.StringCollection
[void]$initFieldsLogin.AddRange($Fields2000_Login)
$server.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database], $initFieldsDb)
$server.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Login], $initFieldsLogin)
} elseif ($server.VersionMajor -eq 9 -or $server.VersionMajor -eq 10) {
# 2005 and 2008
$initFieldsDb = New-Object System.Collections.Specialized.StringCollection
[void]$initFieldsDb.AddRange($Fields200x_Db)
$initFieldsLogin = New-Object System.Collections.Specialized.StringCollection
[void]$initFieldsLogin.AddRange($Fields200x_Login)
$server.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database], $initFieldsDb)
$server.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Login], $initFieldsLogin)
} else {
# 2012 and above
$initFieldsDb = New-Object System.Collections.Specialized.StringCollection
[void]$initFieldsDb.AddRange($Fields201x_Db)
$initFieldsLogin = New-Object System.Collections.Specialized.StringCollection
[void]$initFieldsLogin.AddRange($Fields201x_Login)
$server.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Database], $initFieldsDb)
$server.SetDefaultInitFields([Microsoft.SqlServer.Management.Smo.Login], $initFieldsLogin)
}
}
if ($SqlConnectionOnly) {
return $server.ConnectionContext.SqlConnectionObject
} else {
if (-not $server.ComputerName) {
if (-not $server.NetName -or $SqlInstance -match '\.') {
$parsedcomputername = $ConvertedSqlInstance.ComputerName
} else {
$parsedcomputername = $server.NetName
}
Add-Member -InputObject $server -NotePropertyName ComputerName -NotePropertyValue $parsedcomputername -Force
}
}
if ($isAzure -and $server.ServerType -ne 'SqlAzureDatabase') {
throw "Azure connection failed. The username or password may be incorrect."
}
$server
}
}
}