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
112 lines (86 loc) · 4.28 KB
/
Connect-SqlDscDatabaseEngine.ps1
File metadata and controls
112 lines (86 loc) · 4.28 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
<#
.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 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'.
.INPUTS
None.
.OUTPUTS
`Microsoft.SqlServer.Management.Smo.Server`
Returns the SQL Server server object.
#>
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()]
[ValidateNotNull()]
[System.Int32]
$StatementTimeout = 600,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Encrypt
)
# Call the private function.
return (Connect-Sql @PSBoundParameters)
}