Questo script PowerShell permette di creare o modificare la topologia della search di SharePoint 2016

PowerShell: Create-SPSearchTopology.ps1

<#
.SYNOPSIS
Gestione SharePoint Search Topology

.DESCRIPTION
Questo script crea o aggiorna una topologia di Search di SharePoint 2013 / 2016

Create-SPSearchTopology.ps1 -topology "<host1>\<components1>;<host2>\<components2>,<ecc...>"

Legenda componenti:
# - A = Admin
# - C = Crawler
# - P = Content Processing
# - Y = Analytics Processing
# - Q = Query Processing
# - I = Index Partition 0
# - 0 = Index Partition 0
# - 1 = Index Partition 1
# - 2 = Index Partition 2
# - 3 = Index Partition 3

http://www.sgart.it

.EXAMPLE
Create-SPSearchTopology.ps1 -topology "VM-SPFE01\QI;VM-SPAP02\ACPY"

.EXAMPLE
Create-SPSearchTopology.ps1 "VM-SPFE01\QI;VM-SPAP02\ACPY"
#>
#------------------------------------------------------------------
# Author: Sgart.it
# Date:2025-04-14
#------------------------------------------------------------------
Param (
    [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName )]
    [string]$topology
)
#------------------------------------------------------------------
#$configurations = @(
#	[pscustomobject]@{host="VM-SPFE01"; components="----QI"},
#	[pscustomobject]@{host="VM-SPAP02"; components="ACPY--"}
#)
$configurations = @()
#------------------------------------------------------------------
if($false -eq [string]::IsNullOrWhiteSpace($topology)){
    write-output "Set topology from parameters: `"$topology`""
    $configurations = @()
    $arr = $topology.Trim().Split(@(';',',', [StringsplitOptions]::RemoveEmptyEntries))
    if($null -ne $arr) {
        $arr | ForEach-Object {
            $s = $_.Trim().Split(@('\','/'), [StringsplitOptions]::RemoveEmptyEntries)
            if($null -ne $s -and $s.Count -gt 1){
                $c = [pscustomobject]@{host=$s[0].Trim(); components=$s[1].Trim().ToUpper()};
                $configurations += $c
            }
        }
    }
}
#------------------------------------------------------------------
Write-Output "Configurations:"
if($null -eq $configurations -or $configurations.count -eq 0){
    Write-Output "NULL"
    exit
}
$configurations | ForEach-Object {
	$hosts = $_.host
	$components = $_.components
	Write-Output "- $hosts = $components"
}
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Write-Output "Starting EnterpriseSearchServiceInstance..."
$configurations | ForEach-Object {
	$hosts = $_.host
	Write-Output "Host $hosts start"
	
	$ss1 = Get-SPEnterpriseSearchServiceInstance -Identity $hosts
	if ($ss1.Status -ne "Online") {
	
		Start-SPEnterpriseSearchServiceInstance -Identity $hosts
		
		Write-Output "wait"

		$ss1 = Get-SPEnterpriseSearchServiceInstance -Identity $hosts
		while ($ss1.Status -ne "Online")
		{
			Write-Output "."
			Start-Sleep 5
			$ss1 = Get-SPEnterpriseSearchServiceInstance -Identity $hosts
		}
	} else {
		Write-Output " already online"
	}
}

$searchSvc = Get-SPEnterpriseSearchServiceInstance -Local
if ($searchSvc -eq $null)
{
    throw "Unable to retrieve search service"
}
 
#Set Search Service parameter 
$searchSvc | Set-SPEnterpriseSearchServiceInstance
 
if ($searchSvc.Status -ne "Online")
{
	write-output "Start-SPServiceInstance"
	$searchSvc | Start-SPServiceInstance
}

#Declare New Topology
Write-Output "Define Topology"
$ssa = Get-SPEnterpriseSearchServiceApplication

# Create a new search topology and a reference to the new search topology
$active = Get-SPEnterpriseSearchTopology -Active -SearchApplication $ssa 
if($null -eq $active -or $active.State -ne "Active"){
	write-output "New"
	$newTopology = New-SPEnterpriseSearchTopology -SearchApplication $ssa
}else{
	write-output "Clone"
	$newTopology = New-SPEnterpriseSearchTopology -SearchApplication $ssa -Clone -SearchTopology $active
	write-output "Remove components"
    $ids = $newTopology.GetComponents() | select ComponentID
	$ids | ForEach-Object {
		$id = $_.ComponentId
		write-output " ComponentId $id"
		$c =get-SPEnterpriseSearchComponent -SearchTopology $newTopology | where {$_.ComponentId -eq $id}
		$newTopology.RemoveComponent($c)
	}
}


#Define all required components
Write-Output "Define all required components"
$configurations | ForEach-Object {
	$hosts = $_.host
	$comp = $_.components.ToUpper()
	Write-Output "Host $hosts starting"
	
	if($true -eq $comp.contains("A")) {
		Write-Output "- Admin"
		New-SPEnterpriseSearchAdminComponent -SearchTopology $newTopology -SearchServiceInstance $hosts
	}
	if($true -eq $comp.contains("C")) {
		Write-Output "- Crawl"
		New-SPEnterpriseSearchCrawlComponent -SearchTopology $newTopology -SearchServiceInstance $hosts
	}
	if($true -eq $comp.contains("P")) {
		Write-Output "- Processing"
		New-SPEnterpriseSearchContentProcessingComponent -SearchTopology $newTopology -SearchServiceInstance $hosts
	}
	if($true -eq $comp.contains("Y")) {
		Write-Output "- Analytics"
		New-SPEnterpriseSearchAnalyticsProcessingComponent -SearchTopology $newTopology -SearchServiceInstance $hosts
	}
	if($true -eq $comp.contains("Q")) {
		Write-Output "- Query"
		New-SPEnterpriseSearchQueryProcessingComponent -SearchTopology $newTopology -SearchServiceInstance $hosts
	}
	if($true -eq $comp.contains("I") -or $true -eq $comp.contains("0")) {
		Write-Output "- Index 0"
		New-SPEnterpriseSearchIndexComponent -SearchTopology $newTopology -SearchServiceInstance $hosts -IndexPartition 0
	}
	if($true -eq $comp.contains("1")) {
		Write-Output "- Index 1"
		New-SPEnterpriseSearchIndexComponent -SearchTopology $newTopology -SearchServiceInstance $hosts -IndexPartition 1
	}
	if($true -eq $comp.contains("2")) {
		Write-Output "- Index 2"
		New-SPEnterpriseSearchIndexComponent -SearchTopology $newTopology -SearchServiceInstance $hosts -IndexPartition 2
	}
	if($true -eq $comp.contains("3")) {
		Write-Output "- Index 3"
		New-SPEnterpriseSearchIndexComponent -SearchTopology $newTopology -SearchServiceInstance $hosts -IndexPartition 3
	}
}	
#Apply New Topology
write-output "Apply New Topology"
Set-SPEnterpriseSearchTopology -Identity $newTopology

Per eseguire lo script è sufficiente passare un parametro stringa nel formato: "<host1>\<components1>,<host2>\<components2>,<ecc...>"

PowerShell: Formato parametri

Create-SPSearchTopology.ps1 -topology "<host1>\<components1>;<host2>\<components2>,<ecc...>"
ad esempio

PowerShell: Esempio

Create-SPSearchTopology.ps1 "VM-SPFE01\QI;VM-SPAP02\ACPY"
in questo caso la configurazione risultante è questa
topology: VM-SPFE01\QI;VM-SPAP02\ACPY
topology: VM-SPFE01\QI;VM-SPAP02\ACPY
La proprietà components è una stringa che contiene i codici dei servizi da attivare sullo specifico server secondo questa convenzione:
  • 'A'=Admin
  • 'C'=Crawler
  • 'P'=Content Processing
  • 'Y'=Analytics Processing
  • 'Q'=Query Processing
  • 'I'=Index Partition 0
  • '0'=Index Partition 0
  • '1'=Index Partition 1
  • '2'=Index Partition 2
  • '3'=Index Partition 3
  • '-'=irrilevante, segnaposto solo per mantenere l'allineamento grafico nello script
La sequenza con cui vengono definiti i codici non è rilevante.
Accetta sia caratteri maiuscoli che minuscoli.
Qualsiasi altro carattere viene ignorato (non genera errori).
Tags:
SharePoint 201672 PowerShell204 Search8 SharePoint 2013141 SharePoint 201918 SharePoint502
Potrebbe interessarti anche: