-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElasticSearchConfiguration.ps1
76 lines (59 loc) · 2.38 KB
/
ElasticSearchConfiguration.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
param(
[string]$ConfigFilePath = "C:\Elk\elasticsearch\config\elasticsearch.yml",
[string]$DataPath = "C:/Elk/elasticsearch/data",
[string]$LogsPath = "C:/Elk/elasticsearch/logs",
[string]$RepoPath = "C:/Elk/elasticsearch/AllSnapshots"
)
function Update-ElasticSearchConfiguration
{
Param(
[string]$ConfigFilePath,
[string]$DataPath,
[string]$LogsPath,
[string]$RepoPath
)
function Set-ConfigurationEntry {
Param (
[string[]][ref]$FileLines,
[string]$MatchRegEx,
[string]$Value
)
$ConfigUpdate = $false
for($i = 0; $i -lt $FileLines.Count; $i++) {
$Line = $FileLines[$i]
if ($Line -match $MatchRegEx) {
Write-Host "Updating entry $Value"
$FileLines[$i] = $Value
$ConfigUpdate = $true
break
}
}
if (-not $ConfigUpdate) {
Write-Host "Appending new entry $Value"
# The array is of fixed size, so just append text to the last line
$FileLines[$FileLines.Count -1] += "`n$Value"
}
}
Write-Host $MyInvocation.MyCommand
if (-Not $ConfigFilePath -Or -Not (Test-Path $ConfigFilePath -PathType Leaf)) {
Write-Host "Failed to locate elasticsearch configuration file: $ConfigFilePath."
throw "Failed to locate elasticsearch configuration file: $ConfigFilePath."
}
[string[]]$FileLines = (Get-Content $ConfigFilePath)
$DataPathConfigEntry = "path.data: $DataPath" -replace '\\','/'
$LogsPathConfigEntry = "path.logs: $LogsPath" -replace '\\','/'
$RepoPathConfigEntry = "path.repo: $RepoPath" -replace '\\','/'
Set-ConfigurationEntry -FileLines ([ref]$FileLines) -MatchRegEx "^\s*path\.data\s*:\s*" -Value $DataPathConfigEntry
Set-ConfigurationEntry -FileLines ([ref]$FileLines) -MatchRegEx "^\s*path\.logs\s*:\s*" -Value $LogsPathConfigEntry
Set-ConfigurationEntry -FileLines ([ref]$FileLines) -MatchRegEx "^\s*path\.repo\s*:\s*" -Value $RepoPathConfigEntry
Set-Content $ConfigFilePath $FileLines
Write-Host "$($MyInvocation.MyCommand) done."
}
try {
Update-ElasticSearchConfiguration -ConfigFilePath $ConfigFilePath -DataPath $DataPath -LogsPath $LogsPath -RepoPath $RepoPath
exit 0
}
Catch {
Write-Host $_.Exception.Message
exit 1
}