diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fdba65..e85c160 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added CMAdministrativeUser Resource - Added Compare-MultipleCompares to the ResourceHelper - Added CMDistributionGroup Resource +- Added CMSiteSystemServer Resource ### Changed diff --git a/README.md b/README.md index 62835cc..045f116 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,8 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu administrative users. - **CMDistributionGroup**: Provides a resource for creating Distribution Point Groups and adding Distribution Points to the group. +- **CMSiteSystemServer**: Provides a resource for adding and modifying a Site + System Server and its properties. ### xSccmPreReqs @@ -914,3 +916,34 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu - [CMDistributionGroup_Present](Source\Examples\Resources\CMDistributionGroup\CMDistributionGroup_Present.ps1) - [CMDistributionGroup_Absent](Source\Examples\Resources\CMDistributionGroup\CMDistributionGroup_Absent.ps1) + +### CMSiteSystemServer + +- **[String] SiteCode** _(Key)_: Specifies the Site Code for the Configuration + Manager site. +- **[String] SiteSystemServer** _(Key)_: Specifies the name of the site system server. +- **[String] PublicFqdn** _(Write)_: Specifies the public FQDN of the site server. + Setting PublicFqdn = '' will disable the PublicFqdn setting. +- **[Boolean] FdmOperation** _(Write)_: Indicates whether the site system server + is required to initiate connections to this site system. +- **[Boolean] UseSiteServerAccount** _(Write)_: Indicates that the install uses + the site server's computer account to install the site system. +- **[String] AccountName** _(Write)_: Specifies the account name for installing + the site system. +- **[Boolean] EnableProxy** _(Write)_: Indicates whether to enable a proxy server + to use when the server synchronizes information from the Internet. +- **[String] ProxyServerName** _(Write)_: Specifies the name of a proxy server. + Use a fully qualified domain name FQDN, short name, or IPv4/IPv6 address. +- **[UInt32] ProxyServerPort** _(Write)_: Specifies the proxy server port number + to use when connecting to the Internet. +- **[String] ProxyAccessAccount** _(Write)_: Specifies the credentials to use + to authenticate with the proxy server. + Setting ProxyAccessAccount = '' will reset the proxy to use system account. +- **[String] Ensure** _(Write)_: Specifies whether the system site + server is present or absent. + - Values include: { Present | Absent } + +#### CMSiteSystemServer Examples + +- [CMSiteSystemServer_Present](Source\Examples\Resources\CMSiteSystemServer\CMSiteSystemServer_Present.ps1) +- [CMSiteSystemServer_Absent](Source\Examples\Resources\CMSiteSystemServer\CMSiteSystemServer_Absent.ps1) diff --git a/source/ConfigMgrCBDsc.psd1 b/source/ConfigMgrCBDsc.psd1 index c81eb81..161e54b 100644 --- a/source/ConfigMgrCBDsc.psd1 +++ b/source/ConfigMgrCBDsc.psd1 @@ -68,6 +68,7 @@ 'CMSiteMaintenance' 'CMAdministrativeUser' 'CMDistributionGroup' + 'CMSiteSystemServer' ) <# @@ -84,7 +85,7 @@ 'SccmSqlSetup','SCCMInstall','CMIniFile','Collections','Boundaries','ForestDiscovery','ClientStatusSettings','BoundaryGroups', 'ManagementPoint','AssetIntelligencePoint','FallbackStatusPoint','SoftwareUpdatePoint','DistrubtionPoint','HeartbeatDiscovery', 'ServiceConnectionPoint','NetworkDiscovery','ReportingServicePoint','SystemDiscovery','PXEDistributionPoint','PullDistributionPoint', - 'SiteMaintenance','AdministrativeUser','DistributionGroup') + 'SiteMaintenance','AdministrativeUser','DistributionGroup','SiteSystemServer') # A URL to the license for this module. LicenseUri = 'https://github.com/dsccommunity/ConfigMgrCBDsc/blob/master/LICENSE' diff --git a/source/DSCResources/DSC_CMSiteSystemServer/DSC_CMSiteSystemServer.psm1 b/source/DSCResources/DSC_CMSiteSystemServer/DSC_CMSiteSystemServer.psm1 new file mode 100644 index 0000000..d7e8200 --- /dev/null +++ b/source/DSCResources/DSC_CMSiteSystemServer/DSC_CMSiteSystemServer.psm1 @@ -0,0 +1,511 @@ +$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. + + .PARAMETER SiteSystemServer + Specifies the name of the site system server. +#> +function Get-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param + ( + [Parameter(Mandatory = $true)] + [String] + $SiteCode, + + [Parameter(Mandatory = $true)] + [String] + $SiteSystemServer + ) + + Write-Verbose -Message $script:localizedData.RetrieveSettingValue + Import-ConfigMgrPowerShellModule -SiteCode $SiteCode + Set-Location -Path "$($SiteCode):\" + + $servercheck = (Get-CMSiteSystemServer -SiteSystemServerName $SiteSystemServer -SiteCode $SiteCode) + + if ($servercheck) + { + foreach ($check in $servercheck.Props) + { + switch ($check.PropertyName) + { + 'Server Remote Public Name' { $publicName = $check.Value1 } + 'FDMOperation' { [boolean]$fdm = $check.Value } + 'UseMachineAccount' { [boolean]$useMachine = $check.Value } + 'UserName' { $username = $check.Value2 } + 'UseProxy' { [boolean]$proxy = $check.Value } + 'ProxyName' { $proxyServer = $check.Value2 } + 'ProxyServerPort' { [UInt32]$proxyPort = $check.Value } + 'ProxyUserName' { $proxyUser = $check.Value2 } + 'AnonymousProxyAccess' { $anon = $check.Value } + } + } + + if ($anon -eq 1) + { + $proxyUser = $null + } + + $status = 'Present' + } + else + { + $status = 'Absent' + } + + return @{ + SiteCode = $SiteCode + SiteSystemServer = $SiteSystemServer + PublicFqdn = $publicName + FdmOperation = $fdm + UseSiteServerAccount = $useMachine + AccountName = $username + EnableProxy = $proxy + ProxyServerName = $proxyServer + ProxyServerPort = $proxyPort + ProxyAccessAccount = $proxyUser + Ensure = $status + RoleCount = $servercheck.RoleCount + } +} + +<# + .SYNOPSIS + This will set the desired state. + + .PARAMETER SiteCode + Specifies the site code for Configuration Manager site. + + .PARAMETER SiteSystemServer + Specifies the name of the site system server. + + .PARAMETER PublicFqdn + Specifies the public FQDN of the site server. Setting PublicFqdn = '' will disable + the PublicFqdn setting. + + .PARAMETER FdmOperation + Indicates whether the site system server is required to initiate connections to this site system. + + .PARAMETER UseSiteServerAccount + Indicates that the cmdlet uses the site server's computer account to install the site system. + + .PARAMETER AccountName + Specifies the account name for installing the site system. The account must already exist in Configuration Manager. + + .PARAMETER EnableProxy + Indicates whether to enable a proxy server to use when the server synchronizes information from the Internet. + + .PARAMETER ProxyServerName + Specifies the name of a proxy server. Use a fully qualified domain name FQDN, short name, or IPv4/IPv6 address. + When specifying EnableProxy, ProxyServerName must be specified. + + .PARAMETER ProxyServerPort + Specifies the proxy server port number to use when connecting to the Internet. + + .PARAMETER ProxyAccessAccount + Specifies the credentials to use to authenticate with the proxy server. + Do not use user principal name (UPN) format. Setting $ProxyAccessAccount = '' will remove the account. + + .PARAMETER Ensure + Specifies whether the site system server is to be present or absent. When removing the role, all other site system roles + must first be removed. +#> +function Set-TargetResource +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $SiteCode, + + [Parameter(Mandatory = $true)] + [String] + $SiteSystemServer, + + [Parameter()] + [String] + $PublicFqdn, + + [Parameter()] + [Boolean] + $FdmOperation, + + [Parameter()] + [Boolean] + $UseSiteServerAccount, + + [Parameter()] + [String] + $AccountName, + + [Parameter()] + [Boolean] + $EnableProxy, + + [Parameter()] + [String] + $ProxyServerName, + + [Parameter()] + [ValidateRange(1, 65535)] + [UInt32] + $ProxyServerPort, + + [Parameter()] + [String] + $ProxyAccessAccount, + + [Parameter()] + [ValidateSet('Present','Absent')] + [String] + $Ensure = 'Present' + ) + + Import-ConfigMgrPowerShellModule -SiteCode $SiteCode + Set-Location -Path "$($SiteCode):\" + $state = Get-TargetResource -SiteCode $SiteCode -SiteSystemServer $SiteSystemServer + + try + { + if ($Ensure -eq 'Present') + { + if (($PSBoundParameters.ContainsKey('UseSiteServerAccount') -and $PSBoundParameters.UseSiteServerAccount -eq $true) -and + $PSBoundParameters.ContainsKey('AccountName')) + { + throw $script:localizedData.SiteSvrAccountandAccount + } + + if ($EnableProxy -eq $true -and -not $PSBoundParameters.ContainsKey('ProxyServerName')) + { + throw $script:localizedData.EnableProxyNoServer + } + + if (($PSBoundParameters.ContainsKey('ProxyServerPort') -or $PSBoundParameters.ContainsKey('ProxyAccessAccount') -or + $PSBoundParameters.ContainsKey('ProxyServerName')) -and ($EnableProxy -ne $true)) + { + throw $script:localizedData.ProxySettingNoEnable + } + + if ($state.Ensure -eq 'Absent') + { + $newServer = $true + } + + $valuesToCheck = @('PublicFqdn','FdmOperation','UseSiteServerAccount','AccountName') + $proxyCheck = @('EnableProxy','ProxyServerName','ProxyServerPort','ProxyAccessAccount') + + foreach ($param in $PSBoundParameters.GetEnumerator()) + { + if ($valuesToCheck -contains $param.Key) + { + if ($param.Value -ne $state[$param.Key]) + { + Write-Verbose -Message ($script:localizedData.SetSetting -f $param.Key, $param.Value) + + $buildingParams += @{ + $param.Key = $param.Value + } + } + } + elseif ($proxyCheck -contains $param.Key) + { + if ($param.Value -ne $state[$param.Key]) + { + Write-Verbose -Message ($script:localizedData.SetSetting -f $param.Key, $param.Value) + $proxyBad = $true + } + } + } + + if (-not [string]::IsNullOrEmpty($buildingParams) -and $buildingParams.ContainsKey('AccountName')) + { + if ($null -eq (Get-CMAccount -UserName $AccountName)) + { + throw ($script:localizedData.BadAccountName -f $AccountName) + } + } + + if ($proxyBad) + { + if ($EnableProxy -eq $true) + { + $buildingParams += @{ + EnableProxy = $true + ProxyServerName = $ProxyServerName + } + + if ($PSBoundParameters.ContainsKey('ProxyServerPort')) + { + $buildingParams += @{ + ProxyServerPort = $ProxyServerPort + } + } + + if ($PSBoundParameters.ContainsKey('ProxyAccessAccount')) + { + if (-not [string]::IsNullOrEmpty($ProxyAccessAccount)) + { + $account = Get-CMAccount -UserName $ProxyAccessAccount + + if ([string]::IsNullOrEmpty($account)) + { + throw ($script:localizedData.BadProxyAccess -f $ProxyAccessAccount) + } + + $buildingParams += @{ + ProxyAccessAccount = $account + } + } + } + + if ((-not $PSBoundParameters.ContainsKey('ProxyServerPort')) -and + (-not [string]::IsNullOrEmpty($state.ProxyServerPort) -and $state.ProxyServerPort -ne 80)) + { + Write-Warning -Message ($script:localizedData.NoProxyPort -f $state.ProxyServerPort) + } + + if (-not $PSBoundParameters.ContainsKey('ProxyAccessAccount') -and + -not [string]::IsNullOrEmpty($state.ProxyAccessAccount)) + { + Write-Warning -Message ($script:localizedData.NoProxyAccessAccount -f $state.ProxyAccessAccount) + } + } + else + { + $buildingParams += @{ + EnableProxy = $false + } + } + } + + if ($newServer) + { + New-CMSiteSystemServer -SiteCode $SiteCode -SiteSystemServerName $SiteSystemServer + } + + if ($buildingParams) + { + Set-CMSiteSystemServer -SiteCode $SiteCode -SiteSystemServerName $SiteSystemServer @buildingParams + } + } + elseif ($state.Ensure -eq 'Present') + { + if ($state.RoleCount -ge 2) + { + throw ($script:localizedData.CurrentRoleCount -f $state.RoleCount) + } + + Remove-CMSiteSystemServer -SiteCode $SiteCode -SiteSystemServerName $SiteSystemServer + } + } + 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 SiteSystemServer + Specifies the name of the site system server. + + .PARAMETER PublicFqdn + Specifies the public FQDN of the site server. Setting PublicFqdn = '' will disable + the PublicFqdn setting. + + .PARAMETER FdmOperation + Indicates whether the site system server is required to initiate connections to this site system. + + .PARAMETER UseSiteServerAccount + Indicates that the cmdlet uses the site server's computer account to install the site system. + + .PARAMETER AccountName + Specifies the account name for installing the site system. The account must already exist in Configuration Manager. + + .PARAMETER EnableProxy + Indicates whether to enable a proxy server to use when the server synchronizes information from the Internet. + + .PARAMETER ProxyServerName + Specifies the name of a proxy server. Use a fully qualified domain name FQDN, short name, or IPv4/IPv6 address. + When specifying EnableProxy, ProxyServerName must be specified. + + .PARAMETER ProxyServerPort + Specifies the proxy server port number to use when connecting to the Internet. + + .PARAMETER ProxyAccessAccount + Specifies the credentials to use to authenticate with the proxy server. + Do not use user principal name (UPN) format. Setting $ProxyAccessAccount = '' will remove the account. + + .PARAMETER Ensure + Specifies whether the site system server is to be present or absent. When removing the role, all other site system roles + must first be removed. +#> +function Test-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Boolean])] + param + ( + [Parameter(Mandatory = $true)] + [String] + $SiteCode, + + [Parameter(Mandatory = $true)] + [String] + $SiteSystemServer, + + [Parameter()] + [String] + $PublicFqdn, + + [Parameter()] + [Boolean] + $FdmOperation, + + [Parameter()] + [Boolean] + $UseSiteServerAccount, + + [Parameter()] + [String] + $AccountName, + + [Parameter()] + [Boolean] + $EnableProxy, + + [Parameter()] + [String] + $ProxyServerName, + + [Parameter()] + [ValidateRange(1, 65535)] + [UInt32] + $ProxyServerPort, + + [Parameter()] + [String] + $ProxyAccessAccount, + + [Parameter()] + [ValidateSet('Present','Absent')] + [String] + $Ensure = 'Present' + ) + + Import-ConfigMgrPowerShellModule -SiteCode $SiteCode + Set-Location -Path "$($SiteCode):\" + $state = Get-TargetResource -SiteCode $SiteCode -SiteSystemServer $SiteSystemServer + $result = $true + + if ($Ensure -eq 'Present') + { + if ($state.Ensure -eq 'Absent') + { + Write-Verbose -Message ($script:localizedData.NonSiteServer -f $SiteSystemServer) + $result = $false + } + else + { + if (($PSBoundParameters.ContainsKey('UseSiteServerAccount') -and $PSBoundParameters.UseSiteServerAccount -eq $true) -and + $PSBoundParameters.ContainsKey('AccountName')) + { + Write-Warning -Message $script:localizedData.SiteSvrAccountandAccount + } + + if ($EnableProxy -eq $true -and -not $PSBoundParameters.ContainsKey('ProxyServerName')) + { + Write-Warning -Message $script:localizedData.EnableProxyNoServer + } + + if (($PSBoundParameters.ContainsKey('ProxyServerPort') -or $PSBoundParameters.ContainsKey('ProxyAccessAccount') -or + $PSBoundParameters.ContainsKey('ProxyServerName')) -and ($EnableProxy -ne $true)) + { + Write-Warning -Message $script:localizedData.ProxySettingNoEnable + } + + $testParams = @{ + CurrentValues = $state + DesiredValues = $PSBoundParameters + ValuesToCheck = @('PublicFqdn','FdmOperation','UseSiteServerAccount','AccountName') + } + + $result = Test-DscParameterState @testParams -Verbose + + $proxyCheck = @('EnableProxy','ProxyServerName','ProxyServerPort','ProxyAccessAccount') + + foreach ($param in $PSBoundParameters.GetEnumerator()) + { + if ($proxyCheck -contains $param.Key) + { + if ($param.Key -eq 'ProxyAccessAccount' -and $param.Value -eq '') + { + if (-not [string]::IsNullOrEmpty($state.ProxyAccessAccount)) + { + Write-Verbose -Message ($script:localizedData.ProxyCheck -f $param.Key, $param.Value, $state[$param.Key]) + $proxyBad = $true + } + } + elseif ($param.Value -ne $state[$param.Key]) + { + Write-Verbose -Message ($script:localizedData.ProxyCheck -f $param.Key, $param.Value, $state[$param.Key]) + $proxybad = $true + } + } + } + + if ($proxyBad) + { + if ((-not $PSBoundParameters.ContainsKey('ProxyServerPort')) -and + (-not [string]::IsNullOrEmpty($state.ProxyServerPort) -and $state.ProxyServerPort -ne 80)) + { + Write-Warning -Message ($script:localizedData.NoProxyPort -f $state.ProxyServerPort) + } + + if (-not $PSBoundParameters.ContainsKey('ProxyAccessAccount') -and -not [string]::IsNullOrEmpty($state.ProxyAccessAccount)) + { + Write-Warning -Message ($script:localizedData.NoProxyAccessAccount -f $state.ProxyAccessAccount) + } + + $result = $false + } + } + } + elseif ($state.Ensure -eq 'Present') + { + if ($state.RoleCount -ge 2) + { + Write-Warning -Message ($script:localizedData.CurrentRoleCount -f $state.RoleCount) + } + + $result = $false + } + + Write-Verbose -Message ($script:localizedData.TestState -f $result) + Set-Location -Path "$env:temp" + return $result +} diff --git a/source/DSCResources/DSC_CMSiteSystemServer/DSC_CMSiteSystemServer.schema.mof b/source/DSCResources/DSC_CMSiteSystemServer/DSC_CMSiteSystemServer.schema.mof new file mode 100644 index 0000000..a1905f9 --- /dev/null +++ b/source/DSCResources/DSC_CMSiteSystemServer/DSC_CMSiteSystemServer.schema.mof @@ -0,0 +1,16 @@ +[ClassVersion("1.0.0"), FriendlyName("CMSiteSystemServer")] +class DSC_CMSiteSystemServer : OMI_BaseResource +{ + [Key, Description("Specifies the SiteCode for the Configuration Manager site.")] String SiteCode; + [Key, Description("Specifies the name of the site system server.")] String SiteSystemServer; + [Write, Description("Specifies the public FQDN of the site server.")] String PublicFqdn; + [Write, Description("Indicates whether the site system server is required to initiate connections to this site system.")] Boolean FdmOperation; + [Write, Description("Indicates that the install uses the site server's computer account to install the site system.")] Boolean UseSiteServerAccount; + [Write, Description("Specifies the account name for installing the site system.")] String AccountName; + [Write, Description("Indicates whether to enable a proxy server to use when the server synchronizes information from the Internet.")] Boolean EnableProxy; + [Write, Description("Specifies the name of a proxy server. Use a fully qualified domain name FQDN, short name, or IPv4/IPv6 address.")] String ProxyServerName; + [Write, Description("Specifies the proxy server port number to use when connecting to the Internet.")] UInt32 ProxyServerPort; + [Write, Description("Specifies the credentials to use to authenticate with the proxy server. Do not use user principal name UPN format.")] String ProxyAccessAccount; + [Write, Description("Specifies whether the site system server is to be present or absent."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; + [Read, Description("Specifies the count of roles on the site system server.")] String RoleCount; +}; diff --git a/source/DSCResources/DSC_CMSiteSystemServer/en-US/DSC_CMSiteSystemServer.strings.psd1 b/source/DSCResources/DSC_CMSiteSystemServer/en-US/DSC_CMSiteSystemServer.strings.psd1 new file mode 100644 index 0000000..621847a --- /dev/null +++ b/source/DSCResources/DSC_CMSiteSystemServer/en-US/DSC_CMSiteSystemServer.strings.psd1 @@ -0,0 +1,15 @@ +ConvertFrom-StringData @' + RetrieveSettingValue = Getting results for Configuration Manager accounts. + NonSiteServer = {0} is currently not a Site Server. + SiteSvrAccountandAccount = You have specified to use SiteSystemAccount and an Account for site server communications, you can only specify 1 or the other. + EnableProxyNoServer = When EnableProxy equals $True you must at least specify ProxyServerName. + ProxySettingNoEnable = When specifying a proxy setting you must specify EnableProxy = $True. + ProxyCheck = NOTMATCH: {0} Expected {1} returned {2}. + NoProxyPort = No ProxyServerPort specified the port will be set to default value of 80 overwritting current value of {0}. + NoProxyAccessAccount = No ProxyAccess account specified and is currently set to {0} will reset the proxy access account to use the system account. + CurrentRoleCount = Must uninstall all other roles prior to removing the site server component current rolecount: {0}. + SetSetting = Setting {0} to expected result {1}. + BadAccountName = AccountName {0} does not exist in Configuraion Manager. + BadProxyAccess = ProxyAccessAccount {0} does not exist in Configuraion Manager. + TestState = Test-TargetResource compliance check returned: {0}. +'@ diff --git a/source/Examples/Resources/CMSiteSystemServer/CMSiteSystemServer_Absent.ps1 b/source/Examples/Resources/CMSiteSystemServer/CMSiteSystemServer_Absent.ps1 new file mode 100644 index 0000000..a3aa391 --- /dev/null +++ b/source/Examples/Resources/CMSiteSystemServer/CMSiteSystemServer_Absent.ps1 @@ -0,0 +1,18 @@ +<# + .SYNOPSIS + A DSC configuration script to remove a site system server from Configuration Manager. +#> +Configuration Example +{ + Import-DscResource -ModuleName ConfigMgrCBDsc + + Node localhost + { + CMSiteSystemServer ExampleServer + { + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + Ensure = 'Absent' + } + } +} diff --git a/source/Examples/Resources/CMSiteSystemServer/CMSiteSystemServer_Present.ps1 b/source/Examples/Resources/CMSiteSystemServer/CMSiteSystemServer_Present.ps1 new file mode 100644 index 0000000..653d2c7 --- /dev/null +++ b/source/Examples/Resources/CMSiteSystemServer/CMSiteSystemServer_Present.ps1 @@ -0,0 +1,53 @@ +<# + .SYNOPSIS + A DSC configuration script to configure site system server for Configuration Manager. +#> +Configuration Example +{ + Import-DscResource -ModuleName ConfigMgrCBDsc + + Node localhost + { + CMSiteSystemServer SS01Server + { + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + UseSiteServerAccount = $false + PublicFqdn = 'SS01.contoso.com' + EnableProxy = $false + ProxyServerName = 'CA01.contoso.com' + ProxyAccessAccount = 'contoso\Proxy' + FdmOperation = $true + AccountName = 'contoso\Account' + Ensure = 'Present' + } + + CMSiteSystemServer SS02Server + { + SiteCode = 'Lab' + SiteSystemServer = 'SS02.contoso.com' + UseSiteServerAccount = $true + PublicFqdn = '' + EnableProxy = $false + ProxyServerName = 'CA01.contoso.com' + ProxyAccessAccount = 'contoso\Proxy' + ProxyServerPort = 443 + FdmOperation = $true + Ensure = 'Present' + } + + CMSiteSystemServer SS03Server + { + SiteCode = 'Lab' + SiteSystemServer = 'SS03.contoso.com' + UseSiteServerAccount = $true + PublicFqdn = '' + EnableProxy = $false + ProxyServerName = 'CA01.contoso.com' + ProxyAccessAccount = '' + ProxyServerPort = 443 + FdmOperation = $true + Ensure = 'Present' + } + } +} diff --git a/tests/Unit/CMSiteSystemServer.tests.ps1 b/tests/Unit/CMSiteSystemServer.tests.ps1 new file mode 100644 index 0000000..10162d5 --- /dev/null +++ b/tests/Unit/CMSiteSystemServer.tests.ps1 @@ -0,0 +1,661 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] +param () + +$script:dscModuleName = 'ConfigMgrCBDsc' +$script:dscResourceName = 'DSC_CMSiteSystemServer' + +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 'DSC_CMSiteSystemServer\Get-TargetResource' -Tag 'Get' { + BeforeAll { + $siteServerReturn = @{ + SiteCode = 'Lab' + RoleName = 'SMS Site Server' + NetworkOSPath = '\\SS01.contoso.com' + RoleCount = 1 + Props = @( + @{ + PropertyName = 'AnonymousProxyAccess' + Value = 0 + } + @{ + PropertyName = 'FDMOperation' + Value = 1 + } + @{ + PropertyName = 'ProxyName' + Value2 = 'Proxy.contoso.com' + } + @{ + PropertyName = 'ProxyServerPort' + Value = 443 + } + @{ + PropertyName = 'ProxyUserName' + Value2 = 'contoso\ProxyUser' + } + @{ + PropertyName = 'Server Remote Public Name' + Value1 = 'SS01.contoso.com' + } + @{ + PropertyName = 'UseMachineAccount' + Value = 0 + } + @{ + PropertyName = 'UseProxy' + Value = 1 + } + @{ + PropertyName = 'UserName' + Value2 = 'contoso\Account' + } + ) + } + + $siteServerReturnAnon = @{ + SiteCode = 'Lab' + RoleName = 'SMS Site Server' + NetworkOSPath = '\\SS01.contoso.com' + RoleCount = 1 + Props = @( + @{ + PropertyName = 'AnonymousProxyAccess' + Value = 1 + } + @{ + PropertyName = 'FDMOperation' + Value = 1 + } + @{ + PropertyName = 'ProxyName' + Value2 = 'Proxy.contoso.com' + } + @{ + PropertyName = 'ProxyServerPort' + Value = 443 + } + @{ + PropertyName = 'ProxyUserName' + Value2 = 'contoso\ProxyUser' + } + @{ + PropertyName = 'Server Remote Public Name' + Value1 = 'SS01.contoso.com' + } + @{ + PropertyName = 'UseMachineAccount' + Value = 0 + } + @{ + PropertyName = 'UseProxy' + Value = 1 + } + @{ + PropertyName = 'UserName' + Value2 = 'contoso\Account' + } + ) + } + + $getInput = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + } + + + Mock -CommandName Import-ConfigMgrPowerShellModule + Mock -CommandName Set-Location + } + + Context 'When retrieving site system settings' { + + It 'Should return desired result when a site system' { + Mock -CommandName Get-CMSiteSystemServer -MockWith { $siteServerReturn } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.SiteSystemServer | Should -Be -ExpectedValue 'SS01.contoso.com' + $result.PublicFqdn | Should -Be -ExpectedValue 'SS01.contoso.com' + $result.FdmOperation | Should -Be -ExpectedValue $true + $result.UseSiteServerAccount | Should -Be -ExpectedValue $false + $result.AccountName | Should -Be -ExpectedValue 'contoso\Account' + $result.EnableProxy | Should -Be -ExpectedValue $true + $result.ProxyServerName | Should -Be -ExpectedValue 'Proxy.contoso.com' + $result.ProxyServerPort | Should -Be -ExpectedValue 443 + $result.ProxyAccessAccount | Should -Be -ExpectedValue 'contoso\ProxyUser' + $result.Ensure | Should -Be -ExpectedValue 'Present' + $result.RoleCount | Should -Be -ExpectedValue 1 + } + + It 'Should return desired result when a site system and no proxy account' { + Mock -CommandName Get-CMSiteSystemServer -MockWith { $siteServerReturnAnon } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.SiteSystemServer | Should -Be -ExpectedValue 'SS01.contoso.com' + $result.PublicFqdn | Should -Be -ExpectedValue 'SS01.contoso.com' + $result.FdmOperation | Should -Be -ExpectedValue $true + $result.UseSiteServerAccount | Should -Be -ExpectedValue $false + $result.AccountName | Should -Be -ExpectedValue 'contoso\Account' + $result.EnableProxy | Should -Be -ExpectedValue $true + $result.ProxyServerName | Should -Be -ExpectedValue 'Proxy.contoso.com' + $result.ProxyServerPort | Should -Be -ExpectedValue 443 + $result.ProxyAccessAccount | Should -Be -ExpectedValue $null + $result.Ensure | Should -Be -ExpectedValue 'Present' + $result.RoleCount | Should -Be -ExpectedValue 1 + } + + It 'Should return desired result when not a site system' { + Mock -CommandName Get-CMSiteSystemServer -MockWith { $null } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.SiteSystemServer | Should -Be -ExpectedValue 'SS01.contoso.com' + $result.PublicFqdn | Should -Be -ExpectedValue $null + $result.FdmOperation | Should -Be -ExpectedValue $null + $result.UseSiteServerAccount | Should -Be -ExpectedValue $null + $result.AccountName | Should -Be -ExpectedValue $null + $result.EnableProxy | Should -Be -ExpectedValue $null + $result.ProxyServerName | Should -Be -ExpectedValue $null + $result.ProxyServerPort | Should -Be -ExpectedValue $null + $result.ProxyAccessAccount | Should -Be -ExpectedValue $null + $result.Ensure | Should -Be -ExpectedValue 'Absent' + $result.RoleCount | Should -Be -ExpectedValue $null + } + } + } + + Describe 'DSC_CMSiteSystemServer\Set-TargetResource' -Tag 'Set' { + BeforeAll { + $getReturnPresent = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + PublicFqdn = 'SS01.contoso.com' + FdmOperation = $true + UseSiteServerAccount = $false + AccountName = 'contoso\account' + EnableProxy = $true + ProxyServerName = 'Proxy.contoso.com' + ProxyServerPort = 443 + ProxyAccessAccount = 'contoso\ProxyUser' + Ensure = 'Present' + RoleCount = 1 + } + + $getReturnAbsent = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + PublicFqdn = 'SS01.contoso.com' + FdmOperation = $null + UseSiteServerAccount = $null + AccountName = $null + EnableProxy = $null + ProxyServerName = $null + ProxyServerPort = $null + ProxyAccessAccount = $null + Ensure = 'Absent' + RoleCount = $null + } + + $inputAbsent = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + Ensure = 'Absent' + } + + Mock -CommandName Import-ConfigMgrPowerShellModule + Mock -CommandName Set-Location + Mock -CommandName New-CMSiteSystemServer + Mock -CommandName Set-CMSiteSystemServer + Mock -CommandName Remove-CMSiteSystemServer + } + + Context 'When Set-TargetResource runs successfully' { + BeforeEach { + $misMatchInput = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + PublicFqdn = '' + FdmOperation = $false + UseSiteServerAccount = $false + AccountName = 'contoso\useraccount' + EnableProxy = $true + ProxyServerName = 'Proxy1.contoso.com' + Ensure = 'Present' + } + + $proxyAccessAccount = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + AccountName = 'contoso\useraccount' + EnableProxy = $true + ProxyServerName = 'Proxy.contoso.com' + ProxyAccessAccount = 'contoso\ProxyUserBad' + Ensure = 'Present' + } + + $disableProxy = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + EnableProxy = $false + } + + Mock -CommandName Get-CMAccount -MockWith { $true } + } + + It 'Should call expected command when changing settings' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + + Set-TargetResource @misMatchInput + 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 Get-CMAccount -Exactly -Times 1 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 1 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + + It 'Should call expected command when adding a site system server' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnAbsent } + + Set-TargetResource @misMatchInput + 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 Get-CMAccount -Exactly -Times 1 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 1 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 1 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + + It 'Should call expected command when changing the proxy access account' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + + Set-TargetResource @proxyAccessAccount + 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 Get-CMAccount -Exactly -Times 2 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 1 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + + It 'Should call expected command when removing a site system server' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + + Set-TargetResource @inputAbsent + 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 Get-CMAccount -Exactly -Times 0 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 1 -Scope It + } + + It 'Should call expected command when disabling proxy' { + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + + Set-TargetResource @disableProxy + 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 Get-CMAccount -Exactly -Times 0 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 1 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + } + + Context 'When Set-TargetResource throws' { + BeforeEach { + $proxySettingsNoServerName = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + ProxyAccessAccount = 'contoso\ProxyUser' + EnableProxy = $true + ProxyServerPort = 80 + } + + $proxySettingBadProxyAccount = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + ProxyServerName = 'CA01.contoso.com' + ProxyAccessAccount = 'contoso\ProxyUserBad' + EnableProxy = $true + ProxyServerPort = 80 + } + + $proxySettingNoEnableProxy = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + ProxyServerPort = 80 + } + + $userAccountBad = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + AccountName = 'contoso\badaccount' + } + + $accountAndUseSiteSystem = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + AccountName = 'contoso\account' + UseSiteServerAccount = $true + } + + $getReturnPresentMultipleRoles = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + PublicFqdn = 'SS01.contoso.com' + FdmOperation = $true + UseSiteServerAccount = $false + AccountName = 'contoso\account' + EnableProxy = $true + ProxyServerName = 'Proxy.contoso.com' + ProxyServerPort = 443 + ProxyAccessAccount = 'contoso\ProxyUser' + Ensure = 'Present' + RoleCount = 3 + } + + $proxyNoServer = 'When EnableProxy equals $True you must at least specify ProxyServerName.' + $proxySettingNoEnable = 'When specifying a proxy setting you must specify EnableProxy = $True.' + $badAccountName = 'AccountName contoso\badaccount does not exist in Configuraion Manager.' + $badProxyAccess = 'ProxyAccessAccount contoso\ProxyUserBad does not exist in Configuraion Manager.' + $siteAndAccount = 'You have specified to use SiteSystemAccount and an Account for site server communications, you can only specify 1 or the other.' + $rolecount = 'Must uninstall all other roles prior to removing the site server component current rolecount: 3.' + } + + It 'Should call expected command when not specifying ProxyServerName' { + Mock -CommandName Get-CMAccount + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + + { Set-TargetResource @proxySettingsNoServerName } | Should -Throw -ExpectedMessage $proxyNoServer + 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 Get-CMAccount -Exactly -Times 0 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + + It 'Should call expected command when not specifying a bad proxy access account' { + Mock -CommandName Get-CMAccount + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + + { Set-TargetResource @proxySettingBadProxyAccount } | Should -Throw -ExpectedMessage $badProxyAccess + 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 Get-CMAccount -Exactly -Times 1 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + + It 'Should call expected command when AccountName account does not exist' { + Mock -CommandName Get-CMAccount + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + + { Set-TargetResource @userAccountBad } | Should -Throw -ExpectedMessage $badAccountName + 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 Get-CMAccount -Exactly -Times 1 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + + It 'Should call expected command when specifying proxy settings and not specifying EnableProxy' { + Mock -CommandName Get-CMAccount + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + + { Set-TargetResource @proxySettingNoEnableProxy } | Should -Throw -ExpectedMessage $proxySettingNoEnable + 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 Get-CMAccount -Exactly -Times 0 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + + It 'Should call expected command when specifying UserAccount and UseSiteSystemAccount' { + Mock -CommandName Get-CMAccount + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + + { Set-TargetResource @accountAndUseSiteSystem } | Should -Throw -ExpectedMessage $siteAndAccount + 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 Get-CMAccount -Exactly -Times 0 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + + It 'Should call expected command removing the site system server role and has multiple roles installed' { + Mock -CommandName Get-CMAccount + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresentMultipleRoles } + + { Set-TargetResource @inputAbsent } | Should -Throw -ExpectedMessage $rolecount + 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 Get-CMAccount -Exactly -Times 0 -Scope It + Assert-MockCalled New-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Set-CMSiteSystemServer -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMSiteSystemServer -Exactly -Times 0 -Scope It + } + } + } + + Describe 'DSC_CMSiteSystemServer\Test-TargetResource' -Tag 'Test' { + BeforeAll { + $testDSCValues = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + PublicFqdn = 'SSO01.contoso.com' + FdmOperation = $false + UseSiteServerAccount = $false + AccountName = 'contoso\Accountbad' + } + + $absentInput = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + Ensure = 'Absent' + } + + Mock -CommandName Import-ConfigMgrPowerShellModule + Mock -CommandName Set-Location + } + + Context 'When Get-TargetResource turns present' { + BeforeEach { + $getReturnPresent = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + PublicFqdn = 'SS01.contoso.com' + FdmOperation = $true + UseSiteServerAccount = $false + AccountName = 'contoso\account' + EnableProxy = $true + ProxyServerName = 'Proxy.contoso.com' + ProxyServerPort = 443 + ProxyAccessAccount = 'contoso\ProxyUser' + Ensure = 'Present' + RoleCount = 3 + } + + $siteServerAndAccount = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + UseSiteServerAccount = $true + AccountName = 'contoso\Account' + } + + $proxySettingsMatch = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + ProxyAccessAccount = 'contoso\ProxyUser' + EnableProxy = $true + ProxyServerPort = 443 + ProxyServerName = 'Proxy.contoso.com' + } + + $proxySettingsNoServerName = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + ProxyAccessAccount = 'contoso\ProxyUser' + EnableProxy = $true + ProxyServerPort = 80 + } + + $proxyAccessAccount = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + ProxyAccessAccount = '' + EnableProxy = $true + ProxyServerPort = 80 + } + + $proxySettingsNoPort = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + ProxyAccessAccount = 'contoso\ProxyUser1' + EnableProxy = $true + ProxyServerName = 'Proxy.contoso.com' + } + + $proxySettingsNoAccessAccount = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + EnableProxy = $true + ProxyServerPort = 80 + ProxyServerName = 'Proxy.contoso.com' + } + + $proxySettingNoEnableProxy = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + ProxyServerPort = 80 + } + + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + } + + It 'Should return desired result false when non proxy settings do not match' { + Test-TargetResource @testDSCValues | Should -Be $false + } + + It 'Should return desired result false when when specifying Account and UseSiteServerAccount' { + Test-TargetResource @siteServerAndAccount | Should -Be $false + } + + It 'Should return desired result true when when proxy settings match' { + Test-TargetResource @proxySettingsMatch | Should -Be $true + } + + It 'Should return desired result false with no ProxyServerName and settings do not match' { + Test-TargetResource @proxySettingsNoServerName | Should -Be $false + } + + It 'Should return desired result false with no proxy port and settings do not match' { + Test-TargetResource @proxySettingsNoPort | Should -Be $false + } + + It 'Should return desired result false with no proxy access account and settings do not match' { + Test-TargetResource @proxySettingsNoAccessAccount | Should -Be $false + } + + It 'Should return desired result false with no EnableProxy and setting a proxy settings that does not match' { + Test-TargetResource @proxySettingNoEnableProxy | Should -Be $false + } + + It 'Should return desired result false with resetting proxy access account to system account' { + Test-TargetResource @proxyAccessAccount | Should -Be $false + } + + It 'Should return desired result false when expecting absent but is present' { + Test-TargetResource @absentInput | Should -Be $false + } + } + + Context 'When Get-TargetResource turns absent' { + BeforeEach { + $getReturnAbsent = @{ + SiteCode = 'Lab' + SiteSystemServer = 'SS01.contoso.com' + PublicFqdn = 'SS01.contoso.com' + FdmOperation = $null + UseSiteServerAccount = $null + AccountName = $null + EnableProxy = $null + ProxyServerName = $null + ProxyServerPort = $null + ProxyAccessAccount = $null + Ensure = 'Absent' + RoleCount = $null + } + + Mock -CommandName Get-TargetResource -MockWith { $getReturnAbsent } + } + + It 'Should return desired result true when expecting absent and is absent' { + Test-TargetResource @absentInput | Should -Be $true + } + + It 'Should return desired result false when expecting present and is absent' { + Test-TargetResource @testDSCValues | Should -Be $false + } + } + } + } +} +finally +{ + Invoke-TestCleanup +} diff --git a/tests/Unit/Stubs/ConfigMgrCBDscStub.psm1 b/tests/Unit/Stubs/ConfigMgrCBDscStub.psm1 index f8d8ec6..2d9c25a 100644 --- a/tests/Unit/Stubs/ConfigMgrCBDscStub.psm1 +++ b/tests/Unit/Stubs/ConfigMgrCBDscStub.psm1 @@ -45243,8 +45243,7 @@ function Set-CMSiteSystemServer [Parameter(ParameterSetName='SearchByNameMandatory')] [Parameter(ParameterSetName='SearchByValueMandatory')] - [PSTypeName('IResultObject#SMS_SCI_Reserved')] - [System.Object] + [string] ${ProxyAccessAccount}, [Parameter(ParameterSetName='SearchByValueMandatory', Mandatory=$true, ValueFromPipeline=$true)]