forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd-DbaCmsRegServerGroup.ps1
119 lines (93 loc) · 4.92 KB
/
Add-DbaCmsRegServerGroup.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
#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle#
function Add-DbaCmsRegServerGroup {
<#
.SYNOPSIS
Adds registered server groups to SQL Server Central Management Server (CMS)
.DESCRIPTION
Adds registered server groups to SQL Server Central Management Server (CMS). If you need more flexibility, look into Import-DbaCmsRegServer which accepts multiple kinds of input and allows you to add reg servers and groups from different CMS.
.PARAMETER SqlInstance
The target SQL Server instance
.PARAMETER SqlCredential
Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
.PARAMETER Name
The name of the registered server group
.PARAMETER Description
The description for the registered server group
.PARAMETER Group
The SQL Server Central Management Server group. If no groups are specified, the new group will be created at the root.
.PARAMETER InputObject
Allows results from Get-DbaCmsRegServerGroup to be piped in
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: RegisteredServer, CMS
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Add-DbaCmsRegServerGroup
.EXAMPLE
PS C:\> Add-DbaCmsRegServerGroup -SqlInstance sql2012 -Name HR
Creates a registered server group called HR, in the root of sql2012's CMS
.EXAMPLE
PS C:\> Add-DbaCmsRegServerGroup -SqlInstance sql2012, sql2014 -Name sub-folder -Group HR
Creates a registered server group on sql2012 and sql2014 called sub-folder within the HR group
.EXAMPLE
PS C:\> Get-DbaCmsRegServerGroup -SqlInstance sql2012, sql2014 -Group HR | Add-DbaCmsRegServerGroup -Name sub-folder
Creates a registered server group on sql2012 and sql2014 called sub-folder within the HR group of each server
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[Alias("ServerInstance", "SqlServer")]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[parameter(Mandatory)]
[string]$Name,
[string]$Description,
[string]$Group,
[parameter(ValueFromPipeline)]
[Microsoft.SqlServer.Management.RegisteredServers.ServerGroup[]]$InputObject,
[switch]$EnableException
)
process {
if (-not $InputObject -and -not $SqlInstance) {
Stop-Function -Message "You must either pipe in a registered server group or specify a sqlinstance"
return
}
foreach ($instance in $SqlInstance) {
if ((Test-Bound -ParameterName Group)) {
$InputObject += Get-DbaCmsRegServerGroup -SqlInstance $instance -SqlCredential $SqlCredential -Group $Group
} else {
$InputObject += Get-DbaCmsRegServerGroup -SqlInstance $instance -SqlCredential $SqlCredential -Id 1
}
}
foreach ($reggroup in $InputObject) {
$parentserver = Get-RegServerParent -InputObject $reggroup
$server = $parentserver.ServerConnection.ServerInstance.SqlConnectionObject
if ($null -eq $parentserver) {
Stop-Function -Message "Something went wrong and it's hard to explain, sorry. This basically shouldn't happen." -Continue
}
if ($Pscmdlet.ShouldProcess($parentserver.SqlInstance, "Adding $Name")) {
try {
$newgroup = New-Object Microsoft.SqlServer.Management.RegisteredServers.ServerGroup($reggroup, $Name)
$newgroup.Description = $Description
$newgroup.Create()
Get-DbaCmsRegServerGroup -SqlInstance $parentserver.ServerConnection.SqlConnectionObject -Group (Get-RegServerGroupReverseParse -object $newgroup)
$parentserver.ServerConnection.Disconnect()
} catch {
Stop-Function -Message "Failed to add $reggroup on $server" -ErrorRecord $_ -Continue
}
}
}
}
end {
Test-DbaDeprecation -DeprecatedOn "1.0.0" -Alias Add-DbaRegisteredServerGroup
}
}