diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 index 015e3fc95..6b85880f4 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPManagedAccount/MSFT_xSPManagedAccount.psm1 @@ -48,18 +48,19 @@ function Set-TargetResource [parameter(Mandatory = $false)] [System.String] $Schedule, [parameter(Mandatory = $true)] [System.String] $AccountName ) - - Write-Verbose -Message "Setting managed account $AccountName" - Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { - $params = $args[0] - - - $current = Get-TargetResource @params - if ($current.Count -eq 0) { + if ($null -eq (Get-TargetResource @PSBoundParameters)) { + Write-Verbose "Creating a new managed account" + Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { + $params = $args[0] New-SPManagedAccount -Credential $params.Account } + } + Write-Verbose -Message "Updating settings for managed account" + Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { + $params = $args[0] + $updateParams = @{ Identity = $params.Account.UserName } diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 index 3fad07354..a27731b3c 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPSecureStoreServiceApp/MSFT_xSPSecureStoreServiceApp.psm1 @@ -69,36 +69,36 @@ function Set-TargetResource ) $result = Get-TargetResource @PSBoundParameters + $params = $PSBoundParameters + + switch((Get-xSharePointInstalledProductVersion).FileMajorPart) { + 16 { + $hasOptionalParams = $false + @("AuditlogMaxSize","DatabaseName","DatabasePassword","DatabaseServer","DatabaseUsername", ` + "FailoverDatabaseServer","PartitionMode","Sharing","DatabaseCredentials") | ForEach-Object { + if ($PSBoundParameters.ContainsKey($_) -eq $true) { $hasOptionalParams = $true } + } + if ($hasOptionalParams -eq $false) { + # Add the MinDB param to ensure that the cmdlet call gets differentiated without the optional params being set + $params.Add("EnableMinDB", $false) + } + } + } - if ($result.Count -eq 0) { + if ($null -eq $result) { Write-Verbose -Message "Creating Secure Store Service Application $Name" - Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { + Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $params -ScriptBlock { $params = $args[0] - if ($params.ContainsKey("InstallAccount")) { $params.Remove("InstallAccount") | Out-Null } - switch((Get-xSharePointInstalledProductVersion).FileMajorPart) { - 15 { - $app = New-SPSecureStoreServiceApplication @params - } - 16 { - $app = New-SPSecureStoreServiceApplication @params -EnableMinDB:$false - } - Default { - throw [Exception] "An unknown version of SharePoint (Major version $_) was detected. Only versions 15 (SharePoint 2013) or 16 (SharePoint 2016) are supported." - } - } - if ($app) { - New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy" -ServiceApplication $app - } + New-SPSecureStoreServiceApplication @params | New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy" } } else { if ([string]::IsNullOrEmpty($ApplicationPool) -eq $false -and $ApplicationPool -ne $result.ApplicationPool) { Write-Verbose -Message "Updating Secure Store Service Application $Name" Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { $params = $args[0] - $serviceApp = Get-SPServiceApplication -Name $params.Name | Where-Object { $_.TypeName -eq "Secure Store Service Application" } $appPool = Get-SPServiceApplicationPool -Identity $params.ApplicationPool diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 index 4e5698f54..1c7f12ca4 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileServiceApp/MSFT_xSPUserProfileServiceApp.psm1 @@ -35,7 +35,18 @@ function Get-TargetResource } else { - $databases = Get-UserProfileServiceProperties $serviceApp + $databases = @{} + $propData = $serviceApp.GetType().GetProperties([System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic) + + $socialProp = $propData | Where-Object {$_.Name -eq "SocialDatabase"} + $databases.Add("SocialDatabase", $socialProp.GetValue($serviceApp)) + + $profileProp = $propData | Where-Object {$_.Name -eq "ProfileDatabase"} + $databases.Add("ProfileDatabase", $profileProp.GetValue($serviceApp)) + + $syncProp = $propData | Where-Object {$_.Name -eq "SynchronizationDatabase"} + $databases.Add("SynchronizationDatabase", $syncProp.GetValue($serviceApp)) + $spFarm = Get-SPFarm if ($params.FarmAccount.UserName -eq $spFarm.DefaultServiceAccount.Name) { @@ -62,28 +73,6 @@ function Get-TargetResource return $result } -function Get-UserProfileServiceProperties() { - [CmdletBinding()] - [OutputType([System.Collections.Hashtable])] - param - ( - [parameter(Mandatory = $true)] [System.String] $serviceApp - ) - $results = @{} - $propData = $serviceApp.GetType().GetProperties([System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::NonPublic) - - $socialProp = $propData | Where-Object {$_.Name -eq "SocialDatabase"} - $results.Add("SocialDatabase", $socialProp.GetValue($serviceApp)) - - $profileProp = $propData | Where-Object {$_.Name -eq "ProfileDatabase"} - $results.Add("ProfileDatabase", $profileProp.GetValue($serviceApp)) - - $syncProp = $propData | Where-Object {$_.Name -eq "SynchronizationDatabase"} - $results.Add("SynchronizationDatabase", $syncProp.GetValue($serviceApp)) - - return $results -} - function Set-TargetResource { [CmdletBinding()] diff --git a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileSyncService/MSFT_xSPUserProfileSyncService.psm1 b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileSyncService/MSFT_xSPUserProfileSyncService.psm1 index 01d422ee3..bfa80d63d 100644 --- a/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileSyncService/MSFT_xSPUserProfileSyncService.psm1 +++ b/Modules/xSharePoint/DSCResources/MSFT_xSPUserProfileSyncService/MSFT_xSPUserProfileSyncService.psm1 @@ -9,6 +9,11 @@ function Get-TargetResource [parameter(Mandatory = $true)] [System.Management.Automation.PSCredential] $FarmAccount, [parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount ) + + if ((Get-xSharePointInstalledProductVersion).FileMajorPart -ne 15) { + throw [Exception] "Only SharePoint 2013 is supported to deploy the user profile sync service via DSC, as 2016 does not use the FIM based sync service." + } + Write-Verbose -Message "Getting the local user profile sync service instance" $result = Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock { @@ -59,6 +64,10 @@ function Set-TargetResource [parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount ) + if ((Get-xSharePointInstalledProductVersion).FileMajorPart -ne 15) { + throw [Exception] "Only SharePoint 2013 is supported to deploy the user profile sync service via DSC, as 2016 does not use the FIM based sync service." + } + Write-Verbose -Message "Setting User Profile Synchronization Service" # Add the FarmAccount to the local Admins group, if it's not already there @@ -129,6 +138,10 @@ function Test-TargetResource [parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount ) + if ((Get-xSharePointInstalledProductVersion).FileMajorPart -ne 15) { + throw [Exception] "Only SharePoint 2013 is supported to deploy the user profile sync service via DSC, as 2016 does not use the FIM based sync service." + } + $CurrentValues = Get-TargetResource @PSBoundParameters Write-Verbose -Message "Testing for User Profile Synchronization Service" return Test-xSharePointSpecificParameters -CurrentValues $CurrentValues -DesiredValues $PSBoundParameters -ValuesToCheck @("Ensure") diff --git a/Tests/xSharePoint/xSharePoint.xSPBCSServiceApp.Tests.ps1 b/Tests/xSharePoint/xSharePoint.xSPBCSServiceApp.Tests.ps1 index bd4d25a3b..4f8a2023e 100644 --- a/Tests/xSharePoint/xSharePoint.xSPBCSServiceApp.Tests.ps1 +++ b/Tests/xSharePoint/xSharePoint.xSPBCSServiceApp.Tests.ps1 @@ -34,7 +34,7 @@ Describe "xSPBCSServiceApp" { Import-Module $Global:CurrentSharePointStubModule -WarningAction SilentlyContinue - Context "When no service application exists in the current farm" { + Context "When no service applications exist in the current farm" { Mock Get-SPServiceApplication { return $null } Mock New-SPBusinessDataCatalogServiceApplication { } @@ -54,6 +54,19 @@ Describe "xSPBCSServiceApp" { } } + Context "When service applications exist in the current farm but the specific BCS app does not" { + + Mock Get-SPServiceApplication { return @(@{ + TypeName = "Some other service app type" + }) } + + It "returns null from the Get method" { + Get-TargetResource @testParams | Should BeNullOrEmpty + Assert-MockCalled Get-SPServiceApplication -ParameterFilter { $Name -eq $testParams.Name } + } + + } + Context "When a service application exists and is configured correctly" { Mock Get-SPServiceApplication { return @(@{ diff --git a/Tests/xSharePoint/xSharePoint.xSPUserProfileServiceApp.Tests.ps1 b/Tests/xSharePoint/xSharePoint.xSPUserProfileServiceApp.Tests.ps1 index 329478f9d..8d1b09f64 100644 --- a/Tests/xSharePoint/xSharePoint.xSPUserProfileServiceApp.Tests.ps1 +++ b/Tests/xSharePoint/xSharePoint.xSPUserProfileServiceApp.Tests.ps1 @@ -27,20 +27,6 @@ Describe "xSPUserProfileServiceApp" { Import-Module $Global:CurrentSharePointStubModule -WarningAction SilentlyContinue - Mock Get-UserProfileServiceProperties { return @{ - ProfileDatabase = @{ - Name = "SP_ProfileDB" - Server = @{ Name = "SQL.domain.local" } - } - SocialDatabase = @{ - Name = "SP_SocialDB" - Server = @{ Name = "SQL.domain.local" } - } - SynchronizationDatabase = @{ - Name = "SP_SyncDB" - Server = @{ Name = "SQL.domain.local" } - } - }} Mock Get-SPFarm { return @{ DefaultServiceAccount = @{ Name = $testParams.FarmAccount.Username } }} @@ -72,11 +58,48 @@ Describe "xSPUserProfileServiceApp" { Context "When a service application exists and is configured correctly" { Mock Get-SPServiceApplication { - return @(@{ - TypeName = "User Profile Service Application" - DisplayName = $testParams.Name - ApplicationPool = @{ Name = $testParams.ApplicationPool } - }) + return @( + New-Object Object | + Add-Member NoteProperty TypeName "User Profile Service Application" -PassThru | + Add-Member NoteProperty DisplayName $testParams.Name -PassThru | + Add-Member NoteProperty ApplicationPool @{ Name = $testParams.ApplicationPool } -PassThru | + Add-Member ScriptMethod GetType { + New-Object Object | + Add-Member ScriptMethod GetProperties { + param($x) + return @( + (New-Object Object | + Add-Member NoteProperty Name "SocialDatabase" -PassThru | + Add-Member ScriptMethod GetValue { + param($x) + return @{ + Name = "SP_SocialDB" + Server = @{ Name = "SQL.domain.local" } + } + } -PassThru + ), + (New-Object Object | + Add-Member NoteProperty Name "ProfileDatabase" -PassThru | + Add-Member ScriptMethod GetValue { + return @{ + Name = "SP_ProfileDB" + Server = @{ Name = "SQL.domain.local" } + } + } -PassThru + ), + (New-Object Object | + Add-Member NoteProperty Name "SynchronizationDatabase" -PassThru | + Add-Member ScriptMethod GetValue { + return @{ + Name = "SP_ProfileSyncDB" + Server = @{ Name = "SQL.domain.local" } + } + } -PassThru + ) + ) + } -PassThru + } -PassThru -Force + ) } It "returns values from the get method" { diff --git a/Tests/xSharePoint/xSharePoint.xSPUserProfileSyncService.Tests.ps1 b/Tests/xSharePoint/xSharePoint.xSPUserProfileSyncService.Tests.ps1 index 322e0e04a..088953a3b 100644 --- a/Tests/xSharePoint/xSharePoint.xSPUserProfileSyncService.Tests.ps1 +++ b/Tests/xSharePoint/xSharePoint.xSPUserProfileSyncService.Tests.ps1 @@ -21,6 +21,11 @@ Describe "xSPUserProfileSyncService" { } Import-Module (Join-Path ((Resolve-Path $PSScriptRoot\..\..).Path) "Modules\xSharePoint") + Import-Module $Global:CurrentSharePointStubModule -WarningAction SilentlyContinue + $versionBeingTested = (Get-Item $Global:CurrentSharePointStubModule).Directory.BaseName + $majorBuildNumber = $versionBeingTested.Substring(0, $versionBeingTested.IndexOf(".")) + Mock Get-xSharePointInstalledProductVersion { return @{ FileMajorPart = $majorBuildNumber } } + Mock Invoke-xSharePointCommand { return Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments -NoNewScope } @@ -38,124 +43,149 @@ Describe "xSPUserProfileSyncService" { Mock Remove-xSharePointUserToLocalAdmin { } Mock New-PSSession { return $null } -ModuleName "xSharePoint.Util" - Context "User profile sync service is not running and should be" { - Mock Get-SPServiceInstance { if ($Global:xSharePointUPACheck -eq $false) { - $Global:xSharePointUPACheck = $true - return @( @{ - Status = "Disabled" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::Empty - TypeName = "User Profile Synchronization Service" - }) - } else { - return @( @{ - Status = "Online" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::NewGuid() - TypeName = "User Profile Synchronization Service" - }) + switch ($majorBuildNumber) { + 15 { + Context "User profile sync service is not running and should be" { + Mock Get-SPServiceInstance { if ($Global:xSharePointUPACheck -eq $false) { + $Global:xSharePointUPACheck = $true + return @( @{ + Status = "Disabled" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::Empty + TypeName = "User Profile Synchronization Service" + }) + } else { + return @( @{ + Status = "Online" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::NewGuid() + TypeName = "User Profile Synchronization Service" + }) + } + } + Mock Get-SPServiceApplication { return @( + New-Object Object | + Add-Member NoteProperty ID ([Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f")) -PassThru | + Add-Member NoteProperty TypeName "User Profile Service Application" -PassThru | + Add-Member ScriptMethod SetSynchronizationMachine { + param($computerName, $syncServiceID, $FarmUserName, $FarmPassword) + } -PassThru + )} + + It "returns absent from the get method" { + $Global:xSharePointUPACheck = $false + (Get-TargetResource @testParams).Ensure | Should Be "Absent" } - } - Mock Get-SPServiceApplication { return @( - New-Object Object | - Add-Member NoteProperty ID ([Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f")) -PassThru | - Add-Member NoteProperty TypeName "User Profile Service Application" -PassThru | - Add-Member ScriptMethod SetSynchronizationMachine { - param($computerName, $syncServiceID, $FarmUserName, $FarmPassword) - } -PassThru - )} - - It "returns absent from the get method" { - $Global:xSharePointUPACheck = $false - (Get-TargetResource @testParams).Ensure | Should Be "Absent" - } - It "returns false from the test method" { - $Global:xSharePointUPACheck = $false - Test-TargetResource @testParams | Should Be $false - } + It "returns false from the test method" { + $Global:xSharePointUPACheck = $false + Test-TargetResource @testParams | Should Be $false + } - It "calls the start service cmdlet from the set method" { - $Global:xSharePointUPACheck = $false - Set-TargetResource @testParams + It "calls the start service cmdlet from the set method" { + $Global:xSharePointUPACheck = $false + Set-TargetResource @testParams - Assert-MockCalled Start-SPServiceInstance + Assert-MockCalled Start-SPServiceInstance + } } - } - Context "User profile sync service is running and should be" { - Mock Get-SPServiceInstance { return @( @{ - Status = "Online" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::NewGuid() - TypeName = "User Profile Synchronization Service" - }) - } + Context "User profile sync service is running and should be" { + Mock Get-SPServiceInstance { return @( @{ + Status = "Online" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::NewGuid() + TypeName = "User Profile Synchronization Service" + }) + } - It "returns present from the get method" { - (Get-TargetResource @testParams).Ensure | Should Be "Present" - } + It "returns present from the get method" { + (Get-TargetResource @testParams).Ensure | Should Be "Present" + } - It "returns true from the test method" { - Test-TargetResource @testParams | Should Be $true + It "returns true from the test method" { + Test-TargetResource @testParams | Should Be $true + } } - } - $testParams.Ensure = "Absent" - - Context "User profile sync service is running and shouldn't be" { - Mock Get-SPServiceInstance { if ($Global:xSharePointUPACheck -eq $false) { - $Global:xSharePointUPACheck = $true - return @( @{ - Status = "Online" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::NewGuid() - TypeName = "User Profile Synchronization Service" - }) - } else { - return @( @{ - Status = "Disabled" - ID = [Guid]::Empty - UserProfileApplicationGuid = [Guid]::Empty - TypeName = "User Profile Synchronization Service" - }) + $testParams.Ensure = "Absent" + + Context "User profile sync service is running and shouldn't be" { + Mock Get-SPServiceInstance { if ($Global:xSharePointUPACheck -eq $false) { + $Global:xSharePointUPACheck = $true + return @( @{ + Status = "Online" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::NewGuid() + TypeName = "User Profile Synchronization Service" + }) + } else { + return @( @{ + Status = "Disabled" + ID = [Guid]::Empty + UserProfileApplicationGuid = [Guid]::Empty + TypeName = "User Profile Synchronization Service" + }) + } + } + + It "returns present from the get method" { + $Global:xSharePointUPACheck = $false + (Get-TargetResource @testParams).Ensure | Should Be "Present" } - } - It "returns present from the get method" { - $Global:xSharePointUPACheck = $false - (Get-TargetResource @testParams).Ensure | Should Be "Present" - } + It "returns false from the test method" { + $Global:xSharePointUPACheck = $false + Test-TargetResource @testParams | Should Be $false + } - It "returns false from the test method" { - $Global:xSharePointUPACheck = $false - Test-TargetResource @testParams | Should Be $false + It "calls the start service cmdlet from the set method" { + $Global:xSharePointUPACheck = $false + Set-TargetResource @testParams + + Assert-MockCalled Stop-SPServiceInstance + } } - It "calls the start service cmdlet from the set method" { - $Global:xSharePointUPACheck = $false - Set-TargetResource @testParams + Context "User profile sync service is not running and shouldn't be" { + Mock Get-SPServiceInstance { return @( @{ + Status = "Disabled" + ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") + UserProfileApplicationGuid = [Guid]::Empty + TypeName = "User Profile Synchronization Service" + }) + } + + It "returns absent from the get method" { + (Get-TargetResource @testParams).Ensure | Should Be "Absent" + } - Assert-MockCalled Stop-SPServiceInstance + It "returns true from the test method" { + Test-TargetResource @testParams | Should Be $true + } } - } - - Context "User profile sync service is not running and shouldn't be" { - Mock Get-SPServiceInstance { return @( @{ - Status = "Disabled" - ID = [Guid]::Parse("21946987-5163-418f-b781-2beb83aa191f") - UserProfileApplicationGuid = [Guid]::Empty - TypeName = "User Profile Synchronization Service" - }) - } - - It "returns absent from the get method" { - (Get-TargetResource @testParams).Ensure | Should Be "Absent" } - - It "returns true from the test method" { - Test-TargetResource @testParams | Should Be $true + 16 { + Context "All methods throw exceptions as user profile sync doesn't exist in 2016" { + It "throws on the get method" { + { Get-TargetResource @testParams } | Should Throw + } + + It "throws on the test method" { + { Test-TargetResource @testParams } | Should Throw + } + + It "throws on the set method" { + { Set-TargetResource @testParams } | Should Throw + } + } } } + + + + + + } } \ No newline at end of file