-
Notifications
You must be signed in to change notification settings - Fork 22
/
Snippets List.ps1
332 lines (297 loc) · 7.89 KB
/
Snippets List.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
## A list of snippets
$snips = Get-IseSnippet
# Create a new snippet snippet!
if(!$snips.Where{$_.Name -like 'New-Snippet*'})
{
$snippet1 = @{
Title = 'New-Snippet'
Description = 'Create a New Snippet'
Text = @"
`$snippet = @{
Title = `'Put Title Here`'
Description = `'Description Here`'
Text = @`"
Code in Here
`"@
}
New-IseSnippet @snippet
"@
}
New-IseSnippet @snippet1 –Force
}
# SMO Snippet
if(!$snips.Where{$_.Name -like 'SMO-Server*'})
{
$snippet = @{
Title = 'SMO-Server'
Description = 'Creates a SQL Server SMO Object'
Text = @"
`$srv = New-Object Microsoft.SqlServer.Management.Smo.Server `$Server
"@
}
New-IseSnippet @snippet
}
## Data table snippet
if(!$snips.Where{$_.Name -like 'New-DataTable*'})
{
$snippet = @{
Title = 'New-DataTable'
Description = 'Creates a Data Table Object'
Text = @"
# Create Table Object
`$table = New-Object system.Data.DataTable `$TableName
# Create Columns
`$col1 = New-Object system.Data.DataColumn NAME1,([string])
`$col2 = New-Object system.Data.DataColumn NAME2,([decimal])
#Add the Columns to the table
`$table.columns.add(`$col1)
`$table.columns.add(`$col2)
# Create a new Row
`$row = `$table.NewRow()
# Add values to new row
`$row.Name1 = 'VALUE'
`$row.NAME2 = 'VALUE'
#Add new row to table
`$table.Rows.Add(`$row)
"@
}
New-IseSnippet @snippet
}
#formatted duration snippet
if(!$snips.Where{$_.Name -like 'Formatted Duration*'})
{
$snippet = @{
Title = 'Formatted Duration'
Description = 'Formats Get-SQLAgentJobHistory into timespan'
Text = @"
`$FormattedDuration = @{
Name = 'FormattedDuration'
Expression = {
[timespan]`$_.RunDuration.ToString().PadLeft(6,'0').insert(4,':').insert(2,':')
}
}
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'Prompt for*'})
{
$snippet = @{
Title = 'Prompt for input'
Description = 'Simple way of gathering input from users with simple yes and no'
Text = @"
# Get some input from users
`$title = "Put your Title Here"
`$message = "Put Your Message here (Y/N)"
`$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Will continue"
`$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Will exit"
`$options = [System.Management.Automation.Host.ChoiceDescription[]](`$yes, `$no)
`$result = `$host.ui.PromptForChoice(`$title, `$message, `$options, 0)
if (`$result -eq 1)
{ Write-Output "User pressed no!"}
elseif (`$result -eq 0)
{ Write-Output "User pressed yes!"}
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'Run SQL query with SMO*'})
{
$snippet = @{
Title = 'Run SQL query with SMO'
Description = 'creates SMO object and runs a sql command'
Text = @"
`$srv = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList `$Server
`$SqlConnection = `$srv.ConnectionContext
`$SqlConnection.StatementTimeout = 8000
`$SqlConnection.ConnectTimeout = 10
`$SqlConnection.Connect()
`$Results = `$SqlConnection.ExecuteWithResults(`$Query).Tables
`$SqlConnection.Disconnect()
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'SQL Assemblies*'})
{
$snippet = @{
Title = 'SQL Assemblies'
Description = 'SQL Assemblies'
Text = @"
[void][reflection.assembly]::LoadWithPartialName( "Microsoft.SqlServer.Management.Common" );
[void][reflection.assembly]::LoadWithPartialName( "Microsoft.SqlServer.SmoEnum" );
[void][reflection.assembly]::LoadWithPartialName( "Microsoft.SqlServer.Smo" );
[void][reflection.assembly]::LoadWithPartialName( "Microsoft.SqlServer.SmoExtended " );
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
"@
}
New-IseSnippet $snippet
}
if(!$snips.Where{$_.Name -like 'Bulk copy from data table*'})
{
$snippet = @{
Title = 'Bulk copy from data table'
Description = 'Bulk copy from data table'
Text = @"
`$sqlserver = ''
`$database = ''
`$table = ''
`$batchsize = 5000
# Build the sqlbulkcopy connection, and set the timeout to infinite
`$connectionstring = "Data Source=`$sqlserver;Integrated Security=true;Initial Catalog=`$database;"
`$bulkcopy = New-Object Data.SqlClient.SqlBulkCopy(`$connectionstring, [System.Data.SqlClient.SqlBulkCopyOptions]::TableLock)
`$bulkcopy.DestinationTableName = `$table
`$bulkcopy.bulkcopyTimeout = 0
`$bulkcopy.batchsize = `$batchsize
`$bulkcopy.WriteToServer(`$datatable)
`$datatable.Clear()
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'WSMan Test and CIM instead of WMI*'})
{
$snippet = @{
Title = 'WSMan Test and CIM instead of WMI'
Description = 'Creates a CiM Session depending on the results of the WSMan test'
Text = @"
## Servername
`$Server = ''
## Test for WSMan
`$WSMAN = Test-WSMan `$Server
## Change Protocol if needed for CimSession
if(`$WSMAN.ProductVersion.Contains('Stack: 2.0'))
{
`$opt = New-CimSessionOption -Protocol Dcom
`$s = New-CimSession -ComputerName `$Server -SessionOption `$opt
}
else
{
`$s = New-CimSession -ComputerName `$Server
}
## Do your funky CIM stuff
Get-CimInstance -ClassName Win32_OperatingSystem -CimSession `$s| select LastBootUpTime
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'Max Length of Datatable*'})
{
$snippet = @{
Title = 'Max Length of Datatable'
Description = 'Takes a datatable object and iterates through it to get the max length of the string columns - useful for data loads'
Text = @"
`$columns = (`$datatable | Get-Member -MemberType Property).Name
foreach(`$column in `$Columns)
{
`$max = 0
foreach (`$a in `$datatable)
{
if(`$max -lt `$a.`$column.length)
{
`$max = `$a.`$column.length
}
}
Write-Output "`$column max length is `$max"
}
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'Start a stopwatch*'})
{
$snippet = @{
Title = 'Start a stopwatch'
Description = 'Starts a stopwatch'
Text = @"
`$sw = [diagnostics.stopwatch]::StartNew()
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'New Excel Object*'})
{
$snippet = @{
Title = "New Excel Object";
Description = "Creates a New Excel Object";
Text = @"
# Create a .com object for Excel
`$xl = new-object -comobject excel.application
`$xl.Visible = `$true # Set this to False when you run in production
`$wb = `$xl.Workbooks.Add() # Add a workbook
`$ws = `$wb.Worksheets.Item(1) # Add a worksheet
`$cells=`$ws.Cells
Do Some Stuff
perhaps
`$cells.item(`$row,`$col)="Server"
`$cells.item(`$row,`$col).font.size=16
`$Cells.item(`$row,`$col).Columnwidth = 10
`$col++
`$wb.Saveas("C:\temp\Test`$filename.xlsx")
`$xl.quit()
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'SQL Authentication SMO*'})
{
$snippet = @{
Title = 'SQL Authentication SMO'
Description = 'SQL Authentication SMO'
Text = @"
`$sqllogin = Get-Credential
`$srv = New-Object Microsoft.SqlServer.Management.Smo.Server `$server
`$srv.ConnectionContext.LoginSecure = `$false
`$srv.ConnectionContext.set_Login(`$sqllogin.username)
`$srv.ConnectionContext.set_SecurePassword(`$sqllogin.Password)
try
{
`$srv.ConnectionContext.Connect()
}
catch
{
throw "Can't connect to `$server or access denied. Quitting."
}
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'Simple Create Database*'})
{
$snippet = @{
Title = 'Simple Create Database'
Description = 'SImple SMO code to create a database'
Text = @"
##Create a database
`$server = ''
`$DBName = 'TheBeardsDatabase'
`$db = New-Object Microsoft.SqlServer.Management.Smo.Database `$Server, `$DBName
`$db.Create()
"@
}
New-IseSnippet @snippet
}
if(!$snips.Where{$_.Name -like 'Create a database Role*'})
{
$snippet = @{
Title = 'Create a database Role'
Description = 'Simple SMO to create a Database Role'
Text = @"
##Create a role
`$server = ''
`$DBName = ''
`$RoleName = ''
`$srv = New-Object Microsoft.SqlServer.Management.Smo.Server `$Server
`$db = `$srv.Databases[`$DBName]
`$Role = New-Object Microsoft.SqlServer.Management.Smo.DatabaseRole `$db, `$RoleName
`$Role.Create()
"@
}
New-IseSnippet @snippet
}