Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional support for Local Server Groups #5734

Merged
merged 13 commits into from
Jun 12, 2019
128 changes: 99 additions & 29 deletions functions/Add-DbaRegServer.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function Add-DbaRegServer {
<#
.SYNOPSIS
Adds registered servers to SQL Server Central Management Server (CMS)
Adds registered servers to SQL Server Central Management Server (CMS) or Local Server Groups

.DESCRIPTION
Adds registered servers to SQL Server Central Management Server (CMS). If you need more flexiblity, look into Import-DbaRegServer which
Adds registered servers to SQL Server Central Management Server (CMS) or Local Server Groups. If you need more flexiblity, look into Import-DbaRegServer which
accepts multiple kinds of input and allows you to add reg servers from different CMSes.

.PARAMETER SqlInstance
The target SQL Server instance
The target SQL Server instance if a CMS is used

.PARAMETER SqlCredential
Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
Expand All @@ -17,14 +17,29 @@ function Add-DbaRegServer {
Server Name is the actual SQL instance name (labeled Server Name)

.PARAMETER Name
Name is basically the nickname in SSMS CMS interface (labeled Registered Server Name)
Name is basically the nickname in SSMS Registered Server interface (labeled Registered Server Name)

.PARAMETER Description
Adds a description for the registered server

.PARAMETER Group
Adds the registered server to a specific group.

.PARAMETER ActiveDirectoryTenant
Active Directory Tenant

.PARAMETER ActiveDirectoryUserId
Active Directory User id

.PARAMETER ConnectionString
SQL Server connection string

.PARAMETER OtherParams
Additional parameters to append to the connection string

.PARAMETER ServerObject
SMO Server Objects (from Connect-DbaInstance)

.PARAMETER InputObject
Allows the piping of a registered server group

Expand Down Expand Up @@ -57,6 +72,11 @@ function Add-DbaRegServer {

Creates a registered server on sql2008's CMS which points to the SQL Server, sql01. When scrolling in CMS, the name "sql01" will be visible.

.EXAMPLE
PS C:\> Add-DbaRegServer -ServerName sql01

Creates a registered server in Local Server Groups which points to the SQL Server, sql01. When scrolling in Registered Servers, the name "sql01" will be visible.

.EXAMPLE
PS C:\> Add-DbaRegServer -SqlInstance sql2008 -ServerName sql01 -Name "The 2008 Clustered Instance" -Description "HR's Dedicated SharePoint instance"

Expand All @@ -69,35 +89,53 @@ function Add-DbaRegServer {
Creates a registered server on sql2008's CMS which points to the SQL Server, sql01. When scrolling in CMS, the name "sql01" will be visible within the Seattle group which is in the hr group.

.EXAMPLE
PS C:\> Get-DbaRegServerGroup -SqlInstance sql2008 -Group hr\Seattle | Add-DbaRegServer -ServerName sql01111

Creates a registered server on sql2008's CMS which points to the SQL Server, sql01. When scrolling in CMS, the name "sql01" will be visible within the Seattle group which is in the hr group.
PS C:\> Connect-DbaInstance -SqlInstance dockersql1 -SqlCredential sqladmin | Add-DbaRegServer -ServerName mydockerjam

Creates a registered server called "mydockerjam" in Local Server Groups that uses SQL authentication and points to the server dockersql1.
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[parameter(Mandatory)]
[string]$ServerName,
[string]$Name = $ServerName,
[string]$Description,
[object]$Group,
[string]$ActiveDirectoryTenant,
[string]$ActiveDirectoryUserId,
[string]$ConnectionString,
[string]$OtherParams,
[parameter(ValueFromPipeline)]
[Microsoft.SqlServer.Management.RegisteredServers.ServerGroup[]]$InputObject,
[parameter(ValueFromPipeline)]
[Microsoft.SqlServer.Management.Smo.Server[]]$ServerObject,
[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"
# double check in case a null name was bound
if (-not $PSBoundParameters.ServerName -and -not $PSBoundParameters.ServerObject) {
Stop-Function -Message "You must specify either ServerName or ServerObject"
return
}

# double check in case a null name was bound
if (-not $Name) {
$Name = $ServerName
}

if (-not $SqlInstance -and -not $InputObject) {
Write-Message -Level Verbose -Message "Parsing local"
if (($Group)) {
if ($Group -is [Microsoft.SqlServer.Management.RegisteredServers.ServerGroup]) {
$InputObject += Get-DbaRegServerGroup -Group $Group.Name
} else {
Write-Message -Level Verbose -Message "String group provided"
$InputObject += Get-DbaRegServerGroup -Group $Group
}
} else {
Write-Message -Level Verbose -Message "No group passed, getting root"
$InputObject += Get-DbaRegServerGroup -Id 1
}
}

foreach ($instance in $SqlInstance) {
if (($Group)) {
if ($Group -is [Microsoft.SqlServer.Management.RegisteredServers.ServerGroup]) {
Expand All @@ -115,24 +153,56 @@ function Add-DbaRegServer {
}

foreach ($reggroup in $InputObject) {
$parentserver = Get-RegServerParent -InputObject $reggroup

if ($null -eq $parentserver) {
Stop-Function -Message "Something went wrong and it's hard to explain, sorry. This basically shouldn't happen." -Continue
if ($reggroup.Source -eq "Azure Data Studio") {
Stop-Function -Message "You cannot use dbatools to remove or add registered servers in Azure Data Studio" -Continue
}

$server = $reggroup.ParentServer

if ($Pscmdlet.ShouldProcess($parentserver.SqlInstance, "Adding $ServerName")) {
try {
$newserver = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($reggroup, $Name)
$newserver.ServerName = $ServerName
$newserver.Description = $Description
$newserver.Create()

Get-DbaRegServer -SqlInstance $server -Name $Name -ServerName $ServerName
} catch {
Stop-Function -Message "Failed to add $ServerName on $($parentserver.SqlInstance)" -ErrorRecord $_ -Continue
if ($reggroup.ID) {
$target = $reggroup.ParentServer.SqlInstance
} else {
$target = "Local Registered Servers"
}
if ($Pscmdlet.ShouldProcess($target, "Adding $ServerName")) {

if ($ServerObject) {
foreach ($server in $ServerObject) {
if (-not $PSBoundParameters.Name) {
$Name = $server.Name
}
if (-not $PSBoundParameters.ServerName) {
$ServerName = $server.Name
}
try {
$newserver = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($reggroup, $Name)
$newserver.ServerName = $ServerName
$newserver.Description = $Description
$newserver.ConnectionString = $server.ConnectionContext.ConnectionString
$newserver.SecureConnectionString = $server.ConnectionContext.SecureConnectionString
$newserver.ActiveDirectoryTenant = $ActiveDirectoryTenant
$newserver.ActiveDirectoryUserId = $ActiveDirectoryUserId
$newserver.OtherParams = $OtherParams
$newserver.CredentialPersistenceType = "PersistLoginNameAndPassword"
$newserver.Create()

Get-DbaRegServer -SqlInstance $reggroup.ParentServer -Name $Name -ServerName $ServerName
} catch {
Stop-Function -Message "Failed to add $ServerName on $target" -ErrorRecord $_ -Continue
}
}
} else {
try {
$newserver = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($reggroup, $Name)
$newserver.ServerName = $ServerName
$newserver.Description = $Description
$newserver.ConnectionString = $ConnectionString
$newserver.ActiveDirectoryTenant = $ActiveDirectoryTenant
$newserver.ActiveDirectoryUserId = $ActiveDirectoryUserId
$newserver.OtherParams = $OtherParams
$newserver.Create()

Get-DbaRegServer -SqlInstance $reggroup.ParentServer -Name $Name -ServerName $ServerName
} catch {
Stop-Function -Message "Failed to add $ServerName on $target" -ErrorRecord $_ -Continue
}
}
}
}
Expand Down
33 changes: 21 additions & 12 deletions functions/Add-DbaRegServerGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ function Add-DbaRegServerGroup {
[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-DbaRegServerGroup -SqlInstance $instance -SqlCredential $SqlCredential -Group $Group
Expand All @@ -89,24 +85,37 @@ function Add-DbaRegServerGroup {
}
}

if (-not $SqlInstance -and -not $InputObject) {
if ((Test-Bound -ParameterName Group)) {
$InputObject += Get-DbaRegServerGroup -Group $Group
} else {
$InputObject += Get-DbaRegServerGroup -Id 1
}
}

foreach ($reggroup in $InputObject) {
$parentserver = Get-RegServerParent -InputObject $reggroup
$server = $reggroup.ParentServer
if ($reggroup.Source -eq "Azure Data Studio") {
Stop-Function -Message "You cannot use dbatools to remove or add registered server groups in Azure Data Studio" -Continue
}

if ($null -eq $parentserver) {
Stop-Function -Message "Something went wrong and it's hard to explain, sorry. This basically shouldn't happen." -Continue
if ($reggroup.ID) {
$target = $reggroup.Parent
} else {
$target = "Local Registered Server Groups"
}

if ($Pscmdlet.ShouldProcess($parentserver.SqlInstance, "Adding $Name")) {
if ($Pscmdlet.ShouldProcess($target, "Adding $Name")) {
try {
$newgroup = New-Object Microsoft.SqlServer.Management.RegisteredServers.ServerGroup($reggroup, $Name)
$newgroup.Description = $Description
$newgroup.Create()

Get-DbaRegServerGroup -SqlInstance $server -Group (Get-RegServerGroupReverseParse -object $newgroup)
$parentserver.ServerConnection.Disconnect()
Get-DbaRegServerGroup -SqlInstance $reggroup.ParentServer -Group (Get-RegServerGroupReverseParse -object $newgroup)
if ($parentserver.ServerConnection) {
$parentserver.ServerConnection.Disconnect()
}
} catch {
Stop-Function -Message "Failed to add $reggroup on $server" -ErrorRecord $_ -Continue
Stop-Function -Message "Failed to add $reggroup" -ErrorRecord $_ -Continue
}
}
}
Expand Down
28 changes: 21 additions & 7 deletions functions/Get-DbaRegServerGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,32 @@ function Get-DbaRegServerGroup {
#>
[CmdletBinding()]
param (
[parameter(Mandatory, ValueFromPipeline)]
[parameter(ValueFromPipeline)]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[object[]]$Group,
[object[]]$ExcludeGroup,
[int[]]$Id,
[switch]$EnableException
)
begin {
$serverstores = $groups = @()
}
process {
foreach ($instance in $SqlInstance) {
try {
$serverstore = Get-DbaRegServerStore -SqlInstance $instance -SqlCredential $SqlCredential -EnableException
$serverstores += Get-DbaRegServerStore -SqlInstance $instance -SqlCredential $SqlCredential -EnableException
} catch {
Stop-Function -Message "Cannot access Central Management Server '$instance'" -ErrorRecord $_ -Continue
}
}

$groups = @()
if (-not $SqlInstance) {
$serverstores += Get-DbaRegServerStore
}

if ($group) {
foreach ($serverstore in $serverstores) {
if ($Group) {
foreach ($currentgroup in $Group) {
Write-Message -Level Verbose -Message "Processing $currentgroup"
if ($currentgroup -is [Microsoft.SqlServer.Management.RegisteredServers.ServerGroup]) {
Expand Down Expand Up @@ -137,19 +144,26 @@ function Get-DbaRegServerGroup {
if ($Id) {
Write-Message -Level Verbose -Message "Filtering for id $Id. Id 1 = default."
if ($Id -eq 1) {
$groups = $serverstore.DatabaseEngineServerGroup | Where-Object Id -in $Id
$groups = $serverstore.DatabaseEngineServerGroup
} else {
$groups = $serverstore.DatabaseEngineServerGroup.GetDescendantRegisteredServers().Parent | Where-Object Id -in $Id
}
}
$serverstore.ServerConnection.Disconnect()
if ($serverstore.ServerConnection) {
$serverstore.ServerConnection.Disconnect()
}

foreach ($groupobject in $groups) {
Add-Member -Force -InputObject $groupobject -MemberType NoteProperty -Name ComputerName -value $serverstore.ComputerName
Add-Member -Force -InputObject $groupobject -MemberType NoteProperty -Name InstanceName -value $serverstore.InstanceName
Add-Member -Force -InputObject $groupobject -MemberType NoteProperty -Name SqlInstance -value $serverstore.SqlInstance
Add-Member -Force -InputObject $groupobject -MemberType NoteProperty -Name ParentServer -value $serverstore.ParentServer

Select-DefaultView -InputObject $groupobject -Property ComputerName, InstanceName, SqlInstance, Name, DisplayName, Description, ServerGroups, RegisteredServers
if ($groupobject.ComputerName) {
Select-DefaultView -InputObject $groupobject -Property ComputerName, InstanceName, SqlInstance, Name, DisplayName, Description, ServerGroups, RegisteredServers
} else {
Select-DefaultView -InputObject $groupobject -Property Name, DisplayName, Description, ServerGroups, RegisteredServers
}
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion functions/Get-DbaRegServerStore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Get-DbaRegServerStore {
#>
[CmdletBinding()]
param (
[parameter(Mandatory, ValueFromPipeline)]
[parameter(ValueFromPipeline)]
[DbaInstanceParameter[]]$SqlInstance,
[PSCredential]$SqlCredential,
[switch]$EnableException
Expand All @@ -67,5 +67,17 @@ function Get-DbaRegServerStore {
Add-Member -Force -InputObject $store -MemberType NoteProperty -Name ParentServer -value $server
Select-DefaultView -InputObject $store -ExcludeProperty ServerConnection, DomainInstanceName, DomainName, Urn, Properties, Metadata, Parent, ConnectionContext, PropertyMetadataChanged, PropertyChanged, ParentServer
}

# Magic courtesy of Mathias Jessen and David Shifflet
if (-not $PSBoundParameters.SqlInstance) {
$file = [Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore]::LocalFileStore.DomainInstanceName
if ($file) {
if ((Test-Path -Path $file)) {
$class = [Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore]
$initMethod = $class.GetMethod('InitChildObjects', [Reflection.BindingFlags]'Static,NonPublic')
$initMethod.Invoke($null, @($file))
}
}
}
}
}
4 changes: 2 additions & 2 deletions functions/Move-DbaRegServer.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function Move-DbaRegServer {
<#
.SYNOPSIS
Moves registered servers around SQL Server Central Management Server (CMS)
Moves registered servers around SQL Server Central Management Server (CMS). Local Registered Servers not currently supported.

.DESCRIPTION
Moves registered servers around SQL Server Central Management Server (CMS)
Moves registered servers around SQL Server Central Management Server (CMS). Local Registered Servers not currently supported.

.PARAMETER SqlInstance
The target SQL Server instance or instances.
Expand Down
4 changes: 2 additions & 2 deletions functions/Move-DbaRegServerGroup.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function Move-DbaRegServerGroup {
<#
.SYNOPSIS
Moves registered server groups around SQL Server Central Management Server (CMS).
Moves registered server groups around SQL Server Central Management Server (CMS). Local Registered Server Groups not currently supported.

.DESCRIPTION
Moves registered server groups around SQL Server Central Management Server (CMS).
Moves registered server groups around SQL Server Central Management Server (CMS). Local Registered Server Groups not currently supported.

.PARAMETER SqlInstance
The target SQL Server instance or instances.
Expand Down
Loading