DSCResources/MSFT_SPSearchIndexPartition/MSFT_SPSearchIndexPartition.psm1

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.UInt32]
        $Index,

        [Parameter(Mandatory = $true)]
        [System.String[]]
        $Servers,

        [Parameter()]
        [System.String]
        $RootDirectory,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ServiceAppName
    )

    Write-Verbose -Message "Getting Search Index Partition '$Index' settings"

    $result = Invoke-SPDscCommand -Arguments $PSBoundParameters `
        -ScriptBlock {
        $params = $args[0]
        $ConfirmPreference = 'None'

        $ssa = Get-SPEnterpriseSearchServiceApplication -Identity $params.ServiceAppName -Verbose:$false
        $currentTopology = $ssa.ActiveTopology

        $searchComponent = Get-SPEnterpriseSearchComponent -SearchTopology $currentTopology -Verbose:$false | `
                Where-Object -FilterScript {
                ($_.GetType().Name -eq "IndexComponent") `
                    -and ($_.IndexPartitionOrdinal -eq $params.Index)
            }

        $IndexComponents = $searchComponent.ServerName
        $rootDirectory = ($searchComponent | Select-Object -First 1).RootDirectory

        if ($rootDirectory -eq "")
        {
            $ssi = Get-SPEnterpriseSearchServiceInstance -Verbose:$false
            $component = $ssi.Components | Select-Object -First 1
            $rootDirectory = $component.IndexLocation
        }

        return @{
            Index          = $params.Index
            Servers        = $IndexComponents
            RootDirectory  = $rootDirectory
            ServiceAppName = $params.ServiceAppName
        }
    }
    return $result
}

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.UInt32]
        $Index,

        [Parameter(Mandatory = $true)]
        [System.String[]]
        $Servers,

        [Parameter()]
        [System.String]
        $RootDirectory,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ServiceAppName
    )

    Write-Verbose -Message "Setting Search Index Partition '$Index' settings"

    $CurrentValues = Get-TargetResource @PSBoundParameters

    Invoke-SPDscCommand -Arguments @($PSBoundParameters, $CurrentValues) `
        -ScriptBlock {
        $params = $args[0]
        $CurrentValues = $args[1]
        $ConfirmPreference = 'None'

        $AllSearchServers = $params.Servers

        # Ensure the search service instance is running on all servers
        foreach ($searchServer in $AllSearchServers)
        {
            $searchService = Get-SPEnterpriseSearchServiceInstance -Identity $searchServer `
                -Verbose:$false

            if ($searchService.Status -eq "Offline")
            {
                Write-Verbose -Message "Start Search Service Instance"
                Start-SPEnterpriseSearchServiceInstance -Identity $searchService -Verbose:$false
            }

            #Wait for Search Service Instance to come online
            $loopCount = 0
            $online = Get-SPEnterpriseSearchServiceInstance -Identity $searchServer -Verbose:$false
            do
            {
                $online = Get-SPEnterpriseSearchServiceInstance -Identity $searchServer -Verbose:$false
                Write-Verbose -Message "Waiting for service: $($online.TypeName)"
                $loopCount++
                Start-Sleep -Seconds 30
            }
            until ($online.Status -eq "Online" -or $loopCount -eq 20)
        }

        if ($params.ContainsKey("RootDirectory") -eq $true)
        {
            # Create the index partition directory on each remote server
            foreach ($IndexPartitionServer in $params.Servers)
            {
                $networkPath = "\\$IndexPartitionServer\" + $params.RootDirectory.Replace(":\", "$\")
                New-Item -Path $networkPath -ItemType Directory -Force
            }

            # Create the directory on the local server as it will not apply the topology without it
            if ((Test-Path -Path $params.RootDirectory) -eq $false)
            {
                New-Item $params.RootDirectory -ItemType Directory -Force
            }
        }

        # Get all service service instances to assign topology components to
        $AllSearchServiceInstances = @{ }
        foreach ($server in $AllSearchServers)
        {
            $si = Get-SPEnterpriseSearchServiceInstance -Identity $server -Verbose:$false
            $AllSearchServiceInstances.Add($server, $si)
        }

        Write-Verbose -Message 'Get current topology and prepare a new one'
        $ssa = Get-SPEnterpriseSearchServiceApplication -Identity $params.ServiceAppName `
            -Verbose:$false
        $currentTopology = $ssa.ActiveTopology
        $newTopology = New-SPEnterpriseSearchTopology -SearchApplication $ssa `
            -Clone `
            -SearchTopology $currentTopology `
            -Verbose:$false

        $componentTypes = @{
            Servers = "IndexComponent"
        }

        Write-Verbose -Message 'Build up the topology changes for each object type'
        @("Servers") | ForEach-Object -Process {
            $CurrentSearchProperty = $_
            Write-Verbose -Message "Setting components for '$CurrentSearchProperty' property"
            $ComponentsToRemove = @()

            if ($null -eq $CurrentValues.$CurrentSearchProperty)
            {
                $ComponentsToAdd = $params.$CurrentSearchProperty
            }
            else
            {
                $ComponentsToAdd = @()

                $components = $params.$CurrentSearchProperty | Where-Object -FilterScript {
                    $CurrentValues.$CurrentSearchProperty.Contains($_) -eq $false
                }
                foreach ($component in $components)
                {
                    $ComponentsToAdd += $component
                }
                $components = $CurrentValues.$CurrentSearchProperty | Where-Object -FilterScript {
                    $params.$CurrentSearchProperty.Contains($_) -eq $false
                }
                foreach ($component in $components)
                {
                    $ComponentsToRemove += $component
                }
            }
            foreach ($componentToAdd in $ComponentsToAdd)
            {
                Write-Verbose -Message "Adding $($AllSearchServiceInstances.$componentToAdd) to the topology"
                $NewComponentParams = @{
                    SearchTopology        = $newTopology
                    SearchServiceInstance = $AllSearchServiceInstances.$componentToAdd
                }
                switch ($componentTypes.$CurrentSearchProperty)
                {
                    "IndexComponent"
                    {
                        $NewComponentParams.Add("IndexPartition", $params.Index)
                        if ($params.ContainsKey("RootDirectory") -eq $true)
                        {
                            if ([string]::IsNullOrEmpty($params.RootDirectory) -eq $false)
                            {
                                $NewComponentParams.Add("RootDirectory", $params.RootDirectory)
                            }
                        }
                        New-SPEnterpriseSearchIndexComponent @NewComponentParams -Verbose:$false
                    }
                }
            }
            foreach ($componentToRemove in $ComponentsToRemove)
            {
                Write-Verbose -Message "Removing $($componentToRemove) from the topology"
                $component = Get-SPEnterpriseSearchComponent -SearchTopology $newTopology `
                    -Verbose:$false | Where-Object -FilterScript {
                    ($_.GetType().Name -eq $componentTypes.$CurrentSearchProperty) `
                        -and ($_.ServerName -eq $componentToRemove) `
                        -and ($_.IndexPartitionOrdinal -eq $params.Index)
                }

                if ($null -ne $component)
                {
                    $component | Remove-SPEnterpriseSearchComponent -SearchTopology $newTopology `
                        -Verbose:$false `
                        -Confirm:$false
                }
            }
        }

        Write-Verbose -Message 'Apply the new topology'
        Set-SPEnterpriseSearchTopology -Identity $newTopology -Verbose:$false
    }
}

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.UInt32]
        $Index,

        [Parameter(Mandatory = $true)]
        [System.String[]]
        $Servers,

        [Parameter()]
        [System.String]
        $RootDirectory,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ServiceAppName
    )

    Write-Verbose -Message "Testing Search Index Partition '$Index' settings"

    $CurrentValues = Get-TargetResource @PSBoundParameters

    Write-Verbose -Message "Current Values: $(Convert-SPDscHashtableToString -Hashtable $CurrentValues)"
    Write-Verbose -Message "Target Values: $(Convert-SPDscHashtableToString -Hashtable $PSBoundParameters)"

    $result = Test-SPDscParameterState -CurrentValues $CurrentValues `
        -Source $($MyInvocation.MyCommand.Source) `
        -DesiredValues $PSBoundParameters `
        -ValuesToCheck @("Servers")

    Write-Verbose -Message "Test-TargetResource returned $result"

    return $result
}

function Export-TargetResource
{
    $VerbosePreference = "SilentlyContinue"
    $ParentModuleBase = Get-Module "SharePointDsc" -ListAvailable | Select-Object -ExpandProperty Modulebase
    $module = Join-Path -Path $ParentModuleBase -ChildPath  "\DSCResources\MSFT_SPSearchIndexPartition\MSFT_SPSearchIndexPartition.psm1" -Resolve
    $Content = ''
    $params = Get-DSCFakeParameters -ModulePath $module

    $ssas = Get-SPServiceApplication | Where-Object -FilterScript { $_.GetType().FullName -eq "Microsoft.Office.Server.Search.Administration.SearchServiceApplication" }

    $i = 1
    $total = $ssas.Length
    foreach ($ssa in $ssas)
    {
        try
        {
            if ($null -ne $ssa)
            {
                $serviceName = $ssa.DisplayName
                Write-Host "Scanning Index Partitions for Search Service Application [$i/$total] {$serviceName}"

                $ssa = Get-SPEnterpriseSearchServiceApplication -Identity $ssa
                $currentTopology = $ssa.ActiveTopology
                $indexComponents = Get-SPEnterpriseSearchComponent -SearchTopology $currentTopology | `
                        Where-Object -FilterScript {
                        $_.GetType().Name -eq "IndexComponent"
                    }

                [System.Collections.ArrayList]$indexesAlreadyScanned = @()
                $j = 1
                $totalIndex = $indexComponents.Length
                foreach ($indexComponent in $indexComponents)
                {
                    try
                    {
                        if (!$indexesAlreadyScanned.Contains($indexComponent.IndexPartitionOrdinal))
                        {
                            $icServerName = $indexComponent.ServerName
                            Write-Host " -> Index Component [$j/$totalIndex] {$icServerName}"

                            $indexesAlreadyScanned += $indexComponent.IndexPartitionOrdinal
                            $PartialContent = " SPSearchIndexPartition " + [System.Guid]::NewGuid().ToString() + "`r`n"
                            $PartialContent += " {`r`n"
                            $params.ServiceAppName = $serviceName
                            $params.Index = $indexComponent.IndexPartitionOrdinal
                            $params.Servers = $indexComponent.ServerName
                            $params.RootDirectory = $indexComponent.RootDirectory
                            $results = Get-TargetResource @params

                            Add-ConfigurationDataEntry -Node "NonNodeData" -Key "SearchIndexPartitionServers" -Value $results.Servers -Description "List of Servers that will host the Search Index Partitions;"
                            $results.Servers = "`$ConfigurationData.NonNodeData.SearchIndexPartitionServers"

                            $results = Repair-Credentials -results $results

                            $currentBlock = Get-DSCBlock -Params $results -ModulePath $module
                            $currentBlock = Convert-DSCStringParamToVariable -DSCBlock $currentBlock -ParameterName "PsDscRunAsCredential"
                            $currentBlock = Convert-DSCStringParamToVariable -DSCBlock $currentBlock -ParameterName "Servers"
                            $PartialContent += $currentBlock
                            $PartialContent += " }`r`n"
                            $Content += $PartialContent
                            $j++
                        }
                    }
                    catch
                    {
                        $Global:ErrorLog += "[Index Component]" + $indexComponent.ServerName + "`r`n"
                        $Global:ErrorLog += "$_`r`n`r`n"
                    }
                }
            }
            $i++
        }
        catch
        {
            $Global:ErrorLog += "[Search Index Partition]" + $ssa.DisplayName + "`r`n"
            $Global:ErrorLog += "$_`r`n`r`n"
        }
    }
    return $Content
}

Export-ModuleMember -Function *-TargetResource