sitecore-csv.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<#
.Synopsis
 Processing Config Enable-Disable Sitecore_8.2-160906_RTM.csv file
 
.DESCRIPTION
 This function is processing csv file, that contains guideline about enable/disable configuration
 files on each enviroment.
 
#>

function Set-SitecoreEnvironment {
[CmdletBinding(SupportsShouldProcess=$true)]
    param (
[parameter(Mandatory=$true)]
        [string]$CsvPath,

        [parameter(Mandatory=$true)]
        [string]$WebPath,
        
        [parameter()]
        [ValidateSet('ContentDelivery','ContentManagement','Processing','CMProcessing','Reporting')]
        [string]$Environment = $null,

        [parameter()]
        [ValidateSet('Solr','Lucene')]
        [string]$Search = $null,

[switch]$Backup

    )

$projectFolder = Split-Path -Parent $MyInvocation.MyCommand.Path

Write-Verbose $projectFolder

if($Backup -eq $true)
{
$sourceFolder = Join-Path -Path $WebPath -ChildPath "website\App_Config"
# More about date formating - https://technet.microsoft.com/en-us/library/ee692801.aspx
$date = Get-Date -Format d-M-yyyy-HH-MM-s
$backupFile = "website\App_Config-$date.zip"
$backupPath = Join-Path -Path $WebPath -ChildPath $backupFile
Zip-Directory -DestinationFileName $backupPath -SourceDirectory $sourceFolder
}

if( ($Search -ne '') -and ($Environment -ne ''))
{
Write-Verbose "Configure $Search for environment: $Environment"
# Prefilter all configuration files to select only files with values 'Solr is used' or 'Lucene is used'
$searchProviderPreFilter = {$_.SearchProviderUsed -ne '' -and $_.SearchProviderUsed -ne 'Base' }

# Set field 'Enable' to true if
# configuration file is used by selected search engine
# and configuration file should be enabled on selected environment
$action = @{Name="Enable"; Expression = { ($_.SearchProviderUsed -eq "$Search is used" -and $_.$Environment -eq 'Enable')}}

# Build full path to configuration file
$file = @{Name="ConfigPath";Expression = {  Combine-Path $WebPath $_.FilePath $_.ConfigFileName}}

Import-Csv $CsvPath | Where-Object $searchProviderPreFilter  | Select-Object  $file, $action, @{Name="ProcessExample"; Expression={$true}} | Switch-SitecoreConfigFile 
}
elseif( $Environment -ne '' -and $Search -eq '')
{
Write-Verbose "Configure environment: $Environment"
# Prefilter all configuration files to select all files not connected with any search provider
$searchProviderPreFilter = {$_.SearchProviderUsed -eq '' -or $_.SearchProviderUsed -eq 'Base' }

# Set field 'Enable' to true if
# and configuration file should be enabled on selected environment
$action = @{Name="Enable"; Expression = { ( $_.$Environment -eq 'Enable')}}

# Build full path to configuration file
$file = @{Name="ConfigPath";Expression = {  Combine-Path $WebPath $_.FilePath $_.ConfigFileName}}

Import-Csv $CsvPath  | Where-Object $searchProviderPreFilter | Select-Object  $file, $action, @{Name="ProcessExample"; Expression={$false}} | Switch-SitecoreConfigFile
}

}

<#
.Synopsis
 
 Enable or disable Sitecore configuration file
 
.DESCRIPTION
 
 This function is used to enable/disable configuration file. This functionality is realized by change file name extension.
 Enable configuration file means - remove extension '.disabled'
 Disable configuration file means - add extension '.disabled'
 
 This function process objects passed by pipeline. Object should have two properties ConfigPath and Enable.
 ConfigPath - this is path to configration file
 Enable - if set to true file is enabled, otherwise disabled
 
.EXAMPLE
  
#>

function Switch-SitecoreConfigFile
{
    process
    {
$VerbosePreference = "Continue"
        $configFile = $_.ConfigPath;
       

Write-Verbose "Processing $configFile..."

if( -not (Test-Path -Path $configFile) )
{
Write-Warning "File not exist"
return;
        }


$extension = [System.IO.Path]::GetExtension($configFile)
$processExample = $_.ProcessExample

# extension .example should be processed only when search configuration is processed
    if($processExample -eq $true -and $_.Enable -eq $true -and  $extension -eq '.example')
        {
            $newConfigFile = $configFile -replace "\.example","" 
              
            Write-Verbose "Enable example file $newConfigFile"

            Rename-Item $configFile -NewName $newConfigFile
        }
        elseif($_.Enable -eq $true -and  $extension -eq '.disabled')
        {
            $newConfigFile = $configFile -replace ".disabled","" 
              
            Write-Verbose "Enable file $newConfigFile"

            Rename-Item $configFile -NewName $newConfigFile
        }
        elseif($_.Enable -eq $false -and $extension -eq '.config' )
        {
            $newConfigFile = $configFile -replace "\.config",".config.disabled"

            Write-Verbose "Disable file $newConfigFile"

            Rename-Item $configFile -NewName $newConfigFile
        }
        else
        {
            Write-Warning "Change is not necessary"
        }
}
}

<#
 
 .EXAMPLE
 Combine-Path 'a'
  
 .EXAMPLE
 Combine-Path 'a' 'b'
 
 .EXAMPLE
 Combine-Path 'a' 'b' 'c'
#>

function Combine-Path
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    param (
        [parameter(Mandatory=$true)]
        [string]$Path1,
        
        [parameter(Mandatory=$true)]
        [string]$Path2,

        [parameter()]
        [string]$Path3 
    )

return Join-Path $Path1 -ChildPath (Join-Path $Path2 -ChildPath $Path3)  
}


Export-ModuleMember -Function Set-SitecoreEnvironment