-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpowershell.json
315 lines (307 loc) · 11.8 KB
/
powershell.json
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
{
/*
These are PowerShell snippets which you can use in Visual Studio Code
To use them click File --> Preferences --> User Snippets and type PowerShell
or edit $env:\appdata\code\user\snippets\powershell.json
In general and in order I converted exisitng snippets like this
Replace `$ with $$
Replace \ with \\
Replace " with \"
\r for new line
\t for tab
Each line in ""
, at the end of each line in the body except the last one
Look out for red or green squiggles :-)
*/
"SMO-Server": {
"prefix": "SMO-Server",
"body": [
"$$srv = New-Object Microsoft.SqlServer.Management.Smo.Server $$Server"
],
"description": "Creates a SQL Server SMO Object"
},
"DataTable": {
"prefix": "DataTable",
"body": [
"# Create Table Object",
"$$table = New-Object system.Data.DataTable $$TableName",
"\r# Create Columns",
"$$col1 = New-Object system.Data.DataColumn NAME1,([string])",
"$$col2 = New-Object system.Data.DataColumn NAME2,([decimal])",
"\r#Add the Columns to the table",
"$$table.columns.add($$col1)",
"$$table.columns.add($$col2)",
"\r# Create a new Row",
"$$row = $$table.NewRow() ",
"\r# Add values to new row",
"$$row.Name1 = 'VALUE'",
"$$row.NAME2 = 'VALUE'",
"\r#Add new row to table",
"$$table.Rows.Add($$row)"
],
"description": "Creates a Data Table Object"
},
"Formatted Duration": {
"prefix": "Formatted Duration",
"body": [
"$$FormattedDuration = @{",
"\tName = 'FormattedDuration'",
"\tExpression = {",
"\t\t[timespan]$$_.RunDuration.ToString().PadLeft(6,'0').insert(4,':').insert(2,':')",
"\t}",
"}"
],
"description": "Formats Get-SQLAgentJobHistory into timespan"
},
"Prompt for input": {
"prefix": "Prompt for input",
"body": [
"# 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) ",
"\r",
"if ($$result -eq 1) { ",
"\tWrite-Output \"User pressed no!\"",
"}",
"elseif ($$result -eq 0){ ",
"\tWrite-Output \"User pressed yes!\"",
"}"
],
"description": "Simple way of gathering input from users with simple yes and no"
},
"Run SQL query with SMO": {
"prefix": "SQL query with SMO",
"body": [
"$$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()"
],
"description": "creates SMO object and runs a sql command"
},
"SQL Assemblies": {
"prefix": "SQL Assemblies",
"body": [
"[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\") "
],
"description": "Loads the SQL Assemblies"
},
"Bulk copy from data table": {
"prefix": "Bulk copy from data table",
"body": [
"$$sqlserver = ''",
"$$database = ''",
"$$table = ''",
"$$batchsize = 5000",
"\r",
"# 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()"
],
"description": "Bulk copy from data table"
},
"WSMan Test and CIM instead of WMI": {
"prefix": "WSMan Test and CIM instead of WMI",
"body": [
"## Servername",
"$$Server = ''",
"\r",
"## Test for WSMan",
"$$WSMAN = Test-WSMan $$Server",
"\r",
"## Change Protocol if needed for CimSession",
"if($$WSMAN.ProductVersion.Contains('Stack: 2.0')) {",
"\t$$opt = New-CimSessionOption -Protocol Dcom",
"\t$$s = New-CimSession -ComputerName $$Server -SessionOption $$opt",
"}",
"else {",
"\t$$s = New-CimSession -ComputerName $$Server",
"}",
"\r",
"## Do your funky CIM stuff",
"Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $$s| select LastBootUpTime "
],
"description": "Creates a CiM Session depending on the results of the WSMan test"
},
"Max Length of Datatable": {
"prefix": "Max Length of Datatable",
"body": [
"$$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\"",
"}"
],
"description": "Takes a datatable object and iterates through it to get the max length of the string columns - useful for data loads"
},
"stopWatch": {
"prefix": "Stopwatch",
"body": [
"$$sw = [diagnostics.stopwatch]::StartNew()"
],
"description": "Starts a stopwatch"
},
"New Excel Object": {
"prefix": "Excel Object",
"body": [
"# 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",
"\r ",
"Do Some Stuff",
"\r ",
"perhaps",
"\r ",
"$$cells.item($$row,$$col)=\"Server\"",
"$$cells.item($$row,$$col).font.size=16",
"$$Cells.item($$row,$$col).Columnwidth = 10",
"$$col++",
"\r",
"$$wb.Saveas(\"C:\\temp\\Test$$filename.xlsx\")",
"$$xl.quit()"
],
"description": "Creates a New Excel Object"
},
"SQL Authentication SMO": {
"prefix": "SQL Authentication SMO",
"body": [
"$$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)",
"\r",
"try { ",
"\t$$srv.ConnectionContext.Connect() ",
"} ",
"catch { ",
"\tthrow \"Can't connect to $$server or access denied. Quitting.\" ",
"}"
],
"description": "Creates a SQL Authentication SMO Server Object"
},
"Simple Create Database": {
"prefix": "Database - Create with SMO",
"body": [
"##Create a database",
"$$server = ''",
"$$DBName = 'TheBeardsDatabase'",
"$$db = New-Object Microsoft.SqlServer.Management.Smo.Database $$Server, $$DBName",
"$$db.Create()"
],
"description": "Create Database using SMO and defaults"
},
"Create a database Role": {
"prefix": "Role - Database Create with SMO",
"body": [
"##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()"
],
"description": "Simple SMO to create a Database Role"
},
"SQL firewall Rules": {
"prefix": "Firewall Rules SQL",
"body": [
"#Enabling SQL Server Ports",
"New-NetFirewallRule -DisplayName “SQL Server” -Direction Inbound –Protocol TCP –LocalPort 1433 -Action allow",
"New-NetFirewallRule -DisplayName “SQL Admin Connection” -Direction Inbound –Protocol TCP –LocalPort 1434 -Action allow",
"New-NetFirewallRule -DisplayName “SQL Database Management” -Direction Inbound –Protocol UDP –LocalPort 1434 -Action allow",
"New-NetFirewallRule -DisplayName “SQL Service Broker” -Direction Inbound –Protocol TCP –LocalPort 4022 -Action allow",
"New-NetFirewallRule -DisplayName “SQL Debugger/RPC” -Direction Inbound –Protocol TCP –LocalPort 135 -Action allow",
"#Enabling SQL Analysis Ports",
"New-NetFirewallRule -DisplayName “SQL Analysis Services” -Direction Inbound –Protocol TCP –LocalPort 2383 -Action allow",
"New-NetFirewallRule -DisplayName “SQL Browser” -Direction Inbound –Protocol TCP –LocalPort 2382 -Action allow",
"#Enabling Misc. Applications",
"New-NetFirewallRule -DisplayName “HTTP” -Direction Inbound –Protocol TCP –LocalPort 80 -Action allow",
"New-NetFirewallRule -DisplayName “SSL” -Direction Inbound –Protocol TCP –LocalPort 443 -Action allow",
"New-NetFirewallRule -DisplayName “SQL Server Browse Button Service” -Direction Inbound –Protocol UDP –LocalPort 1433 -Action allow"
],
"description": "Set Firewall for SQL "
},
"Remote Management firewall Rules": {
"prefix": "Firewall Rules Remote - File and Printer",
"body": [
"Set-NetFirewallRule -DisplayGroup 'File And Printer Sharing' -Enabled True"
],
"description": "Set Firewal for Remote Mangememnt WMI etc"
},
"Ping firewall Rules": {
"prefix": "Firewall Rules Ping",
"body": [
"New-NetFirewallRule -Name Allow_Ping -Description 'Allow Ping' -Protocol ICMPv4 -Icmptype 8 -Enabled True -Profile Any -Action Allow"
],
"description": "Set Firewall for ping"
},
"Remote Management firewall Rules": {
"prefix": "Firewall Rules Remote - wmi",
"body": [
"Get-NetFirewallRule -DisplayGroup 'Remote*' | Set-NetFirewallRule -Enabled True"
],
"description": "Set Firewall for Remote Management"
},
"Show All Excel Colours": {
"prefix": "Excel Colours",
"body": [
"$xl = new-object -comobject excel.application",
"$xl.Visible = $true",
"$xl.DisplayAlerts = $False",
"$wb = $xl.Workbooks.Add()",
"$ws = $wb.Worksheets.Item(1)",
"$row = 1",
"$i = 1",
"For($i = 1; $i -lt 57; $i++){",
"$ws.Cells.Item($row, 1) = \"'$'ws.Cells.Item($row, 2).Font.ColorIndex = \" + $row",
"$ws.Cells.Item($row, 2).Font.ColorIndex = $row",
"$ws.Cells.Item($row, 2) = \"test \" + $row",
"$row++",
"}",
"[void]$ws.cells.entireColumn.Autofit()"
],
"description": "Shows all the colour indexes for the Excel colours"
}
}
/*
// Place your snippets for PowerShell here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
"Print to console": {
"prefix": "log",
"body": [
"console.log('$1');",
"$2"
],
"description": "Log output to console"
}
*/