Skip to content

Commit 1a0587e

Browse files
authored
Merge pull request #1437 from ChristophHannappel/fix/SPCertificateSettings
Fixes Compare-Object Exception if there are currently no CertificateN…
2 parents c8281f1 + b310e7d commit 1a0587e

File tree

3 files changed

+83
-15
lines changed

3 files changed

+83
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- SPFarm
1616
- Updated to run cmdlet `Update-SPFlightsConfigFile` on SharePoint Subscription.
1717

18+
### Fixed
19+
20+
- SPCertificateSettings
21+
- Fixed an error where the command failed to add
22+
SPCertificateNotificationContacts when there are currently none set.
23+
1824
## [5.4.0] - 2023-04-04
1925

2026
### Fixed

SharePointDsc/DSCResources/MSFT_SPCertificateSettings/MSFT_SPCertificateSettings.psm1

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -285,22 +285,35 @@ function Set-TargetResource
285285
if ($contactsProvided)
286286
{
287287
Write-Verbose "Checking Certificate Notification Contacts"
288-
$currentContacts = [array](Get-SPCertificateNotificationContact).Address
288+
[array]$currentContacts = Get-SPCertificateNotificationContact
289289

290-
$diffs = Compare-Object -ReferenceObject $desiredContacts -DifferenceObject $currentContacts
291-
foreach ($diff in $diffs)
290+
# If there aren't any current Contacts we'll add them all. Also Compare-Object does not like $null Objects.
291+
# This fixes Issue "SPCertificateSettings: Unable to set contacts when previously blank" https://github.com/dsccommunity/SharePointDsc/issues/1430
292+
if ($currentContacts.Count -eq 0)
292293
{
293-
switch ($diff.SideIndicator)
294+
foreach ($contact in $desiredContacts)
294295
{
295-
"<="
296-
{
297-
Write-Verbose "Adding $($diff.InputObject)"
298-
$null = Add-SPCertificateNotificationContact -EmailAddress $diff.InputObject
299-
}
300-
"=>"
296+
Write-Verbose "Adding $($diff.InputObject)"
297+
$null = Add-SPCertificateNotificationContact -EmailAddress $contact
298+
}
299+
}
300+
else
301+
{
302+
$diffs = Compare-Object -ReferenceObject $desiredContacts -DifferenceObject ($currentContacts | Select-Object -ExpandProperty Address)
303+
foreach ($diff in $diffs)
304+
{
305+
switch ($diff.SideIndicator)
301306
{
302-
Write-Verbose "Removing $($diff.InputObject)"
303-
$null = Remove-SPCertificateNotificationContact -EmailAddress $diff.InputObject -Confirm:$false
307+
"<="
308+
{
309+
Write-Verbose "Adding $($diff.InputObject)"
310+
$null = Add-SPCertificateNotificationContact -EmailAddress $diff.InputObject
311+
}
312+
"=>"
313+
{
314+
Write-Verbose "Removing $($diff.InputObject)"
315+
$null = Remove-SPCertificateNotificationContact -EmailAddress $diff.InputObject -Confirm:$false
316+
}
304317
}
305318
}
306319
}

tests/Unit/SharePointDsc/SharePointDsc.SPCertificateSettings.Tests.ps1

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function Invoke-TestSetup
3333

3434
$script:testEnvironment = Initialize-TestEnvironment `
3535
-DSCModuleName $script:DSCModuleName `
36-
-DSCResourceName $script:DSCResourceFullName `
36+
-DscResourceName $script:DSCResourceFullName `
3737
-ResourceType 'Mof' `
3838
-TestType 'Unit'
3939
}
@@ -260,7 +260,7 @@ try
260260
CertificateExpirationWarningThresholdDays = 15
261261
CertificateExpirationErrorThresholdDays = 15
262262
CertificateNotificationContacts = @(
263-
@{
263+
[PSCustomObject]@{
264264
Address = 'wrong@contoso.com'
265265
}
266266
)
@@ -270,7 +270,7 @@ try
270270
Mock -CommandName Get-SPFarm -MockWith { return @{ } }
271271
Mock -CommandName Get-SPCertificateNotificationContact -MockWith {
272272
return @(
273-
@{
273+
[PSCustomObject]@{
274274
Address = 'wrong@contoso.com'
275275
}
276276
)
@@ -295,6 +295,55 @@ try
295295
}
296296
}
297297

298+
Context -Name "The server is in a farm and zero contacts have been applied" -Fixture {
299+
BeforeAll {
300+
$testParams = @{
301+
IsSingleInstance = 'Yes'
302+
CertificateNotificationContacts = 'admin@contoso.com'
303+
}
304+
305+
Mock -CommandName Get-SPCertificateSettings -MockWith {
306+
$returnVal = @{
307+
DefaultOrganizationalUnit = ''
308+
DefaultOrganization = ''
309+
DefaultLocality = ''
310+
DefaultState = ''
311+
DefaultCountry = ''
312+
DefaultKeyAlgorithm = 'RSA'
313+
DefaultRsaKeySize = 2048
314+
DefaultEllipticCurve = 'nistP256'
315+
DefaultHashAlgorithm = 'SHA256'
316+
DefaultRsaSignaturePadding = 'Pkcs1'
317+
CertificateExpirationAttentionThresholdDays = 60
318+
CertificateExpirationWarningThresholdDays = 15
319+
CertificateExpirationErrorThresholdDays = 15
320+
CertificateNotificationContacts = [System.Net.Mail.MailAddressCollection]::new()
321+
}
322+
return $returnVal
323+
}
324+
Mock -CommandName Get-SPFarm -MockWith { return @{ } }
325+
Mock -CommandName Get-SPCertificateNotificationContact -MockWith {
326+
return [System.Net.Mail.MailAddressCollection]::new()
327+
}
328+
Mock -CommandName Add-SPCertificateNotificationContact -MockWith {}
329+
Mock -CommandName Remove-SPCertificateNotificationContact -MockWith {}
330+
}
331+
332+
It "Should return values from the get method" {
333+
$result = Get-TargetResource @testParams
334+
$result.CertificateNotificationContacts.Count | Should -Be 0
335+
}
336+
337+
It "Should return false from the test method" {
338+
Test-TargetResource @testParams | Should -Be $false
339+
}
340+
341+
It "Should update the certificate settings" {
342+
Set-TargetResource @testParams
343+
Assert-MockCalled Add-SPCertificateNotificationContact
344+
}
345+
}
346+
298347
Context -Name "The server is in a farm and the correct settings have been applied" -Fixture {
299348
BeforeAll {
300349
$testParams = @{

0 commit comments

Comments
 (0)