From 3a7a356232b7d87d63717a771f163b8c0f50f85e Mon Sep 17 00:00:00 2001 From: Nicholas Ellis Date: Mon, 20 Jul 2020 13:39:39 -0400 Subject: [PATCH 1/2] Initial Sync --- CHANGELOG.md | 1 + README.md | 37 ++ source/ConfigMgrCBDsc.psd1 | 3 +- .../DSC_CMStatusReportingComponent.psm1 | 425 +++++++++++++++ .../DSC_CMStatusReportingComponent.schema.mof | 17 + ...SC_CMStatusReportingComponent.strings.psd1 | 9 + .../CMStatusReportingComponent_Example.ps1 | 28 + .../Unit/CMStatusReportingComponent.tests.ps1 | 506 ++++++++++++++++++ 8 files changed, 1025 insertions(+), 1 deletion(-) create mode 100644 source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.psm1 create mode 100644 source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.schema.mof create mode 100644 source/DSCResources/DSC_CMStatusReportingComponent/en-US/DSC_CMStatusReportingComponent.strings.psd1 create mode 100644 source/Examples/Resources/CMStatusReportingComponent/CMStatusReportingComponent_Example.ps1 create mode 100644 tests/Unit/CMStatusReportingComponent.tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index a8fc0bf..3591ff4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added Compare-MultipleCompares to the ResourceHelper - Added CMDistributionGroup Resource - Added CMSiteSystemServer Resource +- Added CMStatusReportingComponent Resource ### Changed diff --git a/README.md b/README.md index beb775d..c65defb 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,8 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu Groups and adding Distribution Points to the group. - **CMSiteSystemServer**: Provides a resource for adding and modifying a Site System Server and its properties. +- **CMStatusReportingComponent**: Provides a resource for modifying the Status + Reporting Component and its properties. ### xSccmPreReqs @@ -953,3 +955,38 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu - [CMSiteSystemServer_Present](Source\Examples\Resources\CMSiteSystemServer\CMSiteSystemServer_Present.ps1) - [CMSiteSystemServer_Absent](Source\Examples\Resources\CMSiteSystemServer\CMSiteSystemServer_Absent.ps1) + +### CMStatusReportingComponent + +- **[String] SiteCode** _(Key)_: Specifies the Site Code for the Configuration + Manager site. +- **[Boolean] ClientLogChecked** _(Write)_: Indicates whether a client log is checked. +- **[Boolean] ClientLogFailureChecked** _(Write)_: Indicates whether a client log + failure is checked. +- **[String] ClientLogType** _(Write)_: Specifies a client log type. + - Values include: { AllMilestones | AllMilestonesAndAllDetails | + ErrorAndWarningMilestones | ErrorMilestones } +- **[Boolean] ClientReportChecked** _(Write)_: Indicates whether a client report + is checked. +- **[Boolean] ClientReportFailureChecked** _(Write)_: Indicates whether a client + failure is checked. +- **[String] ClientReportType** _(Write)_: Specifies a client report type. + - Values include: { AllMilestones | AllMilestonesAndAllDetails | + ErrorAndWarningMilestones | ErrorMilestones } +- **[Boolean] ServerLogChecked** _(Write)_: Indicates whether a server log is checked. +- **[Boolean] ServerLogFailureChecked** _(Write)_: Indicates whether a server log + failure is checked. +- **[String] ServerLogType** _(Write)_: Specifies a server log type. + - Values include: { AllMilestones | AllMilestonesAndAllDetails | + ErrorAndWarningMilestones | ErrorMilestones } +- **[Boolean] ServerReportChecked** _(Write)_: Indicates whether a server report + is checked. +- **[Boolean] ServerReportFailureChecked** _(Write)_: Indicates whether a server + report failure is checked. +- **[String] ServerReportType** _(Write)_: Specifies a server report type. + - Values include: { AllMilestones | AllMilestonesAndAllDetails | + ErrorAndWarningMilestones | ErrorMilestones } + +#### CMStatusReportingComponent Examples + +- [CMStatusReportingComponent_Example](Source\Examples\Resources\CMStatusReportingComponent\CMStatusReportingComponent_Example.ps1) diff --git a/source/ConfigMgrCBDsc.psd1 b/source/ConfigMgrCBDsc.psd1 index 161e54b..d8b5180 100644 --- a/source/ConfigMgrCBDsc.psd1 +++ b/source/ConfigMgrCBDsc.psd1 @@ -69,6 +69,7 @@ 'CMAdministrativeUser' 'CMDistributionGroup' 'CMSiteSystemServer' + 'CMStatusReportingComponent' ) <# @@ -85,7 +86,7 @@ 'SccmSqlSetup','SCCMInstall','CMIniFile','Collections','Boundaries','ForestDiscovery','ClientStatusSettings','BoundaryGroups', 'ManagementPoint','AssetIntelligencePoint','FallbackStatusPoint','SoftwareUpdatePoint','DistrubtionPoint','HeartbeatDiscovery', 'ServiceConnectionPoint','NetworkDiscovery','ReportingServicePoint','SystemDiscovery','PXEDistributionPoint','PullDistributionPoint', - 'SiteMaintenance','AdministrativeUser','DistributionGroup','SiteSystemServer') + 'SiteMaintenance','AdministrativeUser','DistributionGroup','SiteSystemServer','StatusReportingComponent') # A URL to the license for this module. LicenseUri = 'https://github.com/dsccommunity/ConfigMgrCBDsc/blob/master/LICENSE' diff --git a/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.psm1 b/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.psm1 new file mode 100644 index 0000000..83982e9 --- /dev/null +++ b/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.psm1 @@ -0,0 +1,425 @@ +$script:dscResourceCommonPath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\DscResource.Common' +$script:configMgrResourcehelper = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\ConfigMgrCBDsc.ResourceHelper' + +Import-Module -Name $script:dscResourceCommonPath +Import-Module -Name $script:configMgrResourcehelper + +$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' + +<# + .SYNOPSIS + This will return a hashtable of results. + + .PARAMETER SiteCode + Specifies the site code for Configuration Manager site. + +#> +function Get-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param + ( + [Parameter(Mandatory = $true)] + [String] + $SiteCode + ) + + Write-Verbose -Message $script:localizedData.RetrieveSettingValue + Import-ConfigMgrPowerShellModule -SiteCode $SiteCode + Set-Location -Path "$($SiteCode):\" + + $clientComponentStatus = Get-CMStatusReportingComponent -SiteCode $SiteCode | Where-Object -FilterScript {$_.ItemName -eq 'Client Component Status Reporting'} + $serverComponentStatus = Get-CMStatusReportingComponent -SiteCode $SiteCode | Where-Object -FilterScript {$_.ItemName -eq 'Server Component Status Reporting'} + + foreach ($prop in $clientComponentStatus.props) + { + switch ($prop.PropertyName) + { + 'Default Status Message Reporting Level' { + $clientReporting = ($prop.Value2 -split ",") + $cReportType = $clientReporting[0] + $cReportChecked = $true + switch ($cReportType) + { + 'NONE' { $cReportChecked = $false } + 'EWI' { $cReportType = 'AllMilestones' } + 'All' { $cReportType = 'AllMilestonesAndAllDetails' } + 'EW' { $cReportType = 'ErrorAndWarningMilestones' } + 'E' { $cReportType = 'ErrorMilestones' } + } + $cReportFailure = [System.Convert]::ToBoolean($clientReporting[1]) + } + 'Default Windows NT Event Reporting Level' { + $clientLogging = ($prop.Value2 -split ",") + $cLogType = $clientLogging[0] + $cLogChecked = $true + switch ($cLogType) + { + 'NONE' { $cLogChecked = $false } + 'EWI' { $cLogType = 'AllMilestones' } + 'All' { $cLogType = 'AllMilestonesAndAllDetails' } + 'EW' { $cLogType = 'ErrorAndWarningMilestones' } + 'E' { $cLogType = 'ErrorMilestones' } + } + $cLogFailure = [System.Convert]::ToBoolean($clientLogging[1]) + } + } + } + + foreach ($prop in $serverComponentStatus.props) + { + switch ($prop.PropertyName) + { + 'Default Status Message Reporting Level' { + $serverReporting = ($prop.Value1 -split ",") + $sReportType = $serverReporting[0] + $sReportChecked = $true + switch ($sReportType) + { + 'NONE' { $sReportChecked = $false } + 'EWI' { $sReportType = 'AllMilestones' } + 'All' { $sReportType = 'AllMilestonesAndAllDetails' } + 'EW' { $sReportType = 'ErrorAndWarningMilestones' } + 'E' { $sReportType = 'ErrorMilestones' } + } + $sReportFailure = [System.Convert]::ToBoolean($serverReporting[1]) + } + 'Default Windows NT Event Reporting Level' { + $serverLogging = ($prop.Value1 -split ",") + $sLogType = $serverLogging[0] + $sLogChecked = $true + switch ($sLogType) + { + 'NONE' { $sLogChecked = $false } + 'EWI' { $sLogType = 'AllMilestones' } + 'All' { $sLogType = 'AllMilestonesAndAllDetails' } + 'EW' { $sLogType = 'ErrorAndWarningMilestones' } + 'E' { $sLogType = 'ErrorMilestones' } + } + $sLogFailure = [System.Convert]::ToBoolean($serverLogging[1]) + } + } + } + + return @{ + SiteCode = $SiteCode + ClientLogChecked = $cLogChecked + ClientLogFailureChecked = $cLogFailure + ClientLogType = $cLogType + ClientReportChecked = $cReportChecked + ClientReportFailureChecked = $cReportFailure + ClientReportType = $cReportType + ServerLogChecked = $sLogChecked + ServerLogFailureChecked = $sLogFailure + ServerLogType = $sLogType + ServerReportChecked = $sReportChecked + ServerReportFailureChecked = $sReportFailure + ServerReportType = $sReportType + } +} + +<# + .SYNOPSIS + This will set the desired state. + + .PARAMETER SiteCode + Specifies the site code for Configuration Manager site. + + .PARAMETER ClientLogChecked + Indicates whether a client log is checked. + + .PARAMETER ClientLogFailureChecked + Indicates whether a client log failure is checked. + + .PARAMETER ClientLogType + Specifies a client log type. The acceptable values are: + + AllMilestones, AllMilestonesAndAllDetails, ErrorAndWarningMilestones, ErrorMilestones + + .PARAMETER ClientReportChecked + Indicates whether a client report is checked. + + .PARAMETER ClientReportFailureChecked + Indicates whether a client failure is checked. + + .PARAMETER ClientReportType + Specifies a client report type. The acceptable values are: + + AllMilestones, AllMilestonesAndAllDetails, ErrorAndWarningMilestones, ErrorMilestones + + .PARAMETER ServerLogChecked + Indicates whether a server log is checked. + + .PARAMETER ServerLogFailureChecked + Indicates whether a server log failure is checked. + + .PARAMETER ServerLogType + Specifies a server log type. The acceptable values are: + + AllMilestones, AllMilestonesAndAllDetails, ErrorAndWarningMilestones, ErrorMilestones + + .PARAMETER ServerReportChecked + Indicates whether a server report is checked. + + .PARAMETER ServerReportFailureChecked + Indicates whether a server report failure is checked. + + .PARAMETER ServerReportType + Specifies a server report type. The acceptable values are: + + AllMilestones, AllMilestonesAndAllDetails, ErrorAndWarningMilestones, ErrorMilestones +#> +function Set-TargetResource +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $SiteCode, + + [Parameter()] + [Boolean] + $ClientLogChecked, + + [Parameter()] + [Boolean] + $ClientLogFailureChecked, + + [Parameter()] + [ValidateSet('AllMilestones','AllMilestonesAndAllDetails','ErrorAndWarningMilestones','ErrorMilestones')] + [String] + $ClientLogType, + + [Parameter()] + [Boolean] + $ClientReportChecked, + + [Parameter()] + [Boolean] + $ClientReportFailureChecked, + + [Parameter()] + [ValidateSet('AllMilestones','AllMilestonesAndAllDetails','ErrorAndWarningMilestones','ErrorMilestones')] + [String] + $ClientReportType, + + [Parameter()] + [Boolean] + $ServerLogChecked, + + [Parameter()] + [Boolean] + $ServerLogFailureChecked, + + [Parameter()] + [ValidateSet('AllMilestones','AllMilestonesAndAllDetails','ErrorAndWarningMilestones','ErrorMilestones')] + [String] + $ServerLogType, + + [Parameter()] + [Boolean] + $ServerReportChecked, + + [Parameter()] + [Boolean] + $ServerReportFailureChecked, + + [Parameter()] + [ValidateSet('AllMilestones','AllMilestonesAndAllDetails','ErrorAndWarningMilestones','ErrorMilestones')] + [String] + $ServerReportType + ) + + Import-ConfigMgrPowerShellModule -SiteCode $SiteCode + Set-Location -Path "$($SiteCode):\" + $state = Get-TargetResource -SiteCode $SiteCode + + try + { + if ((($state.ClientLogChecked -eq $false) -and ($null -eq $ClientLogChecked) -and ($ClientLogType -or $ClientLogFailureChecked)) -or (($ClientLogChecked -eq $false) -and ($clientLogType -or $ClientLogFailureChecked))) + { + throw $script:localizedData.CLogThrow + } + + if ((($state.ClientReportChecked -eq $false) -and ($null -eq $ClientReportChecked) -and ($ClientReportType -or $ClientReportFailureChecked)) -or (($ClientReportChecked -eq $false) -and ($ClientReportType -or $ClientReportFailureChecked))) + { + throw $script:localizedData.CReportThrow + } + + if ((($state.ServerLogChecked -eq $false) -and ($null -eq $ServerLogChecked) -and ($ServerLogType -or $ServerLogFailureChecked)) -or (($ServerLogChecked -eq $false) -and ($ServerLogType -or $ServerLogFailureChecked))) + { + throw $script:localizedData.SLogThrow + } + + if ((($state.ServerReportChecked -eq $false) -and ($null -eq $ServerReportChecked) -and ($ServerReportType -or $ServerReportFailureChecked)) -or (($ServerReportChecked -eq $false) -and ($ServerReportType -or $ServerReportFailureChecked))) + { + throw $script:localizedData.SReportThrow + } + + $evalList = @('ClientLogChecked','ClientLogFailureChecked','ClientLogType','ClientReportChecked','ClientReportFailureChecked','ClientReportType' + 'ServerLogChecked','ServerLogFailureChecked','ServerLogType','ServerReportChecked','ServerReportFailureChecked','ServerReportType') + + foreach ($param in $PSBoundParameters.GetEnumerator()) + { + if ($evalList -contains $param.key) + { + if ($param.Value -ne $state[$param.key]) + { + Write-Verbose -Message ($script:localizedData.SettingValue -f $param.Key, $param.Value) + $buildingParams += @{ + $param.Key = $param.Value + } + } + } + } + + if ($buildingParams) + { + Set-CMStatusReportingComponent -SiteCode $SiteCode @buildingParams + } + + } + catch + { + throw $_ + } + finally + { + Set-Location -Path "$env:temp" + } +} + +<# + .SYNOPSIS + This will test the desired state. + + .PARAMETER SiteCode + Specifies the site code for Configuration Manager site. + + .PARAMETER ClientLogChecked + Indicates whether a client log is checked. + + .PARAMETER ClientLogFailureChecked + Indicates whether a client log failure is checked. + + .PARAMETER ClientLogType + Specifies a client log type. The acceptable values are: + + AllMilestones, AllMilestonesAndAllDetails, ErrorAndWarningMilestones, ErrorMilestones + + .PARAMETER ClientReportChecked + Indicates whether a client report is checked. + + .PARAMETER ClientReportFailureChecked + Indicates whether a client failure is checked. + + .PARAMETER ClientReportType + Specifies a client report type. The acceptable values are: + + AllMilestones, AllMilestonesAndAllDetails, ErrorAndWarningMilestones, ErrorMilestones + + .PARAMETER ServerLogChecked + Indicates whether a server log is checked. + + .PARAMETER ServerLogFailureChecked + Indicates whether a server log failure is checked. + + .PARAMETER ServerLogType + Specifies a server log type. The acceptable values are: + + AllMilestones, AllMilestonesAndAllDetails, ErrorAndWarningMilestones, ErrorMilestones + + .PARAMETER ServerReportChecked + Indicates whether a server report is checked. + + .PARAMETER ServerReportFailureChecked + Indicates whether a server report failure is checked. + + .PARAMETER ServerReportType + Specifies a server report type. The acceptable values are: + + AllMilestones, AllMilestonesAndAllDetails, ErrorAndWarningMilestones, ErrorMilestones +#> +function Test-TargetResource +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $SiteCode, + + [Parameter()] + [Boolean] + $ClientLogChecked, + + [Parameter()] + [Boolean] + $ClientLogFailureChecked, + + [Parameter()] + [ValidateSet('AllMilestones','AllMilestonesAndAllDetails','ErrorAndWarningMilestones','ErrorMilestones')] + [String] + $ClientLogType, + + [Parameter()] + [Boolean] + $ClientReportChecked, + + [Parameter()] + [Boolean] + $ClientReportFailureChecked, + + [Parameter()] + [ValidateSet('AllMilestones','AllMilestonesAndAllDetails','ErrorAndWarningMilestones','ErrorMilestones')] + [String] + $ClientReportType, + + [Parameter()] + [Boolean] + $ServerLogChecked, + + [Parameter()] + [Boolean] + $ServerLogFailureChecked, + + [Parameter()] + [ValidateSet('AllMilestones','AllMilestonesAndAllDetails','ErrorAndWarningMilestones','ErrorMilestones')] + [String] + $ServerLogType, + + [Parameter()] + [Boolean] + $ServerReportChecked, + + [Parameter()] + [Boolean] + $ServerReportFailureChecked, + + [Parameter()] + [ValidateSet('AllMilestones','AllMilestonesAndAllDetails','ErrorAndWarningMilestones','ErrorMilestones')] + [String] + $ServerReportType + ) + + Import-ConfigMgrPowerShellModule -SiteCode $SiteCode + Set-Location -Path "$($SiteCode):\" + $state = Get-TargetResource -SiteCode $SiteCode + $result = $true + + $testParams = @{ + CurrentValues = $state + DesiredValues = $PSBoundParameters + ValuesToCheck = @('ClientLogChecked','ClientLogFailureChecked','ClientLogType','ClientReportChecked','ClientReportFailureChecked','ClientReportType' + 'ServerLogChecked','ServerLogFailureChecked','ServerLogType','ServerReportChecked','ServerReportFailureChecked','ServerReportType') + } + + $result = Test-DscParameterState @testParams -Verbose -TurnOffTypeChecking + + Write-Verbose -Message ($script:localizedData.TestState -f $result) + return $result +} + +Export-ModuleMember -Function *-TargetResource diff --git a/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.schema.mof b/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.schema.mof new file mode 100644 index 0000000..ec3d87d --- /dev/null +++ b/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.schema.mof @@ -0,0 +1,17 @@ +[ClassVersion("1.0.0"), FriendlyName("CMStatusReportingComponent")] +class DSC_CMStatusReportingComponent: OMI_BaseResource +{ + [Key, Description("Specifies the SiteCode for the Configuration Manager site.")] String SiteCode; + [Write, Description("Indicates whether a client log is checked.")] Boolean ClientLogChecked; + [Write, Description("Indicates whether a client log failure is checked.")] Boolean ClientLogFailureChecked; + [Write, Description("Specifies a client log type."),ValueMap{"AllMilestones","AllMilestonesAndAllDetails","ErrorAndWarningMilestones","ErrorMilestones"},Values{"AllMilestones","AllMilestonesAndAllDetails","ErrorAndWarningMilestones","ErrorMilestones"}] String ClientLogType; + [Write, Description("Indicates whether a client report is checked.")] Boolean ClientReportChecked; + [Write, Description("Indicates whether a client failure is checked.")] Boolean ClientReportFailureChecked; + [Write, Description("Specifies a client report type."),ValueMap{"AllMilestones","AllMilestonesAndAllDetails","ErrorAndWarningMilestones","ErrorMilestones"},Values{"AllMilestones","AllMilestonesAndAllDetails","ErrorAndWarningMilestones","ErrorMilestones"}] String ClientReportType; + [Write, Description("Indicates whether a server log is checked.")] Boolean ServerLogChecked; + [Write, Description("Indicates whether a server log failure is checked.")] Boolean ServerLogFailureChecked; + [Write, Description("Specifies a server log type."),ValueMap{"AllMilestones","AllMilestonesAndAllDetails","ErrorAndWarningMilestones","ErrorMilestones"},Values{"AllMilestones","AllMilestonesAndAllDetails","ErrorAndWarningMilestones","ErrorMilestones"}] String ServerLogType; + [Write, Description("Indicates whether a server report is checked.")] Boolean ServerReportChecked; + [Write, Description("Indicates whether a server report failure is checked.")] Boolean ServerReportFailureChecked; + [Write, Description("Specifies a server report type."),ValueMap{"AllMilestones","AllMilestonesAndAllDetails","ErrorAndWarningMilestones","ErrorMilestones"},Values{"AllMilestones","AllMilestonesAndAllDetails","ErrorAndWarningMilestones","ErrorMilestones"}] String ServerReportType; +}; diff --git a/source/DSCResources/DSC_CMStatusReportingComponent/en-US/DSC_CMStatusReportingComponent.strings.psd1 b/source/DSCResources/DSC_CMStatusReportingComponent/en-US/DSC_CMStatusReportingComponent.strings.psd1 new file mode 100644 index 0000000..f1be728 --- /dev/null +++ b/source/DSCResources/DSC_CMStatusReportingComponent/en-US/DSC_CMStatusReportingComponent.strings.psd1 @@ -0,0 +1,9 @@ +ConvertFrom-StringData @' + RetrieveSettingValue = Getting results for the Configuration Manager Status Reporting Component. + TestState = Test-TargetResource compliance check returned: {0}. + SettingValue = Setting value: {0} to {1}. + CLogThrow = In order to set the Client Log settings, you must specify ClientLogChecked to be True. + CReportThrow = In order to set the Client Report settings, you must specify ClientReportChecked to be True. + SLogThrow = In order to set the Server Log settings, you must specify ServerLogChecked to be True. + SReportThrow = In order to set the Server Report settings, you must specify ServerReportChecked to be True. +'@ diff --git a/source/Examples/Resources/CMStatusReportingComponent/CMStatusReportingComponent_Example.ps1 b/source/Examples/Resources/CMStatusReportingComponent/CMStatusReportingComponent_Example.ps1 new file mode 100644 index 0000000..e42d60d --- /dev/null +++ b/source/Examples/Resources/CMStatusReportingComponent/CMStatusReportingComponent_Example.ps1 @@ -0,0 +1,28 @@ +<# + .SYNOPSIS + A DSC configuration script to configure the Status Reporting Component in Configuration Manager. +#> +Configuration Example +{ + Import-DscResource -ModuleName ConfigMgrCBDsc + + Node localhost + { + CMStatusReportingComponent ExampleSettings + { + SiteCode = 'LAB' + ClientLogChecked = $true + ClientLogFailureChecked = $true + ClientLogType = 'ErrorMilestones' + ClientReportChecked = $true + ClientReportFailureChecked = $true + ClientReportType = 'AllMilestones' + ServerLogChecked = $true + ServerLogFailureChecked = $true + ServerLogType = 'AllMilestones' + ServerReportChecked = $true + ServerReportFailureChecked = $true + ServerReportType = 'AllMilestones' + } + } +} diff --git a/tests/Unit/CMStatusReportingComponent.tests.ps1 b/tests/Unit/CMStatusReportingComponent.tests.ps1 new file mode 100644 index 0000000..6a6061c --- /dev/null +++ b/tests/Unit/CMStatusReportingComponent.tests.ps1 @@ -0,0 +1,506 @@ +param () + +$script:dscModuleName = 'ConfigMgrCBDsc' +$script:dscResourceName = 'DSC_CMStatusReportingComponent' + +function Invoke-TestSetup +{ + try + { + Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' + } + catch [System.IO.FileNotFoundException] + { + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' + } + + $script:testEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dscResourceName ` + -ResourceType 'Mof' ` + -TestType 'Unit' + + # Import Stub function + $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) + Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ConfigMgrCBDscStub.psm1') -Force -WarningAction SilentlyContinue +} + +function Invoke-TestCleanup +{ + Restore-TestEnvironment -TestEnvironment $script:testEnvironment +} + +Invoke-TestSetup + +#Begin Testing +try +{ + InModuleScope $script:dscResourceName { + Describe 'ConfigMgrCBDsc - DSC_CMStatusReportingComponent\Get-TargetResource' -Tag 'Get'{ + BeforeAll{ + $getInput = @{ + SiteCode = 'Lab' + } + + $getEWI = @( + @{ + SiteCode = 'Lab' + ItemName = 'Client Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value2 = 'EWI,True' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value2 = 'EWI,True' + } + ) + } + @{ + SiteCode = 'Lab' + ItemName = 'Server Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value1 = 'EWI,True' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value1 = 'EWI,True' + } + ) + } + ) + + $getAll = @( + @{ + SiteCode = 'Lab' + ItemName = 'Client Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value2 = 'All,True' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value2 = 'All,True' + } + ) + } + @{ + SiteCode = 'Lab' + ItemName = 'Server Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value1 = 'All,True' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value1 = 'All,True' + } + ) + } + ) + + $getEW = @( + @{ + SiteCode = 'Lab' + ItemName = 'Client Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value2 = 'EW,True' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value2 = 'EW,True' + } + ) + } + @{ + SiteCode = 'Lab' + ItemName = 'Server Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value1 = 'EW,True' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value1 = 'EW,True' + } + ) + } + ) + + $getE = @( + @{ + SiteCode = 'Lab' + ItemName = 'Client Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value2 = 'E,True' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value2 = 'E,True' + } + ) + } + @{ + SiteCode = 'Lab' + ItemName = 'Server Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value1 = 'E,True' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value1 = 'E,True' + } + ) + } + ) + + $getNone = @( + @{ + SiteCode = 'Lab' + ItemName = 'Client Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value2 = 'NONE,False' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value2 = 'NONE,False' + } + ) + } + @{ + SiteCode = 'Lab' + ItemName = 'Server Component Status Reporting' + Props = @( + @{ + PropertyName = 'Default Status Message Reporting Level' + Value1 = 'NONE,False' + } + @{ + PropertyName = 'Default Windows NT Event Reporting Level' + Value1 = 'NONE,False' + } + ) + } + ) + + Mock -CommandName Import-ConfigMgrPowerShellModule + Mock -CommandName Set-Location + } + + Context 'When retrieving status reporting component settings' { + + It 'Should return desired result for AllMilestones and failures checked' { + Mock -CommandName Get-CMStatusReportingComponent -MockWith { $getEWI } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.ClientLogChecked | Should -Be -ExpectedValue $true + $result.ClientLogFailureChecked | Should -Be -ExpectedValue $true + $result.ClientLogType | Should -Be -ExpectedValue 'AllMilestones' + $result.ClientReportChecked | Should -Be -ExpectedValue $true + $result.ClientReportFailureChecked | Should -Be -ExpectedValue $true + $result.ClientReportType | Should -Be -ExpectedValue 'AllMilestones' + $result.ServerLogChecked | Should -Be -ExpectedValue $true + $result.ServerLogFailureChecked | Should -Be -ExpectedValue $true + $result.ServerLogType | Should -Be -ExpectedValue 'AllMilestones' + $result.ServerReportChecked | Should -Be -ExpectedValue $true + $result.ServerReportFailureChecked | Should -Be -ExpectedValue $true + $result.ServerReportType | Should -Be -ExpectedValue 'AllMilestones' + } + + It 'Should return desired result for AllMilestonesAndAllDetails and failures checked' { + Mock -CommandName Get-CMStatusReportingComponent -MockWith { $getAll } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.ClientLogChecked | Should -Be -ExpectedValue $true + $result.ClientLogFailureChecked | Should -Be -ExpectedValue $true + $result.ClientLogType | Should -Be -ExpectedValue 'AllMilestonesAndAllDetails' + $result.ClientReportChecked | Should -Be -ExpectedValue $true + $result.ClientReportFailureChecked | Should -Be -ExpectedValue $true + $result.ClientReportType | Should -Be -ExpectedValue 'AllMilestonesAndAllDetails' + $result.ServerLogChecked | Should -Be -ExpectedValue $true + $result.ServerLogFailureChecked | Should -Be -ExpectedValue $true + $result.ServerLogType | Should -Be -ExpectedValue 'AllMilestonesAndAllDetails' + $result.ServerReportChecked | Should -Be -ExpectedValue $true + $result.ServerReportFailureChecked | Should -Be -ExpectedValue $true + $result.ServerReportType | Should -Be -ExpectedValue 'AllMilestonesAndAllDetails' + } + + It 'Should return desired result for ErrorAndWarningMilestones and failures checked' { + Mock -CommandName Get-CMStatusReportingComponent -MockWith { $getEW } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.ClientLogChecked | Should -Be -ExpectedValue $true + $result.ClientLogFailureChecked | Should -Be -ExpectedValue $true + $result.ClientLogType | Should -Be -ExpectedValue 'ErrorAndWarningMilestones' + $result.ClientReportChecked | Should -Be -ExpectedValue $true + $result.ClientReportFailureChecked | Should -Be -ExpectedValue $true + $result.ClientReportType | Should -Be -ExpectedValue 'ErrorAndWarningMilestones' + $result.ServerLogChecked | Should -Be -ExpectedValue $true + $result.ServerLogFailureChecked | Should -Be -ExpectedValue $true + $result.ServerLogType | Should -Be -ExpectedValue 'ErrorAndWarningMilestones' + $result.ServerReportChecked | Should -Be -ExpectedValue $true + $result.ServerReportFailureChecked | Should -Be -ExpectedValue $true + $result.ServerReportType | Should -Be -ExpectedValue 'ErrorAndWarningMilestones' + } + + It 'Should return desired result for ErrorMilestones and failures checked' { + Mock -CommandName Get-CMStatusReportingComponent -MockWith { $getE } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.ClientLogChecked | Should -Be -ExpectedValue $true + $result.ClientLogFailureChecked | Should -Be -ExpectedValue $true + $result.ClientLogType | Should -Be -ExpectedValue 'ErrorMilestones' + $result.ClientReportChecked | Should -Be -ExpectedValue $true + $result.ClientReportFailureChecked | Should -Be -ExpectedValue $true + $result.ClientReportType | Should -Be -ExpectedValue 'ErrorMilestones' + $result.ServerLogChecked | Should -Be -ExpectedValue $true + $result.ServerLogFailureChecked | Should -Be -ExpectedValue $true + $result.ServerLogType | Should -Be -ExpectedValue 'ErrorMilestones' + $result.ServerReportChecked | Should -Be -ExpectedValue $true + $result.ServerReportFailureChecked | Should -Be -ExpectedValue $true + $result.ServerReportType | Should -Be -ExpectedValue 'ErrorMilestones' + } + + It 'Should return desired result when all items are unchecked' { + Mock -CommandName Get-CMStatusReportingComponent -MockWith { $getNone } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.ClientLogChecked | Should -Be -ExpectedValue $false + $result.ClientLogFailureChecked | Should -Be -ExpectedValue $false + $result.ClientLogType | Should -Be -ExpectedValue 'NONE' + $result.ClientReportChecked | Should -Be -ExpectedValue $false + $result.ClientReportFailureChecked | Should -Be -ExpectedValue $false + $result.ClientReportType | Should -Be -ExpectedValue 'NONE' + $result.ServerLogChecked | Should -Be -ExpectedValue $false + $result.ServerLogFailureChecked | Should -Be -ExpectedValue $false + $result.ServerLogType | Should -Be -ExpectedValue 'NONE' + $result.ServerReportChecked | Should -Be -ExpectedValue $false + $result.ServerReportFailureChecked | Should -Be -ExpectedValue $false + $result.ServerReportType | Should -Be -ExpectedValue 'NONE' + } + } + } + + Describe 'ConfigMgrCBDsc - DSC_CMStatusReportingComponent\Set-TargetResource' -Tag 'Set'{ + BeforeAll{ + $getReturnAll = @{ + SiteCode = 'Lab' + ClientLogChecked = $true + ClientLogFailureChecked = $true + ClientLogType = 'AllMilestones' + ClientReportChecked = $true + ClientReportFailureChecked = $true + ClientReportType = 'AllMilestones' + ServerLogChecked = $true + ServerLogFailureChecked = $true + ServerLogType = 'AllMilestones' + ServerReportChecked = $true + ServerReportFailureChecked = $true + ServerReportType = 'AllMilestones' + } + + $inputMismatch = @{ + SiteCode = 'Lab' + ClientLogChecked = $true + ClientLogFailureChecked = $false + ClientLogType = 'AllMilestones' + ClientReportChecked = $true + ClientReportFailureChecked = $false + ClientReportType = 'AllMilestones' + ServerLogChecked = $true + ServerLogFailureChecked = $true + ServerLogType = 'ErrorAndWarningMilestones' + ServerReportChecked = $true + ServerReportFailureChecked = $true + ServerReportType = 'ErrorMilestones' + } + + $cLogMalformed = @{ + SiteCode = 'Lab' + ClientLogChecked = $false + ClientLogType = 'AllMilestones' + } + + $cLogThrow = 'In order to set the Client Log settings, you must specify ClientLogChecked to be True.' + + $cReportMalformed = @{ + SiteCode = 'Lab' + ClientReportChecked = $false + ClientReportType = 'AllMilestones' + } + + $cReportThrow = 'In order to set the Client Report settings, you must specify ClientReportChecked to be True.' + + $sLogMalformed = @{ + SiteCode = 'Lab' + ServerLogChecked = $false + ServerLogType = 'AllMilestones' + } + + $sLogThrow = 'In order to set the Server Log settings, you must specify ServerLogChecked to be True.' + + $sReportMalformed = @{ + SiteCode = 'Lab' + ServerReportChecked = $false + ServerReportType = 'AllMilestones' + } + + $sReportThrow = 'In order to set the Server Report settings, you must specify ServerReportChecked to be True.' + + Mock -CommandName Import-ConfigMgrPowerShellModule + Mock -CommandName Set-Location + Mock -CommandName Set-CMStatusReportingComponent + } + + Context 'When Set-TargetResource runs successfully' { + + It 'Should call expected commands when changing settings' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnAll } + + Set-TargetResource @inputMismatch + Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It + Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It + Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It + Assert-MockCalled Set-CMStatusReportingComponent -Exactly -Times 1 -Scope It + } + } + + Context 'When Set-TargetResource throws' { + + It 'Should call expected commands and throw if client logging settings are malformed' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnAll } + + { Set-TargetResource @cLogMalformed } | Should -Throw -ExpectedMessage $cLogThrow + Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It + Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It + Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It + Assert-MockCalled Set-CMStatusReportingComponent -Exactly -Times 0 -Scope It + } + + It 'Should call expected commands and throw if client reporting settings are malformed' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnAll } + + { Set-TargetResource @cReportMalformed } | Should -Throw -ExpectedMessage $cReportThrow + Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It + Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It + Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It + Assert-MockCalled Set-CMStatusReportingComponent -Exactly -Times 0 -Scope It + } + + It 'Should call expected commands and throw if server logging settings are malformed' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnAll } + + { Set-TargetResource @sLogMalformed } | Should -Throw -ExpectedMessage $sLogThrow + Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It + Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It + Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It + Assert-MockCalled Set-CMStatusReportingComponent -Exactly -Times 0 -Scope It + } + + It 'Should call expected commands and throw if server reporting settings are malformed' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnAll } + + { Set-TargetResource @sReportMalformed } | Should -Throw -ExpectedMessage $sReportThrow + Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It + Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It + Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It + Assert-MockCalled Set-CMStatusReportingComponent -Exactly -Times 0 -Scope It + } + + It 'Should call expected commands and throw if Set-CMStatusReportingComponent throws' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnAll } + Mock -CommandName Set-CMStatusReportingComponent -MockWith { throw } + + { Set-TargetResource @inputMismatch } | Should -Throw + Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It + Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It + Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It + Assert-MockCalled Set-CMStatusReportingComponent -Exactly -Times 1 -Scope It + } + } + } + + Describe 'ConfigMgrCBDsc - DSC_CMStatusReportingComponent\Test-TargetResource' -Tag 'Test'{ + BeforeAll{ + $getReturnAll = @{ + SiteCode = 'Lab' + ClientLogChecked = $true + ClientLogFailureChecked = $true + ClientLogType = 'AllMilestones' + ClientReportChecked = $true + ClientReportFailureChecked = $true + ClientReportType = 'AllMilestones' + ServerLogChecked = $true + ServerLogFailureChecked = $true + ServerLogType = 'AllMilestones' + ServerReportChecked = $true + ServerReportFailureChecked = $true + ServerReportType = 'AllMilestones' + } + + $inputMismatch = @{ + SiteCode = 'Lab' + ClientLogChecked = $true + ClientLogFailureChecked = $false + ClientLogType = 'AllMilestones' + ClientReportChecked = $true + ClientReportFailureChecked = $false + ClientReportType = 'AllMilestones' + ServerLogChecked = $true + ServerLogFailureChecked = $true + ServerLogType = 'ErrorAndWarningMilestones' + ServerReportChecked = $true + ServerReportFailureChecked = $true + ServerReportType = 'ErrorMilestones' + } + + Mock -CommandName Import-ConfigMgrPowerShellModule + Mock -CommandName Set-Location + } + + Context 'When running Test-TargetResource' { + BeforeEach{ + Mock -CommandName Get-TargetResource -MockWith { $getReturnAll } + } + + It 'Should return desired result true when all returned values match inputs' { + + Test-TargetResource @getReturnAll | Should -Be $true + } + + It 'Should return desired result false when there is a mismatch between returned values and inputs' { + + Test-TargetResource @inputMismatch | Should -Be $false + } + } + } + } +} +finally +{ + Invoke-TestCleanup +} From 8d92bae53335d4c4038e1963da36d453f240d86b Mon Sep 17 00:00:00 2001 From: Nicholas Ellis Date: Tue, 21 Jul 2020 08:31:44 -0400 Subject: [PATCH 2/2] Addressing PR Comments --- .../DSC_CMStatusReportingComponent.psm1 | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.psm1 b/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.psm1 index 83982e9..065d93d 100644 --- a/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.psm1 +++ b/source/DSCResources/DSC_CMStatusReportingComponent/DSC_CMStatusReportingComponent.psm1 @@ -32,7 +32,7 @@ function Get-TargetResource $clientComponentStatus = Get-CMStatusReportingComponent -SiteCode $SiteCode | Where-Object -FilterScript {$_.ItemName -eq 'Client Component Status Reporting'} $serverComponentStatus = Get-CMStatusReportingComponent -SiteCode $SiteCode | Where-Object -FilterScript {$_.ItemName -eq 'Server Component Status Reporting'} - foreach ($prop in $clientComponentStatus.props) + foreach ($prop in $clientComponentStatus.Props) { switch ($prop.PropertyName) { @@ -67,7 +67,7 @@ function Get-TargetResource } } - foreach ($prop in $serverComponentStatus.props) + foreach ($prop in $serverComponentStatus.Props) { switch ($prop.PropertyName) { @@ -238,22 +238,22 @@ function Set-TargetResource try { - if ((($state.ClientLogChecked -eq $false) -and ($null -eq $ClientLogChecked) -and ($ClientLogType -or $ClientLogFailureChecked)) -or (($ClientLogChecked -eq $false) -and ($clientLogType -or $ClientLogFailureChecked))) + if (($ClientLogChecked -eq $false) -and ($clientLogType -or $ClientLogFailureChecked)) { throw $script:localizedData.CLogThrow } - if ((($state.ClientReportChecked -eq $false) -and ($null -eq $ClientReportChecked) -and ($ClientReportType -or $ClientReportFailureChecked)) -or (($ClientReportChecked -eq $false) -and ($ClientReportType -or $ClientReportFailureChecked))) + if (($ClientReportChecked -eq $false) -and ($ClientReportType -or $ClientReportFailureChecked)) { throw $script:localizedData.CReportThrow } - if ((($state.ServerLogChecked -eq $false) -and ($null -eq $ServerLogChecked) -and ($ServerLogType -or $ServerLogFailureChecked)) -or (($ServerLogChecked -eq $false) -and ($ServerLogType -or $ServerLogFailureChecked))) + if (($ServerLogChecked -eq $false) -and ($ServerLogType -or $ServerLogFailureChecked)) { throw $script:localizedData.SLogThrow } - if ((($state.ServerReportChecked -eq $false) -and ($null -eq $ServerReportChecked) -and ($ServerReportType -or $ServerReportFailureChecked)) -or (($ServerReportChecked -eq $false) -and ($ServerReportType -or $ServerReportFailureChecked))) + if (($ServerReportChecked -eq $false) -and ($ServerReportType -or $ServerReportFailureChecked)) { throw $script:localizedData.SReportThrow } @@ -263,9 +263,9 @@ function Set-TargetResource foreach ($param in $PSBoundParameters.GetEnumerator()) { - if ($evalList -contains $param.key) + if ($evalList -contains $param.Key) { - if ($param.Value -ne $state[$param.key]) + if ($param.Value -ne $state[$param.Key]) { Write-Verbose -Message ($script:localizedData.SettingValue -f $param.Key, $param.Value) $buildingParams += @{