Skip to content

Commit 79b9383

Browse files
Merge pull request #6377 from FabienTschanz/feat/sensitivity-label-export
Reduce export time of sensitivity label
2 parents 2d2a543 + 41e226e commit 79b9383

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
* SCAutoSensitivityLabelRule
3636
* Updated supported values for `Workload` property.
3737
FIXES [#4617](https://github.com/microsoft/Microsoft365DSC/issues/4617)
38+
* SCSensitivityLabel
39+
* Reduced export time by 90%
3840
* TeamsEventsPolicy
3941
* Added 7 new properties.
4042
FIXES [#6403](https://github.com/microsoft/Microsoft365DSC/issues/6403)

Modules/Microsoft365DSC/DSCResources/MSFT_SCSensitivityLabel/MSFT_SCSensitivityLabel.psm1

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,16 @@ function Get-TargetResource
329329

330330
try
331331
{
332-
$label = Get-Label -Identity $Name -ErrorAction SilentlyContinue `
333-
-IncludeDetailedLabelActions
332+
if ($null -eq $Script:AllLabels)
333+
{
334+
[array]$Script:AllLabels = Get-Label -IncludeDetailedLabelActions
335+
}
336+
$label = $Script:AllLabels | Where-Object { $_.Name -eq $Name }
337+
338+
if ($null -eq $label)
339+
{
340+
$label = Get-Label -Identity $Name -IncludeDetailedLabelActions -ErrorAction SilentlyContinue
341+
}
334342
}
335343
catch
336344
{
@@ -351,7 +359,12 @@ function Get-TargetResource
351359
$parentLabelID = $null
352360
if ($null -ne $label.ParentId)
353361
{
354-
$parentLabel = Get-Label -Identity $label.ParentId -IncludeDetailedLabelActions -ErrorAction 'SilentlyContinue'
362+
$parentLabel = $Script:AllLabels | Where-Object { $_.Name -eq $label.ParentId }
363+
if ($null -eq $parentLabel)
364+
{
365+
$parentLabel = Get-Label -Identity $label.ParentId -IncludeDetailedLabelActions -ErrorAction SilentlyContinue
366+
$Script:AllLabels += $parentLabel
367+
}
355368
$parentLabelID = $parentLabel.Name
356369
}
357370
if ($null -ne $label.LocaleSettings)
@@ -1588,26 +1601,26 @@ function Export-TargetResource
15881601

15891602
try
15901603
{
1591-
[array]$labels = Get-Label -ErrorAction Stop
1604+
[array]$Script:AllLabels = Get-Label -IncludeDetailedLabelActions -ErrorAction Stop
15921605

15931606
$dscContent = ''
15941607
$i = 1
1595-
if ($labels.Length -eq 0)
1608+
if ($Script:AllLabels.Count -eq 0)
15961609
{
15971610
Write-M365DSCHost -Message $Global:M365DSCEmojiGreenCheckMark -CommitWrite
15981611
}
15991612
else
16001613
{
16011614
Write-M365DSCHost -Message "`r`n" -DeferWrite
16021615
}
1603-
foreach ($label in $labels)
1616+
foreach ($label in $Script:AllLabels)
16041617
{
16051618
if ($null -ne $Global:M365DSCExportResourceInstancesCount)
16061619
{
16071620
$Global:M365DSCExportResourceInstancesCount++
16081621
}
16091622

1610-
Write-M365DSCHost -Message " |---[$i/$($labels.Count)] $($label.Name)" -DeferWrite
1623+
Write-M365DSCHost -Message " |---[$i/$($Script:AllLabels.Count)] $($label.Name)" -DeferWrite
16111624

16121625
$Script:exportedInstance = $label
16131626
$Results = Get-TargetResource @PSBoundParameters -Name $label.Name

Tests/Unit/Microsoft365DSC/Microsoft365DSC.SCSensitivityLabel.Tests.ps1

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
6262
)
6363
}
6464

65+
Mock -CommandName Get-Label -MockWith {
66+
return @(
67+
@{
68+
Name = 'TestLabel'
69+
Comment = 'This is a test label'
70+
ToolTip = 'Test tool tip'
71+
DisplayName = 'Test label'
72+
ParentId = 'MyLabel'
73+
Priority = '2'
74+
Settings = '[LabelStatus, Enabled]'
75+
LocaleSettings = '{"LocaleKey":"DisplayName","Settings":[{"Key":"en-us","Value":"English DisplayName"}]}'
76+
Conditions = '{"And":[{"Or":[{"Key":"CCSI","Value":"cb353f78-2b72-4c3c-8827-92ebe4f69fdf","Properties":null,"Settings":[{"Key":"mincount","Value":"1"},{"Key":"maxconfidence","Value":"100"},{"Key":"rulepackage","Value":"00000000-0000-0000-0000-000000000000"},{"Key":"name","Value":"ABA Routing Number"},{"Key":"groupname","Value":"Group1"},{"Key":"minconfidence","Value":"85"},{"Key":"maxcount","Value":"-1"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"confidencelevel","Value":"High"},{"Key":"autoapplytype","Value":"Recommend"}]},{"Key":"ContentMatchesModule","Value":"ba38aa0f-8c86-4c73-87db-95147a0f4420","Properties":null,"Settings":[{"Key":"name","Value":"Legal Affairs"},{"Key":"groupname","Value":"Group1"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"autoapplytype","Value":"Recommend"}]}]},{"And":[{"Key":"CCSI","Value":"50b8b56b-4ef8-44c2-a924-03374f5831ce","Properties":null,"Settings":[{"Key":"mincount","Value":"10"},{"Key":"maxconfidence","Value":"100"},{"Key":"rulepackage","Value":"00000000-0000-0000-0000-000000000004"},{"Key":"name","Value":"All Full Names"},{"Key":"groupname","Value":"Group2"},{"Key":"minconfidence","Value":"85"},{"Key":"maxcount","Value":"100"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"confidencelevel","Value":"High"},{"Key":"autoapplytype","Value":"Recommend"}]},{"Key":"ContentMatchesModule","Value":"ba38aa0f-8c86-4c73-87db-95147a0f4420","Properties":null,"Settings":[{"Key":"name","Value":"Legal Affairs"},{"Key":"groupname","Value":"Group2"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"autoapplytype","Value":"Recommend"}]}]}]}'
77+
}
78+
@{
79+
Name = 'MyLabel'
80+
}
81+
)
82+
}
83+
6584
# Mock Write-M365DSCHost to hide output during the tests
6685
Mock -CommandName Write-M365DSCHost -MockWith {
6786
}
@@ -156,7 +175,7 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
156175
BeforeAll {
157176
$testParams = @{
158177
Name = 'TestLabel'
159-
Comment = 'This is a test label'
178+
Comment = 'Updated comment' # Drift
160179
ToolTip = 'Test tool tip'
161180
DisplayName = 'Test label'
162181
ParentId = 'MyLabel'
@@ -219,20 +238,6 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
219238
Credential = $Credential
220239
Ensure = 'Present'
221240
}
222-
223-
Mock -CommandName Get-Label -MockWith {
224-
return @{
225-
Name = 'TestLabel'
226-
Comment = 'Updated comment'
227-
ToolTip = 'Test tool tip'
228-
DisplayName = 'Test label'
229-
ParentId = 'MyLabel'
230-
Priority = '2'
231-
Settings = '[LabelStatus, Enabled]'
232-
LocaleSettings = '{"LocaleKey":"DisplayName","Settings":[{"Key":"en-us","Value":"English Display Names"}]}'
233-
Conditions = '{"And":[{"Or":[{"Key":"CCSI","Value":"cb353f78-2b72-4c3c-8827-92ebe4f69fdf","Properties":null,"Settings":[{"Key":"mincount","Value":"1"},{"Key":"maxconfidence","Value":"100"},{"Key":"rulepackage","Value":"00000000-0000-0000-0000-000000000000"},{"Key":"name","Value":"ABA Routing Number"},{"Key":"groupname","Value":"Group1"},{"Key":"minconfidence","Value":"85"},{"Key":"maxcount","Value":"-1"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"confidencelevel","Value":"High"},{"Key":"autoapplytype","Value":"Recommend"}]},{"Key":"ContentMatchesModule","Value":"ba38aa0f-8c86-4c73-87db-95147a0f4420","Properties":null,"Settings":[{"Key":"name","Value":"Legal Affairs"},{"Key":"groupname","Value":"Group1"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"autoapplytype","Value":"Recommend"}]}]},{"And":[{"Key":"CCSI","Value":"50b8b56b-4ef8-44c2-a924-03374f5831ce","Properties":null,"Settings":[{"Key":"mincount","Value":"10"},{"Key":"maxconfidence","Value":"100"},{"Key":"rulepackage","Value":"00000000-0000-0000-0000-000000000004"},{"Key":"name","Value":"All Full Names"},{"Key":"groupname","Value":"Group2"},{"Key":"minconfidence","Value":"85"},{"Key":"maxcount","Value":"100"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"confidencelevel","Value":"High"},{"Key":"autoapplytype","Value":"Recommend"}]},{"Key":"ContentMatchesModule","Value":"ba38aa0f-8c86-4c73-87db-95147a0f4420","Properties":null,"Settings":[{"Key":"name","Value":"Legal Affairs"},{"Key":"groupname","Value":"Group2"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"autoapplytype","Value":"Recommend"}]}]}]}'
234-
}
235-
}
236241
}
237242

238243
It 'Should return false from the Test method' {
@@ -266,7 +271,7 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
266271
LocaleKey = 'DisplayName'
267272
LabelSettings = (New-CimInstance -ClassName MSFT_SCLabelSetting -Property @{
268273
Key = 'en-us'
269-
Value = 'English Display Names'
274+
Value = 'English DisplayName'
270275
} -ClientOnly)
271276
} -ClientOnly)
272277

@@ -315,37 +320,17 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
315320
Credential = $Credential
316321
Ensure = 'Present'
317322
}
318-
319-
Mock -CommandName Get-Label -MockWith {
320-
return @{
321-
Name = 'TestLabel'
322-
Comment = 'This is a test label'
323-
ToolTip = 'Test tool tip'
324-
DisplayName = 'Test label'
325-
ParentId = 'MyLabel'
326-
Priority = '2'
327-
Settings = '[LabelStatus, Enabled]'
328-
LocaleSettings = '{"LocaleKey":"DisplayName","Settings":[{"Key":"en-us","Value":"English Display Names"}]}'
329-
Conditions = '{"And":[{"Or":[{"Key":"CCSI","Value":"cb353f78-2b72-4c3c-8827-92ebe4f69fdf","Properties":null,"Settings":[{"Key":"mincount","Value":"1"},{"Key":"maxconfidence","Value":"100"},{"Key":"rulepackage","Value":"00000000-0000-0000-0000-000000000000"},{"Key":"name","Value":"ABA Routing Number"},{"Key":"groupname","Value":"Group1"},{"Key":"minconfidence","Value":"85"},{"Key":"maxcount","Value":"-1"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"confidencelevel","Value":"High"},{"Key":"autoapplytype","Value":"Recommend"}]},{"Key":"ContentMatchesModule","Value":"ba38aa0f-8c86-4c73-87db-95147a0f4420","Properties":null,"Settings":[{"Key":"name","Value":"Legal Affairs"},{"Key":"groupname","Value":"Group1"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"autoapplytype","Value":"Recommend"}]}]},{"And":[{"Key":"CCSI","Value":"50b8b56b-4ef8-44c2-a924-03374f5831ce","Properties":null,"Settings":[{"Key":"mincount","Value":"10"},{"Key":"maxconfidence","Value":"100"},{"Key":"rulepackage","Value":"00000000-0000-0000-0000-000000000004"},{"Key":"name","Value":"All Full Names"},{"Key":"groupname","Value":"Group2"},{"Key":"minconfidence","Value":"85"},{"Key":"maxcount","Value":"100"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"confidencelevel","Value":"High"},{"Key":"autoapplytype","Value":"Recommend"}]},{"Key":"ContentMatchesModule","Value":"ba38aa0f-8c86-4c73-87db-95147a0f4420","Properties":null,"Settings":[{"Key":"name","Value":"Legal Affairs"},{"Key":"groupname","Value":"Group2"},{"Key":"policytip","Value":"My Perfect Test Tip!"},{"Key":"autoapplytype","Value":"Recommend"}]}]}]}'
330-
}
331-
} -ParameterFilter { $Identity -eq 'TestLabel' }
332-
333-
Mock -CommandName Get-Label -MockWith {
334-
return @{
335-
Name = 'MyLabel'
336-
}
337-
} -ParameterFilter { $Identity -eq 'MyLabel' }
338323
}
339324

340-
It 'Should return false from the Test method' {
325+
It 'Should return true from the Test method' {
341326
Test-TargetResource @testParams | Should -Be $true
342327
}
343328
}
344329

345330
Context -Name 'Label should not exist' -Fixture {
346331
BeforeAll {
347332
$testParams = @{
348-
Name = 'TestLabel'
333+
Name = 'TestLabel_DoesNotExist'
349334
ParentId = 'MyLabel'
350335
Credential = $Credential
351336
Ensure = 'Absent'
@@ -356,7 +341,7 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
356341

357342
Mock -CommandName Get-Label -MockWith {
358343
return @{
359-
Name = 'TestLabel'
344+
Name = 'TestLabel_DoesNotExist'
360345
ParentId = 'MyLabel'
361346
}
362347
}

0 commit comments

Comments
 (0)