forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnect-SqlDscDatabaseEngine.ps1
More file actions
153 lines (116 loc) · 5.85 KB
/
Connect-SqlDscDatabaseEngine.ps1
File metadata and controls
153 lines (116 loc) · 5.85 KB
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
<#
.SYNOPSIS
Connect to a SQL Server Database Engine and return the server object.
.DESCRIPTION
This command connects to a SQL Server Database Engine instance and returns
the Server object.
.PARAMETER ServerName
String containing the host name of the SQL Server to connect to.
Default value is the current computer name.
.PARAMETER InstanceName
String containing the SQL Server Database Engine instance to connect to.
Default value is 'MSSQLSERVER'.
.PARAMETER Credential
The credentials to use to impersonate a user when connecting to the
SQL Server Database Engine instance. If this parameter is left out, then
the current user will be used to connect to the SQL Server Database Engine
instance using Windows Integrated authentication.
.PARAMETER LoginType
Specifies which type of logon credential should be used. The valid types
are 'WindowsUser' or 'SqlLogin'. Default value is 'WindowsUser'
If set to 'WindowsUser' then the it will impersonate using the Windows
login specified in the parameter Credential.
If set to 'SqlLogin' then it will impersonate using the native SQL
login specified in the parameter Credential.
.PARAMETER Protocol
Specifies the network protocol to use when connecting to the SQL Server
instance. Valid values are 'tcp' for TCP/IP, 'np' for Named Pipes,
and 'lpc' for Shared Memory.
If not specified, the connection will use the default protocol order
configured on the client.
.PARAMETER Port
Specifies the TCP port number to use when connecting to the SQL Server
instance. This parameter is only applicable when connecting via TCP/IP.
If not specified for a named instance, the SQL Server Browser service
will be used to determine the port. For default instances, port 1433
is used by default.
.PARAMETER StatementTimeout
Set the query StatementTimeout in seconds. Default 600 seconds (10 minutes).
.PARAMETER Encrypt
Specifies if encryption should be used.
.EXAMPLE
Connect-SqlDscDatabaseEngine
Connects to the default instance on the local server.
.EXAMPLE
Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Connects to the instance 'MyInstance' on the local server.
.EXAMPLE
Connect-SqlDscDatabaseEngine -ServerName 'sql.company.local' -InstanceName 'MyInstance'
Connects to the instance 'MyInstance' on the server 'sql.company.local'.
.EXAMPLE
Connect-SqlDscDatabaseEngine -Credential ([System.Management.Automation.PSCredential]::new('DOMAIN\SqlUser', (ConvertTo-SecureString -String 'MyP@ssw0rd1' -AsPlainText -Force)))
Connects to the default instance on the local server impersonating the Windows user 'DOMAIN\SqlUser'.
.EXAMPLE
Connect-SqlDscDatabaseEngine -LoginType 'SqlLogin' -Credential ([System.Management.Automation.PSCredential]::new('sa', (ConvertTo-SecureString -String 'MyP@ssw0rd1' -AsPlainText -Force)))
Connects to the default instance on the local server using the SQL login 'sa'.
.EXAMPLE
Connect-SqlDscDatabaseEngine -ServerName '192.168.1.1' -InstanceName 'MyInstance' -Protocol 'tcp' -Port 50200
Connects to the named instance 'MyInstance' on server '192.168.1.1' using
TCP/IP on port 50200. The connection string format is 'tcp:192.168.1.1\MyInstance,50200'.
.EXAMPLE
Connect-SqlDscDatabaseEngine -ServerName '192.168.1.1' -Protocol 'tcp' -Port 1433
Connects to the default instance on server '192.168.1.1' using TCP/IP on
port 1433. The connection string format is 'tcp:192.168.1.1,1433'.
.INPUTS
None.
.OUTPUTS
`Microsoft.SqlServer.Management.Smo.Server`
Returns the SQL Server server object.
.NOTES
The protocol values ('tcp', 'np', 'lpc') are lowercase to match the SQL
Server connection string prefix format, e.g., 'tcp:ServerName\Instance,Port'.
#>
function Connect-SqlDscDatabaseEngine
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when the output type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
[OutputType([Microsoft.SqlServer.Management.Smo.Server])]
[CmdletBinding(DefaultParameterSetName = 'SqlServer')]
param
(
[Parameter(ParameterSetName = 'SqlServer')]
[Parameter(ParameterSetName = 'SqlServerWithCredential')]
[ValidateNotNull()]
[System.String]
$ServerName = (Get-ComputerName),
[Parameter(ParameterSetName = 'SqlServer')]
[Parameter(ParameterSetName = 'SqlServerWithCredential')]
[ValidateNotNull()]
[System.String]
$InstanceName = 'MSSQLSERVER',
[Parameter(ParameterSetName = 'SqlServerWithCredential', Mandatory = $true)]
[ValidateNotNull()]
[Alias('SetupCredential', 'DatabaseCredential')]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(ParameterSetName = 'SqlServerWithCredential')]
[ValidateSet('WindowsUser', 'SqlLogin')]
[System.String]
$LoginType = 'WindowsUser',
[Parameter()]
[ValidateSet('tcp', 'np', 'lpc')]
[System.String]
$Protocol,
[Parameter()]
[System.UInt16]
$Port,
[Parameter()]
[ValidateNotNull()]
[System.Int32]
$StatementTimeout = 600,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Encrypt
)
# Call the private function.
return (Connect-Sql @PSBoundParameters)
}