-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSqlManagement.psm1
More file actions
656 lines (647 loc) · 20.5 KB
/
SqlManagement.psm1
File metadata and controls
656 lines (647 loc) · 20.5 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
Function Invoke-SqlQuery {
<#
.SYNOPSIS
Run a SQL Query or execute a Stored Procedure
.DESCRIPTION
This function does the main work against a SQL server
.PARAMETER Credential
A credential object if we need to authenticate with a SQL account
.PARAMETER ConnectionString
A connection string used to connect to the SQL server that is build
by the other functions
.PARAMETER Command
A switch that if present indicates the query is really a command
.PARAMETER Query
Either a query or stored procedure
.EXAMPLE
Invoke-SqlQuery -Credential $null -ConnectionString "Server=(local)\MSSQLSERVER;Database=printlog" -Query "select * from dbo.joblog";
Description
-----------
This function is intended to be called by other functions inside the module, but
you can call it directly. This example connects to the printlog database of the local MSSQLSERVER
instance of SQL and returns all records from the joblog table.
.NOTES
FunctionName : Invoke-SqlQuery
Created by : jspatton
Date Coded : 06/09/2014 12:31:07
.LINK
https://code.google.com/p/mod-posh/wiki/Untitled9#Invoke-SqlQuery
.LINK
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential]$Credential = $null,
[parameter(Mandatory = $true)]
[string]$ConnectionString = $null,
[switch]$Command,
[parameter(Mandatory = $true)]
[string]$Query = $null
)
Begin {
}
Process {
try {
Write-Debug "Make sure we stop on errors";
$ErrorActionPreference = "Stop";
Write-Debug "Clear out any previous errors";
$Error.Clear();
Write-Debug "Create System.Data.SqlClient.SqlConnectionStringBuilder";
Write-Debug "To build a proper connection string based on whats passed in";
Write-Verbose $ConnectionString;
$SqlConnectionStringBuilder = New-Object System.Data.SqlClient.SqlConnectionStringBuilder($ConnectionString);
Write-Debug "Validated ConnectionString";
Write-Debug $SqlConnectionStringBuilder.ConnectionString;
Write-Debug "Create System.Data.SqlClient.SqlConnection to connect to sql";
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection($SqlConnectionStringBuilder.ConnectionString);
Write-Debug "Check if credentials are passed in";
if ($Credential) {
Write-Debug "SqlCredentials can only accept read-only passwords";
Write-Debug "Make credential password readonly";
$Credential.Password.MakeReadOnly();
Write-Debug "Create a System.Data.SqlClient.SqlCredential to hold credentials";
$sqlCredential = New-Object System.Data.SqlClient.SqlCredential($Credential.UserName, $Credential.Password);
Write-Verbose "Assign credentials to connection object";
$SqlConnection.Credential = $sqlCredential;
}
Write-Debug "Open connection";
$SqlConnection.Open();
if ($Command) {
Write-Debug "Create a System.Data.SqlClient.SqlCommand object";
$SqlCommand = New-Object System.Data.SqlClient.SqlCommand;
Write-Debug "Set CommandType to 1";
$SqlCommand.CommandType = 1;
Write-Debug "Assign Connection object";
$SqlCommand.Connection = $SqlConnection;
Write-Debug "Set CommandText";
Write-Verbose $Command;
$SqlCommand.CommandText = $Command;
Write-Verbose "Return SQL Data";
$Result = $SqlCommand.ExecuteNonQuery();
}
else {
Write-Debug "Create System.Data.Dataset object to hold result";
$SqlDataSet = New-Object System.Data.DataSet;
Write-Debug "Create System.Data.SqlClient.SqlDataAdpater to fill the Dataset object";
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($Query, $SqlConnection);
Write-Debug "Fill the dataset object";
$Output = $SqlAdapter.Fill($SqlDataSet);
Write-Verbose "Return SQL Data";
$Result = $SqlDataSet.Tables;
}
Return $Result;
}
catch {
$str = (([string] $Error).Split(':'))[1]
Write-Error ($str.Replace('"', ''))
}
finally {
if ($SqlConnection) {
$SqlConnection.Close()
}
}
}
End {
}
}
Function Get-SqlVersion {
<#
.SYNOPSIS
Get the SQL version running
.DESCRIPTION
This function queries the SQL Server and returns the version of
SQL that is installed.
.PARAMETER ComputerName
The name of the SQL server to connect to
.PARAMETER Instance
The instance name is used to resolve to a particular TCP/IP port number on
which a database instance is hosted
.PARAMETER ConnectionString
A connection string used to connect to the SQL server that is build
by the other functions
.PARAMETER Credential
A credential object if we need to authenticate with a SQL account
.EXAMPLE
Get-SqlVersion
Description
-----------
This example shows the basic syntax of the command.
.NOTES
FunctionName : Get-SqlVersion
Created by : jspatton
Date Coded : 06/11/2014 11:23:41
.LINK
https://code.google.com/p/mod-posh/wiki/SqlManagement#Get-SqlVersion
.LINK
http://support.microsoft.com/kb/321185
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $false)]
[string] $Computername = $env:COMPUTERNAME,
[parameter(Mandatory = $false)]
[string] $Instance,
[parameter(Mandatory = $false)]
[string] $ConnectionString = $null,
[parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential] $Credential
)
Begin {
if ($ConnectionString) {
$sqlConnString = $ConnectionString
}
else {
if ($Instance -eq $null) {
$sqlConnString = "Server=tcp:$($ComputerName);Database=$($Database)";
}
else {
$sqlConnString = "Server=tcp:$($ComputerName)\$($Instance);Database=$($Database)";
}
if (!($Credential)) {
$sqlConnString += ";trusted_connection=true";
}
}
$sqlCommandText = "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition');"
}
Process {
$Result = Invoke-SqlQuery -Credential $Credential -ConnectionString $sqlConnString -Query $sqlCommandText;
}
End {
Return $Result
}
}
function New-SqlLogin {
<#
.SYNOPSIS
Creates a Database Engine login for SQL Server and Windows Azure SQL Database
.DESCRIPTION
The login can connect to the Database Engine or SQL Database but only has the
permissions granted to the public role
.PARAMETER Login
Specifies the name of the login that is created. There are four types of
logins: SQL Server logins, Windows logins, certificate-mapped logins, and
asymmetric key-mapped logins.
.PARAMETER ComputerName
The name of the SQL server to connect to
.PARAMETER Database
Specifies the default database to be assigned to the login
.PARAMETER Instance
The instance name is used to resolve to a particular TCP/IP port number on
which a database instance is hosted
.PARAMETER Credential
A credential object that represents a SQL Login that has permissions
.EXAMPLE
New-SqlLogin -Login "DOMAIN\JSmith" -ComputerName $env:COMPUTERNAME -Database master -sqlInstance 'MSSQLSERVER'
Description
-----------
This example shows how to add a windows user to a the master database on
the default instance of Sql Server
.NOTES
FunctionName : New-SqlLogin
Created by : Jeffrey
Date Coded : 06/08/2014 17:32:12
.LINK
https://code.google.com/p/mod-posh/wiki/SqlManagement#New-SqlLogin
.LINK
http://msdn.microsoft.com/en-us/library/ms189751.aspx
.LINK
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx
#>
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[string] $Login,
[parameter(Mandatory = $false)]
[string] $Computername = $env:COMPUTERNAME,
[parameter(Mandatory = $true)]
[string] $Database,
[parameter(Mandatory = $false)]
[string] $Instance,
[parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential] $Credential
)
Begin {
if ($Instance -eq $null) {
$sqlConnString = "Server=tcp:$($ComputerName);Database=$($Database)";
}
else {
$sqlConnString = "Server=tcp:$($ComputerName)\$($Instance);Database=$($Database)";
}
if (!($Credential)) {
$sqlConnString += ";trusted_connection=true";
}
$sqlCommandText = "create login [$($Login)] from windows with default_database=[$($Database)], default_language=[us_english]"
}
Process {
$Result = Invoke-SqlQuery -Credential $Credential -ConnectionString $sqlConnString -Command -Query $sqlCommandText;
}
End {
Return $Result
}
}
function Add-SqlUser {
<#
.SYNOPSIS
Adds a user to the current database
.DESCRIPTION
This function grants access to a database but does not automatically grant any
access to the objects in a database
.PARAMETER Login
Specifies the name of the login that is created. There are four types of
logins: SQL Server logins, Windows logins, certificate-mapped logins, and
asymmetric key-mapped logins.
.PARAMETER ComputerName
The name of the SQL server to connect to
.PARAMETER Database
Specifies the default database to be assigned to the login
.PARAMETER Instance
The instance name is used to resolve to a particular TCP/IP port number on
which a database instance is hosted
.PARAMETER Credential
A credential object that represents a SQL Login that has permissions
.EXAMPLE
Add-SqlUser -Login "DOMAIN\JSmith" -ComputerName $env:COMPUTERNAME -Database 'master' -sqlInstance 'MSSQLSERVER'
Description
-----------
Adds the Windows domain user JSmithto the master database.
.NOTES
FunctionName : Add-SqlUser
Created by : Jeffrey
Date Coded : 06/08/2014 17:32:12
.LINK
https://code.google.com/p/mod-posh/wiki/SqlManagement#Add-SqlUser
.LINK
http://msdn.microsoft.com/en-us/library/ms173463.aspx
#>
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[string] $Login,
[parameter(Mandatory = $false)]
[string] $Computername = $env:COMPUTERNAME,
[parameter(Mandatory = $true)]
[string] $Database,
[parameter(Mandatory = $false)]
[string] $Instance,
[parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential] $Credential
)
Begin {
if ($Instance -eq $null) {
$sqlConnString = "Server=tcp:$($ComputerName);Database=$($Database)";
}
else {
$sqlConnString = "Server=tcp:$($ComputerName)\$($Instance);Database=$($Database)";
}
if (!($Credential)) {
$sqlConnString += ";trusted_connection=true";
}
$sqlCommandText = "create user [$($Login)] For Login [$($Login)]"
}
Process {
$Result = Invoke-SqlQuery -Credential $Credential -ConnectionString $sqlConnString -Command -Query $sqlCommandText;
}
End {
Return $Result
}
}
function Add-SqlRole {
<#
.SYNOPSIS
Adds a database user, database role, Windows login, or Windows group
to a database role in the current database
.DESCRIPTION
A member added to a role inherits the permissions of the role. If
the new member is a Windows-level principal without a corresponding
database user, a database user will be created but may not be fully
mapped to the login. Always check that the login exists and has access
to the database
.PARAMETER Login
Specifies the name of the login that is created. There are four types of
logins: SQL Server logins, Windows logins, certificate-mapped logins, and
asymmetric key-mapped logins.
.PARAMETER ComputerName
The name of the SQL server to connect to
.PARAMETER Database
Specifies the default database to be assigned to the login
.PARAMETER Role
Is the name of the database role in the current database. role is a sysname,
with no default
.PARAMETER Instance
The instance name is used to resolve to a particular TCP/IP port number on
which a database instance is hosted
.PARAMETER Credential
A credential object that represents a SQL Login that has permissions
.EXAMPLE
Add-SqlRole -Login "DOMAIN\JSmith" -ComputerName $env:COMPUTERNAME -Database msdb -Role SQLAgentReaderRole -sqlInstance 'MSSQLSERVER'
Description
-----------
This example grants the SQLAgentReaderRole to the Windows user JSmith
.NOTES
FunctionName : Add-SqlRole
Created by : Jeffrey
Date Coded : 06/08/2014 17:32:12
.LINK
https://code.google.com/p/mod-posh/wiki/SqlManagement#Add-SqlRole
.LINK
http://msdn.microsoft.com/en-us/library/ms187750.aspx
#>
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[string] $Login,
[parameter(Mandatory = $false)]
[string] $Computername = $env:COMPUTERNAME,
[parameter(Mandatory = $true)]
[string] $Database,
[parameter(Mandatory = $true)]
[string] $Role,
[parameter(Mandatory = $false)]
[string] $Instance,
[parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential] $Credential
)
Begin {
if ($Instance -eq $null) {
$sqlConnString = "Server=tcp:$($ComputerName);Database=$($Database)";
}
else {
$sqlConnString = "Server=tcp:$($ComputerName)\$($Instance);Database=$($Database)";
}
if (!($Credential)) {
$sqlConnString += ";trusted_connection=true";
}
$sqlCommandText = "exec [$($Database)]..sp_addrolemember N`'$($Role)`', N`'[$($LoginName)]`'"
}
Process {
$Result = Invoke-SqlQuery -Credential $Credential -ConnectionString $sqlConnString -Command -Query $sqlCommandText;
}
End {
Return $Result
}
}
function Set-SqlServerPermission {
<#
.SYNOPSIS
Grants permissions on a server
.DESCRIPTION
This function grants or denies permissions on an object to or from
a user. Permissions at the server scope can be granted only when the
current database is master. A server is the highest level of the
permissions hierarchy.
.PARAMETER Login
Specifies the name of the login that is created. There are four types of
logins: SQL Server logins, Windows logins, certificate-mapped logins, and
asymmetric key-mapped logins.
.PARAMETER ComputerName
The name of the SQL server to connect to
.PARAMETER Database
Specifies the default database to be assigned to the login
.PARAMETER Grant
A switch that if present grants said permission, otherwise deny
.PARAMETER Permission
One of a long list of available permissions on a server. See the table
on the MSDN link for details about specific permissions.
.PARAMETER Instance
The instance name is used to resolve to a particular TCP/IP port number on
which a database instance is hosted
.PARAMETER Credential
A credential object that represents a SQL Login that has permissions
.EXAMPLE
Set-SqlServerPermission -Login "DOMAIN\JSmith" -ComputerName $env:COMPUTERNAME -Database master -Grant -Permission "VIEW ANY DATABASE" -sqlInstance 'MSSQLSERVER'
Description
-----------
This example grant's the View Any Database permission to the Windows user JSmith
.NOTES
FunctionName : Set-SqlServerPermission
Created by : Jeffrey
Date Coded : 06/08/2014 17:32:12
.LINK
https://code.google.com/p/mod-posh/wiki/SqlManagement#Set-SqlServerPermission
.LINK
http://msdn.microsoft.com/en-us/library/ms186717.aspx
#>
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[string] $Login,
[parameter(Mandatory = $false)]
[string] $Computername = $env:COMPUTERNAME,
[parameter(Mandatory = $true)]
[string] $Database,
[switch] $Grant,
[parameter(Mandatory = $true)]
[string] $Permission,
[parameter(Mandatory = $false)]
[string] $Instance,
[parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential] $Credential
)
Begin {
if ($Instance -eq $null) {
$sqlConnString = "Server=tcp:$($ComputerName);Database=$($Database)";
}
else {
$sqlConnString = "Server=tcp:$($ComputerName)\$($Instance);Database=$($Database)";
}
if (!($Credential)) {
$sqlConnString += ";trusted_connection=true";
}
if ($Grant) {
$sqlCommandText = "GRANT $($Permission) TO [$($LoginName)]"
}
else {
$sqlCommandText = "DENY $($Permission) TO [$($LoginName)]"
}
}
Process {
$Result = Invoke-SqlQuery -Credential $Credential -ConnectionString $sqlConnString -Command -Query $sqlCommandText;
}
End {
Return $Result
}
}
function Get-SqlUser {
<#
.SYNOPSIS
Get a list of SQL users from the server
.DESCRIPTION
This function returns a list of users that are found in the sys.sysusers view
.PARAMETER ComputerName
The name of the SQL server to connect to
.PARAMETER Database
Specifies the default database to be assigned to the login
.PARAMETER Instance
The instance name is used to resolve to a particular TCP/IP port number on
which a database instance is hosted
.PARAMETER Credential
A credential object that represents a SQL Login that has permissions
.EXAMPLE
Get-SqlUser
Description
-----------
This example shows how to get a list of users from the local Sql server
.NOTES
FunctionName : Get-SqlUser
Created by : Jeffrey
Date Coded : 06/08/2014 17:32:12
.LINK
https://code.google.com/p/mod-posh/wiki/SqlManagement#Get-SqlUser
.LINK
http://msdn.microsoft.com/en-us/library/ms179871.aspx
#>
[CmdletBinding()]
param
(
[parameter(Mandatory = $false)]
[string] $ComputerName = $env:COMPUTERNAME,
[parameter(Mandatory = $true)]
[string] $Database,
[parameter(Mandatory = $false)]
[string] $Instance,
[parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential] $Credential
)
Begin {
if ($Instance -eq $null) {
$sqlConnString = "Server=tcp:$($ComputerName);Database=$($Database)";
}
else {
$sqlConnString = "Server=tcp:$($ComputerName)\$($Instance);Database=$($Database)";
}
if (!($Credential)) {
$sqlConnString += ";trusted_connection=true";
}
}
Process {
$sqlCommandText = "SELECT * FROM [sys].[sysusers]"
$Result = Invoke-SqlQuery -Credential $Credential -ConnectionString $sqlConnString -Query $sqlCommandText;
}
End {
Return $Result
}
}
function Get-SqlDatabase {
<#
.SYNOPSIS
Get a list of databases
.DESCRIPTION
Return a list of databases from the sys.databases view
.PARAMETER ComputerName
The name of the SQL server to connect to
.PARAMETER Database
Specifies the default database to be assigned to the login
.PARAMETER Instance
The instance name is used to resolve to a particular TCP/IP port number on
which a database instance is hosted
.PARAMETER Credential
A credential object that represents a SQL Login that has permissions
.EXAMPLE
Get-SqlDatabase
Description
-----------
This example shows how to get a list of databases from the local Sql server
.NOTES
FunctionName : Get-SqlDatabase
Created by : Jeffrey
Date Coded : 06/08/2014 17:32:12
.LINK
https://code.google.com/p/mod-posh/wiki/SqlManagement#Get-SqlDatabase
.LINK
http://msdn.microsoft.com/en-us/library/ms179900.aspx
#>
[CmdletBinding()]
param
(
[parameter(Mandatory = $false)]
[string] $ComputerName = $env:COMPUTERNAME,
[parameter(Mandatory = $false)]
[string] $Database,
[parameter(Mandatory = $false)]
[string] $Instance,
[parameter(Mandatory = $false)]
[System.Management.Automation.PSCredential] $Credential
)
Begin {
if ($Instance -eq $null) {
$sqlConnString = "Server=tcp:$($ComputerName)";
}
else {
$sqlConnString = "Server=tcp:$($ComputerName)\$($Instance)";
}
if ($Database -eq $null) {
$sqlConnString += ";Database=$($Database)";
}
if (!($Credential)) {
$sqlConnString += ";trusted_connection=true";
}
}
Process {
$sqlCommandText = "SELECT [name] ,[create_date] ,[collation_name] ,[state_desc] FROM [sys].[databases]"
$Result = Invoke-SqlQuery -Credential $Credential -ConnectionString $sqlConnString -Query $sqlCommandText;
}
End {
return $Result
}
}
Function Get-SQLInstance {
<#
.SYNOPSIS
Get a list of installed SQL instances
.DESCRIPTION
This function will query the registry of the local or remote computer and
return all instance names stored in the Software\Microsoft\Microsoft SQL Server\Instance Names\SQL
registry subkey.
.PARAMETER ComputerName
The name of the computer to connect to, defaults to local computername
.EXAMPLE
Get-SqlInstance
Description
-----------
This example shows the default syntax of the command, using the default
value for ComputerName.
.NOTES
FunctionName : Get-SqlInstance
Created by : jspatton
Date Coded : 06/11/2014 11:23:41
.LINK
https://code.google.com/p/mod-posh/wiki/SqlManagement#Get-SqlInstance
.LINK
http://www.powershellmagazine.com/2013/08/06/pstip-retrieve-all-sql-instance-names-on-local-and-remote-computers/
#>
[CmdletBinding()]
param
(
[string]$ComputerName = $env:COMPUTERNAME
)
Begin {
}
Process {
try {
$RegistryHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ComputerName)
$Subkey = $RegistryHKLM.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL")
if ($Subkey) {
$SqlInstances = $Subkey.GetValueNames()
foreach ($SqlInstance in $SqlInstances) {
$InstanceID = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$SqlInstance
New-Object -TypeName psobject -Property @{
InstanceName = $SqlInstance
InstanceId = $InstanceID
} | Select-Object -Property InstanceName, InstanceId
}
}
}
catch {
Write-Error $_.Exception.Message
}
}
End {
}
}