Skip to content

Commit

Permalink
Fixed issue with 2016 calls to secure store cmdlets
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianFarnhill committed Sep 27, 2015
1 parent beeaf95 commit 98d527f
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +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 ($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 {
New-SPSecureStoreServiceApplication @params | New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy"
}
16 {
$app = New-SPSecureStoreServiceApplication @params -EnableMinDB:$false | New-SPSecureStoreServiceApplicationProxy -Name "$($params.Name) Proxy"
}
Default {
throw [Exception] "An unknown version of SharePoint (Major version $_) was detected. Only versions 15 (SharePoint 2013) or 16 (SharePoint 2016) are supported."
}
}
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
228 changes: 129 additions & 99 deletions Tests/xSharePoint/xSharePoint.xSPUserProfileSyncService.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}
}
}






}
}

0 comments on commit 98d527f

Please sign in to comment.