Repository navigation
Expand file tree
/
Copy pathdbatools.code-snippets
More file actions
257 lines (255 loc) 路 10 KB
/
Copy pathdbatools.code-snippets
File metadata and controls
257 lines (255 loc) 路 10 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
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
{
// Starting point for project-level snippets for the dbatools contributions.
// These are just a few and will be exapnded as time goes along.
"Get function for SMO object": {
"scope": "powershell",
"prefix": "getsmo",
"body": [
"function Get-Dba$0 {",
"<#",
"\t.SYNOPSIS",
"\t\t${1:Short of what command does}",
"",
"\t.DESCRIPTION",
"\t\t${2:Longer of what command does}",
"",
"\t.PARAMETER SqlInstance",
"\t\tSQL Server name or SMO object representing the SQL Server to connect to. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.",
"",
"\t.PARAMETER SqlCredential",
"\t\tLogin to the target instance using alternate Windows or SQL Login Authentication. Accepts credential objects (Get-Credential).",
"",
"\t.PARAMETER EnableException",
"\t\tBy default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.",
"\t\tThis avoids overwhelming you with a \"sea of red\" exceptions, but is inconvenient because it basically disables advanced scripting.",
"\t\tUsing this switch turns this \"nice by default\" feature off and enables you to catch exceptions with your own try/catch.",
"",
"\t.NOTES",
"\t\tTags: ${3:Singular word for command index}",
"\t\tAuthor: ${4:FirstName} ${5:LastName} (@${6:TwitterHandle}), ${7:blog address}",
"",
"\t\tWebsite: https://dbatools.io",
"\t\tCopyright: (c) 2018 by dbatools, licensed under MIT",
"\t\tLicense: MIT https://opensource.org/licenses/MIT",
"",
"\t.LINK",
"\t\thttps://dbatools.io/Get-Dba$0",
"",
"\t.EXAMPLE",
"\t\tPS C:\\> Get-Dba$0 -SqlInstance localhost",
"",
"\t\t${8:Add minimum example for each parameter}",
"",
"#>",
"\t[CmdletBinding()]",
"\tparam (",
"\t\t[parameter(Position = 0, Mandatory, ValueFromPipeline)]",
"\t\t[Alias(\"ServerInstance\", \"SqlServer\")]",
"\t\t[DbaInstance[]]\\$SqlInstance,",
"\t\t[Alias(\"Credential\")]",
"\t\t[PSCredential]\\$SqlCredential,",
"\t\t[object[]]\\$${9:YourParameter},",
"\t\t[switch]\\$EnableException",
"\t)",
"\tprocess {",
"\t\tforeach (\\$instance in \\$SqlInstance) {",
"\t\t\tWrite-Message -Level Verbose -Message \"Attempting to connect to \\$instance\"",
"\t\t\ttry {",
"\t\t\t\t\\$server = Connect-SqlInstance -SqlInstance \\$instance -SqlCredential \\$SqlCredential",
"\t\t\t}",
"\t\t\tcatch {",
"\t\t\t\tStop-Function -Message \"Failure\" -Category ConnectionError -ErrorRecord \\$_ -Target \\$instance -Continue",
"\t\t\t}",
"",
"\t\t\t# Add in filter code",
"",
"\t\t\t# Add in processing SMO object",
"",
"\t\t\t<# Output must include 3 properties for each object:",
"\t\t\t\t1. ComputerName (\\$server.ComputerName)",
"\t\t\t\t2. InstanceName (\\$server.ServiceName)",
"\t\t\t\t3. SqlInstance (\\$server.DomainInstanceName)",
"",
"\t\t\t\t\"Select_DefaultView -InputObject \\$object -Property <property list\" can be used to only output certain properties",
"\t\t\t\t\"Select_DefaultView -InputObject \\$object -ExcludeProperty <property list\" can be used to output everything but the list of properties",
"\t\t\t#>",
"\t\t}",
"\t}",
"}"
],
"description": "template for building a function to get properties from SMO object"
},
"Set function": {
"scope": "powershell",
"prefix": "setsmo",
"body": [
"function Set-Dba$0 {",
"<#",
"\t.SYNOPSIS",
"\t\t${1:Short of what command does}",
"",
"\t.DESCRIPTION",
"\t\t${2:Longer of what command does}",
"",
"\t.PARAMETER SqlInstance",
"\t\tSQL Server name or SMO object representing the SQL Server to connect to. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.",
"",
"\t.PARAMETER SqlCredential",
"\t\tLogin to the target instance using alternate Windows or SQL Login Authentication. Accepts credential objects (Get-Credential).",
"",
"\t.PARAMETER EnableException",
"\t\tBy default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.",
"\t\tThis avoids overwhelming you with a \"sea of red\" exceptions, but is inconvenient because it basically disables advanced scripting.",
"\t\tUsing this switch turns this \"nice by default\" feature off and enables you to catch exceptions with your own try/catch.",
"",
"\t.NOTES",
"\t\tTags: ${3:Singular word for command index}",
"\t\tAuthor: ${4:FirstName} ${5:LastName} (@${6:TwitterHandle}), ${7:blog address}",
"",
"\t\tWebsite: https://dbatools.io",
"\t\tCopyright: (c) 2018 by dbatools, licensed under MIT",
"\t\tLicense: MIT https://opensource.org/licenses/MIT",
"",
"\t.LINK",
"\t\thttps://dbatools.io/Set-Dba$0",
"",
"\t.EXAMPLE",
"\t\tPS C:\\> Set-Dba$0 -SqlInstance localhost",
"",
"\t\t${8:Add minimum example for each parameter}",
"",
"#>",
"\t[CmdletBinding(SupportsShouldProcess, ConfirmImpact = \"High\")]",
"\tparam (",
"\t\t[parameter(Position = 0, Mandatory, ValueFromPipeline)]",
"\t\t[Alias(\"ServerInstance\", \"SqlServer\")]",
"\t\t[DbaInstance[]]\\$SqlInstance,",
"\t\t[Alias(\"Credential\")]",
"\t\t[PSCredential]\\$SqlCredential,",
"\t\t[object[]]\\$${9:YourParameters},",
"\t\t[switch]\\$EnableException",
"\t)",
"\tprocess {",
"\t\tforeach (\\$instance in \\$SqlInstance) {",
"\t\t\tWrite-Message -Level Verbose -Message \"Attempting to connect to \\$instance\"",
"\t\t\ttry {",
"\t\t\t\t\\$server = Connect-SqlInstance -SqlInstance \\$instance -SqlCredential \\$SqlCredential",
"\t\t\t}",
"\t\t\tcatch {",
"\t\t\t\tStop-Function -Message \"Failure\" -Category ConnectionError -ErrorRecord \\$_ -Target \\$instance -Continue",
"\t\t\t}",
"",
"\t\t\t# Add code for parameter filters",
"",
"\t\t\tforeach (\\$object in \\$objects) {",
"\t\t\t\tif (\\$Pscmdlet.ShouldProcess(\"\\$object on \\$server\", \"What it will do\")) {",
"\t\t\t\t\t\t# Work to be done",
"",
"\t\t\t\t\t\t[PSCustomObject]@{",
"\t\t\t\t\t\t\tComputerName = \\$server.NetName",
"\t\t\t\t\t\t\tInstanceName = \\$server.ServiceName",
"\t\t\t\t\t\t\tSqlInstance = \\$server.DomainInstanceName",
"\t\t\t\t\t\t\tObject = \\$object.Name",
"\t\t\t\t\t\t\tStatus = \"Dropped/Updated/etc\"",
"\t\t\t\t\t\t}",
"\t\t\t\t\t}",
"\t\t\t}",
"\t\t}",
"\t}",
"}"
],
"description": "template for building a function to set properties from SMO object"
},
"Set function": {
"scope": "powershell",
"prefix": "setsql",
"body": [
"function Set-Dba$0 {",
"<#",
"\t.SYNOPSIS",
"\t\t${1:Short of what command does}",
"",
"\t.DESCRIPTION",
"\t\t${2:Longer of what command does}",
"",
"\t.PARAMETER SqlInstance",
"\t\tSQL Server name or SMO object representing the SQL Server to connect to. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.",
"",
"\t.PARAMETER SqlCredential",
"\t\tLogin to the target instance using alternate Windows or SQL Login Authentication. Accepts credential objects (Get-Credential).",
"",
"\t.PARAMETER EnableException",
"\t\tBy default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.",
"\t\tThis avoids overwhelming you with a \"sea of red\" exceptions, but is inconvenient because it basically disables advanced scripting.",
"\t\tUsing this switch turns this \"nice by default\" feature off and enables you to catch exceptions with your own try/catch.",
"",
"\t.NOTES",
"\t\tTags: ${3:Singular word for command index}",
"\t\tAuthor: ${4:FirstName} ${5:LastName} (@${6:TwitterHandle}), ${7:blog address}",
"",
"\t\tWebsite: https://dbatools.io",
"\t\tCopyright: (c) 2018 by dbatools, licensed under MIT",
"\t\tLicense: MIT https://opensource.org/licenses/MIT",
"",
"\t.LINK",
"\t\thttps://dbatools.io/Set-Dba$0",
"",
"\t.EXAMPLE",
"\t\tPS C:\\> Set-Dba$0 -SqlInstance localhost",
"",
"\t\t${8:Add minimum example for each parameter}",
"",
"#>",
"\t[CmdletBinding(SupportsShouldProcess, ConfirmImpact = \"High\")]",
"\tparam (",
"\t\t[parameter(Position = 0, Mandatory, ValueFromPipeline)]",
"\t\t[Alias(\"ServerInstance\", \"SqlServer\")]",
"\t\t[DbaInstance[]]\\$SqlInstance,",
"\t\t[Alias(\"Credential\")]",
"\t\t[PSCredential]\\$SqlCredential,",
"\t\t[object[]]\\$${9:YourParameters},",
"\t\t[switch]\\$EnableException",
"\t)",
"\tbegin {",
"\t\t\\$sql = \"--FORMART YOUR T-SQL, poorsql.com",
"\t\t\t\\-- build your query based on parameters passed in",
"\t\t\"",
"\t}",
"\tprocess {",
"\t\tforeach (\\$instance in \\$SqlInstance) {",
"\t\t\tWrite-Message -Level Verbose -Message \"Attempting to connect to \\$instance\"",
"\t\t\ttry {",
"\t\t\t\t\\$server = Connect-SqlInstance -SqlInstance \\$instance -SqlCredential \\$SqlCredential",
"\t\t\t}",
"\t\t\tcatch {",
"\t\t\t\tStop-Function -Message \"Failure\" -Category ConnectionError -ErrorRecord \\$_ -Target \\$instance -Continue",
"\t\t\t}",
"",
"\t\t\t# Add code for parameter filters",
"",
"\t\t\tforeach (\\$object in \\$objects) {",
"\t\t\t\ttry {",
"\t\t\t\t\t\\$results = \\$server.Query(\\$sql)",
"\t\t\t\t}",
"\t\t\t\tcatch {",
"\t\t\t\t\tStop-Function -Message \"Something went wrong\" -Continue -Target \\$object -ErrorRecord \\$_",
"\t\t\t\t}",
"\t\t\t\tforeach (\\$row in \\$results) {",
"\t\t\t\t\t# Work to be done",
"",
"\t\t\t\t\t[PSCustomObject]@{",
"\t\t\t\t\t\tComputerName = \\$server.NetName",
"\t\t\t\t\t\tInstanceName = \\$server.ServiceName",
"\t\t\t\t\t\tSqlInstance = \\$server.DomainInstanceName",
"\t\t\t\t\t\tObject = \\$row.ColumnName",
"\t\t\t\t\t\tStatus = \"Dropped/Updated/Whatever\"",
"\t\t\t\t\t}",
"\t\t\t\t}",
"\t\t\t}",
"\t\t}",
"\t}",
"}"
],
"description": "template for building a function to set properties from SMO object"
}
}