Copy-SolrIndex.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#Set-StrictMode -Version Latest
#####################################################
# Copy-SolrIndex
#####################################################
<#PSScriptInfo
 
.VERSION 1.6
 
.GUID 512fb058-2d8a-4e12-9fee-3f14f7d4ee46
 
.AUTHOR David Walker, Sitecore Dave, Radical Dave
 
.COMPANYNAME David Walker, Sitecore Dave, Radical Dave
 
.COPYRIGHT David Walker, Sitecore Dave, Radical Dave
 
.TAGS powershell file io solr sitecore
 
.LICENSEURI https://github.com/Radical-Dave/Copy-SolrIndex/blob/main/LICENSE
 
.PROJECTURI https://github.com/Radical-Dave/Copy-SolrIndex
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
#>


<#
 
.DESCRIPTION
 Copy Solr Index by schema [default: sitecore-Master_index]
 
.PARAMETER name
Name of new index [Default: copy_solrindex_index for testing] (if no solr path provided and not current working directory must be full path)
 
.PARAMETER schema
Name of schema to copy [default: sitecore_master_index]
 
.PARAMETER solr
Solr path - use this with PersistForCurrentUser and run from anywhere, otherwise working directory must be SOLR
 
.PARAMETER PersistForCurrentUser
Save solr path to env var for user
 
.PARAMETER Force
Force - overwrite if index already exists
 
.EXAMPLE
PS> .\Copy-SolrIndex 'new_index'
 
.EXAMPLE
PS> .\Copy-SolrIndex 'new_index' -solr 'c:\solr\8.1.1\solr\server' -PersistForCurrentUser
 
.EXAMPLE
PS> .\Copy-SolrIndex 'new_index' 'sitecore_web_index'
 
#>
 
#####################################################
# Copy-SolrIndex
#####################################################
[CmdletBinding(SupportsShouldProcess)]
Param(
    # Name of new index
    [Parameter(Mandatory = $false, position=0)] [string]$name,
    # Name of schema to copy [default: sitecore_master_index]
    [Parameter(Mandatory = $false, position=1)] [string]$schema = "sitecore_master_index",
    # Solr path - use this with PersistForCurrentUser and run from anywhere, otherwise working directory must be SOLR
    [Parameter(Mandatory = $false)] [string]$solr,
    # Save solr path to env var for user
    [Parameter(Mandatory = $false)] [switch]$PersistForCurrentUser,
    # Force - overwrite if index already exists
    [Parameter(Mandatory = $false)] [switch]$Force
)
begin {
    $ErrorActionPreference = 'Stop'
    $PSScriptName = ($MyInvocation.MyCommand.Name.Replace(".ps1",""))
    $PSCallingScript = if ($MyInvocation.PSCommandPath) { $MyInvocation.PSCommandPath | Split-Path -Parent } else { $null }
    Write-Verbose "$PSScriptName $name $schema called by:$PSCallingScript"

    if (!$solr) { #todo:some other normal/default SOLR Env path to check first?
        $solr = [Environment]::GetEnvironmentVariable("SOLR_INDEXES", "User")
        if (!$solr) {
            Write-Verbose "solr path has NOT been persisted using -PersistForCurrentUser"
            $solr = Get-Location
        } else {
            Write-Verbose "solr path was persisted using -PersistForCurrentUser!"
        }
    }

    if (!(Test-Path (Join-Path $solr $schema))) {
        Write-Error "ERROR schema not found:$schema USE -solr and -PersistForCurrentUser to set SOLR path"
        EXIT 1
    }

    if ($PersistForCurrentUser) {
        [Environment]::SetEnvironmentVariable("SOLR_INDEXES", $solr, "User")
        if (!$name) {
            Write-Output "PersistForCurrentUser - saved solr:$solr"
            Exit 0
        }
    }
    
    if (!$name) {$name = "$($PSScriptName.Replace("-","_"))_index"}
    $name = $name.ToLower()

    $dest = Join-Path $solr $name
    $path = "$solr\$schema\*"
}
process {    
    Write-Verbose "$PSScriptName $name $schema start"
    Write-Verbose "path:$path"
    Write-Verbose "dest:$dest"

    if (Test-Path $path) {
        if($PSCmdlet.ShouldProcess($path)) {

            if (Test-Path $dest) {
                if (!$Force) {
                    Write-Error "ERROR $dest already exists. Use -Force to overwrite."
                    EXIT 1
                } else {
                    Write-Verbose "$dest already exist. -Force used - removing."
                    Remove-Item $dest -Recurse -Force | Out-Null
                }
            }

            if (!(Test-Path $dest)) {
                Write-Verbose "Creating destination: $dest"
                New-Item -Path $dest -ItemType Directory | Out-Null
            }

            Write-Verbose "Copying schema: $schema"
            Copy-Item -Path $path -Destination $dest -PassThru | Out-Null

            $propPath = "$dest\core.properties"
            if (Test-Path $propPath) {
                Write-Verbose "Setting index name property: $propPath"
                (Get-Content $propPath).Replace($schema, $name) | Out-File $propPath
            } else {
                Write-Error "$propPath not found."
            }                
        }
    }
    Write-Verbose "$PSScriptName $name $schema end"
    return $dest
}