Skip to content

Commit f2fb478

Browse files
Copy-DbaPolicyManagement: Add ObjectSets migration (#10220)
1 parent eb0aa1d commit f2fb478

1 file changed

Lines changed: 54 additions & 5 deletions

File tree

public/Copy-DbaPolicyManagement.ps1

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ function Copy-DbaPolicyManagement {
44
Copies Policy-Based Management policies, conditions, and categories between SQL Server instances
55
66
.DESCRIPTION
7-
Transfers your entire Policy-Based Management framework from one SQL Server instance to another, including custom policies, conditions, and categories. This streamlines environment standardization and disaster recovery scenarios where you need identical compliance policies across multiple servers.
7+
Transfers your entire Policy-Based Management framework from one SQL Server instance to another, including custom policies, conditions, object sets, and categories. This streamlines environment standardization and disaster recovery scenarios where you need identical compliance policies across multiple servers.
88
9-
By default, all non-system policies and conditions are copied. Existing objects on the destination are skipped unless -Force is used to overwrite them. You can selectively copy specific policies or conditions using the include/exclude parameters, which provide auto-completion from the source server.
9+
By default, all non-system policies, conditions, and object sets are copied. Object sets are migrated after conditions and before policies to ensure policy dependencies are satisfied. Existing objects on the destination are skipped unless -Force is used to overwrite them. You can selectively copy specific policies or conditions using the include/exclude parameters, which provide auto-completion from the source server.
1010
1111
.PARAMETER Source
1212
Specifies the source SQL Server instance containing the Policy-Based Management objects to copy. Must be SQL Server 2008 or higher with sysadmin access.
@@ -71,14 +71,14 @@ function Copy-DbaPolicyManagement {
7171
.OUTPUTS
7272
PSCustomObject
7373
74-
Returns one object per policy category, condition, and policy successfully copied or skipped. All objects use a common schema with the following properties:
74+
Returns one object per policy category, condition, object set, and policy successfully copied or skipped. All objects use a common schema with the following properties:
7575
7676
Default display properties (via Select-DefaultView):
7777
- DateTime: Timestamp of when the copy operation was attempted (DbaDateTime)
7878
- SourceServer: The source SQL Server instance name where the object was copied from
7979
- DestinationServer: The destination SQL Server instance name where the object was copied to
80-
- Name: The name of the policy category, condition, or policy that was processed
81-
- Type: The type of object being copied - one of "Policy Category", "Policy Condition", or "Policy"
80+
- Name: The name of the policy category, condition, object set, or policy that was processed
81+
- Type: The type of object being copied - one of "Policy Category", "Policy Condition", "Policy ObjectSet", or "Policy"
8282
- Status: The result of the operation - "Successful", "Skipped", or "Failed"
8383
- Notes: Additional information about the operation result, such as "Already exists on destination" or error details
8484
@@ -139,6 +139,7 @@ function Copy-DbaPolicyManagement {
139139
$sourceStore = New-Object Microsoft.SqlServer.Management.DMF.PolicyStore $sourceSqlStoreConnection
140140
$storePolicies = $sourceStore.Policies | Where-Object { $_.IsSystemObject -eq $false }
141141
$storeConditions = $sourceStore.Conditions | Where-Object { $_.IsSystemObject -eq $false }
142+
$storeObjectSets = $sourceStore.ObjectSets | Where-Object { $_.IsSystemObject -eq $false }
142143

143144
if ($Force) { $ConfirmPreference = 'none' }
144145
}
@@ -290,6 +291,54 @@ function Copy-DbaPolicyManagement {
290291
}
291292
}
292293

294+
<#
295+
ObjectSets
296+
#>
297+
298+
Write-Message -Level Verbose -Message "Migrating object sets"
299+
foreach ($objectSet in $storeObjectSets) {
300+
$objectSetName = $objectSet.Name
301+
302+
$copyObjectSetStatus = [PSCustomObject]@{
303+
SourceServer = $sourceServer.Name
304+
DestinationServer = $destServer.Name
305+
Name = $objectSetName
306+
Type = "Policy ObjectSet"
307+
Status = $null
308+
Notes = $null
309+
DateTime = [DbaDateTime](Get-Date)
310+
}
311+
312+
if ($null -ne $destStore.ObjectSets[$objectSetName]) {
313+
if ($Pscmdlet.ShouldProcess($destinstance, "ObjectSet '$objectSetName' was skipped because it already exists on $destinstance")) {
314+
Write-Message -Level Verbose -Message "ObjectSet '$objectSetName' was skipped because it already exists on $destinstance."
315+
$copyObjectSetStatus.Status = "Skipped"
316+
$copyObjectSetStatus.Notes = "Already exists on destination"
317+
$copyObjectSetStatus | Select-DefaultView -Property DateTime, SourceServer, DestinationServer, Name, Type, Status, Notes -TypeName MigrationObject
318+
}
319+
continue
320+
}
321+
322+
if ($Pscmdlet.ShouldProcess($destinstance, "Migrating object set $objectSetName")) {
323+
try {
324+
$sql = $objectSet.ScriptCreate().GetScript() | Out-String
325+
Write-Message -Level Debug -Message $sql
326+
Write-Message -Level Verbose -Message "Copying object set $objectSetName"
327+
$null = $destServer.Query($sql)
328+
$destStore.ObjectSets.Refresh()
329+
330+
$copyObjectSetStatus.Status = "Successful"
331+
$copyObjectSetStatus | Select-DefaultView -Property DateTime, SourceServer, DestinationServer, Name, Type, Status, Notes -TypeName MigrationObject
332+
} catch {
333+
$copyObjectSetStatus.Status = "Failed"
334+
$copyObjectSetStatus.Notes = (Get-ErrorMessage -Record $_).Message
335+
$copyObjectSetStatus | Select-DefaultView -Property DateTime, SourceServer, DestinationServer, Name, Type, Status, Notes -TypeName MigrationObject
336+
Write-Message -Level Verbose -Message "Issue creating object set $objectSetName on $destinstance | $PSItem"
337+
continue
338+
}
339+
}
340+
}
341+
293342
<#
294343
Policies
295344
#>

0 commit comments

Comments
 (0)