1
+ # Failsafe
2
+ break
3
+
4
+
5
+ # <Will be removed in final slide, need to update my profile (including prompt)>
6
+ # Importing dbatools so it all will work
7
+ # Import-Module dbatools
8
+ # . .\importinternals.ps1
9
+
10
+
11
+
12
+
13
+
14
+
15
+ # ----------------------------------------------------------------------------#
16
+ # Drowning users in a sea of blood ... not #
17
+ # ----------------------------------------------------------------------------#
18
+
19
+ # This is going to be bloody
20
+ Get-ChildItem C:\doesntexist
21
+
22
+ # So is this
23
+ throw " Some error happened"
24
+
25
+ # This is ... not going to be bloody. But bloody useless for scripting!
26
+ try { Write-Warning " Some error happened" }
27
+ catch { " Error reaction incoming ... not!" }
28
+
29
+ # Make it optional
30
+ try { throw " Some error happened" }
31
+ catch {
32
+ if ($ThrowExceptions ) { throw }
33
+ else { Write-Warning $_ }
34
+ }
35
+ # Opt in
36
+ $ThrowExceptions = $true
37
+
38
+ # ####
39
+ # Works, but messy
40
+
41
+ # Solution
42
+ $EnableException = $false
43
+ Stop-Function - Message " Some error happened"
44
+ $EnableException = $true
45
+ Stop-Function - Message " Some error happened"
46
+
47
+ # ####
48
+ # Demo function
49
+ # TODO: Replace with real functionality
50
+ function Get-Test {
51
+ [CmdletBinding ()]
52
+ Param (
53
+ [Parameter (ValueFromPipeline = $true )]
54
+ [int []]
55
+ $Numbers ,
56
+ [switch ]$Foo ,
57
+ [switch ]$Bar ,
58
+ [switch ]$EnableException
59
+ )
60
+ begin {
61
+ if ($Foo ) {
62
+ Stop-Function - Message " Failing as ordered to"
63
+ return
64
+ }
65
+ }
66
+ process {
67
+ if (Test-FunctionInterrupt ) { return }
68
+
69
+ foreach ($number in $Numbers ) {
70
+ if (($number -eq 2 ) -and ($Bar )) {
71
+ try { Get-DbaBackupHistory - SqlInstance . - SqlCredential $cred - EnableException }
72
+ catch { Stop-Function - Message " Failing" - ErrorRecord $_ - Continue }
73
+ }
74
+ $number
75
+ }
76
+ }
77
+ }
78
+ # No errors wanted
79
+ 1 .. 3 | Get-Test
80
+ # Kill it in begin
81
+ 1 .. 3 | Get-Test - Foo
82
+ # Need more blood
83
+ 1 .. 3 | Get-Test - Foo - EnableException
84
+ # Don't waste it!
85
+ try { 1 .. 3 | Get-Test - Foo - EnableException }
86
+ catch { " Something broke" }
87
+
88
+ # Killing in process
89
+ 1 .. 3 | Get-Test - Bar
90
+ 1 .. 3 | Get-Test - Bar - EnableException
91
+
92
+
93
+ # ----------------------------------------------------------------------------#
94
+ # Configuration #
95
+ # ----------------------------------------------------------------------------#
96
+
97
+ <#
98
+ #TODO: Remove before presentation
99
+ Basic Issue: Growing option Complexity as command meta-level rises
100
+ functionA calls functionB calls functionC calls functionD
101
+ - Pass through all parameters?
102
+ --> Either provide incredibly complex parameters or choose for the user.
103
+ --> Not every choice is right for everybody
104
+ - How do you control behavior that doesn't have a command?
105
+
106
+ --> Need for options separate from commands
107
+ #>
108
+
109
+ # PSReadline has options in dedicated command
110
+ Get-PSReadlineOption
111
+
112
+ <#
113
+ Issue: The more options, the less usable a command with parameters for each of them
114
+
115
+ Solution:
116
+ - Option as parameter value, not parameter
117
+ #>
118
+
119
+ Get-DbaConfig
120
+ Get-DbaConfig | Out-GridView
121
+
122
+ <#
123
+ To note:
124
+ - Supports option documentation
125
+ - Supports input validation
126
+ - Supports reacting to setting changes
127
+ #>
128
+
129
+ $paramSetDbaConfig = @ {
130
+ FullName = ' sql.connection.encrypt'
131
+ Value = $true
132
+ Initialize = $true # Only during module definition
133
+ Validation = ' bool'
134
+ Handler = { Write-Host (" Setting SQL connection encryption to: {0}" -f $args [0 ]) }
135
+ Description = " Whether SQL connections should be encrypted. Don't disable unless you REALLY must."
136
+ }
137
+
138
+ Set-DbaConfig @paramSetDbaConfig
139
+ Get-DbaConfig ' sql.connection.encrypt'
140
+ Set-DbaConfig ' sql.connection.encrypt' " foo"
141
+ Set-DbaConfig ' sql.connection.encrypt' $false
142
+
143
+ # TODO: Implement in Connect-SqlInstance (Internal connection function)
144
+ Get-DbaConfigValue - FullName ' sql.connection.encrypt'
145
+
146
+ Set-DbaConfig ' sql.connection.encrypt' $true
147
+
148
+ <#
149
+ Notes on implementation:
150
+ - Available in all runspaaces
151
+ - Scales well
152
+ - Easy to split configuration definition in to logic groups
153
+ #>
154
+
155
+ <#
156
+ Additional features:
157
+ - Settings can be persisted per user or per machine
158
+ - Can be controlled via GPO/DSC/SCCM
159
+ #>
160
+
161
+
162
+ # ----------------------------------------------------------------------------#
163
+ # Logging #
164
+ # ----------------------------------------------------------------------------#
165
+
166
+ # Dbatools logs quite a bit
167
+ Get-DbaConfigValue - FullName ' path.dbatoolslogpath' | Invoke-Item
168
+
169
+ New-DbatoolsSupportPackage
170
+
171
+ <#
172
+ Challenges:
173
+ - Performance
174
+ - Size
175
+ - Usability & integration
176
+ - Access conflicts
177
+ #>
178
+
179
+ # Usability: Building on the known
180
+ # ---------------------------------
181
+
182
+ # Bad
183
+ Write-Verbose - Message " Something" - Verbose
184
+
185
+ # Good
186
+ Write-Message - Level Verbose - Message " Something" - Verbose
187
+ Write-Message - Level SomewhatVerbose - Message " Something" - Verbose
188
+
189
+ <#
190
+ Levels:
191
+ Critical, Important, Output, Significant, VeryVerbose, Verbose, SomewhatVerbose, System, Debug, InternalComment, Warning
192
+ #>
193
+ Write-Message - Level VeryVerbose - Message " Something"
194
+ Set-DbaConfig ' message.maximuminfo' 4
195
+ Write-Message - Level VeryVerbose - Message " Something"
196
+ # TODO: Fix this thing, then kill TODO
197
+
198
+ # Performance
199
+ # ------------
200
+
201
+ Get-Runspace | ft - AutoSize
202
+
203
+ # Avoid duplication through C# static management
204
+ Get-DbaRunspace
205
+
206
+ # Script doing the actual logging
207
+ # TODO: Fix path
208
+ code " D:\Code\Github\dbatools\internal\scripts\logfilescript.ps1"
209
+
210
+
211
+ # Size
212
+ # -----
213
+
214
+ Get-DbaConfig logging.*
215
+
216
+
217
+ # Integrated rotate
218
+
219
+
220
+ # Access Conflicts
221
+ # -----------------
222
+
223
+ <#
224
+ - One writing thread per process
225
+ - Output files named for computer and process ID
226
+ #>
227
+
228
+
229
+ # The Other Things
230
+ # -----------------
231
+
232
+ # Forensics!
233
+ Get-DbatoolsLog | Out-GridView
234
+ Write-Message - Level Verbose - Message " Something new"
235
+
236
+
237
+ # ----------------------------------------------------------------------------#
238
+ # DbaInstance Parameter class #
239
+ # ----------------------------------------------------------------------------#
240
+
241
+ <#
242
+ Original Situation:
243
+ - Accept anything, then interpret
244
+ - Some contributors / team members would just assume string
245
+ - Non-uniform validations
246
+ --> Unmanaged madness
247
+ #>
248
+
249
+ <#
250
+ Challenge:
251
+ - Uniform user experience on input
252
+ - Validation overhead
253
+ - Converting input from multiple sources
254
+ - Passing through live connections
255
+ - Keeping it usable for average contributors!!
256
+ #>
257
+
258
+ <#
259
+ Answer: Parameter Classes
260
+ #>
261
+ [DbaInstance ]" foo"
262
+ [DbaInstance ]" ."
263
+ [DbaInstance ]" foo\bar"
264
+ [DbaInstance ]" Server=foo\bar;"
265
+ [DbaInstance ]" (localdb)\foo"
266
+ [DbaInstance ]([System.Net.DNS ]::GetHostEntry(" localhost" ))
267
+ [DbaInstance ](Get-ADComputer " Odin" ) # TODO: Set up setup to include connection to AD VM
268
+ [DbaInstance ](Connect-DbaInstance - SqlInstance localhost)
269
+ [DbaInstance ]" foo bar"
270
+ [DbaInstance ]" foo\select"
271
+
272
+ <#
273
+ - Conversion/interpretation as parameter binding
274
+ - Pass through original input
275
+ - Validation as parameter binding
276
+
277
+ All the contributors need to do is replace [string] or [object] with [DbaInstance]
278
+
279
+ Additional benefit:
280
+ - Scales exquisitely well: 30 Minutes of work had 380 commands accept localdb
281
+ as input and work correctly against it.
282
+ #>
283
+
284
+
285
+ # ----------------------------------------------------------------------------#
286
+ # Import Sequence & Tuning #
287
+ # ----------------------------------------------------------------------------#
288
+
289
+ # TODO: Kill (Guide through structure) <-- Probably no time
290
+
291
+ <#
292
+ - Parallel import of
293
+ -- Functions
294
+ -- Libraries
295
+ -- Configurations
296
+
297
+ - Off-Load of
298
+ -- Tab Completion
299
+
300
+ - Measuring each step
301
+ #>
302
+
303
+ <#
304
+ Import Options
305
+ - Dot Sourcing (Import Speed)
306
+ - Copy DLL files before import (To support update function on legacy systems without Side-by-Side)
307
+ - Always Compile (Compile library on import; For devs)
308
+ - Serial Import (Slower import, less resource spike)
309
+ #>
310
+
311
+ [SqlCollaborative.Dbatools.dbaSystem.DebugHost ]::ImportTime | Out-GridView
0 commit comments