-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_config.ps1
201 lines (159 loc) · 6.34 KB
/
write_config.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function Create-Config
{
Write-Output "======================================" > $script:config_path_file
Write-Output "*** $script:program_name configuration ***" >> $script:config_path_file
Write-Output "======================================" >> $script:config_path_file
} #Create-Config
function Get-Config-Setting
{
param([string]$setting_name)
$setting_value = ""
if (-Not (Test-Path -LiteralPath $script:config_path_file))
{
Create-Config
}
else
{
# Return a collection where each object = one line of content.
$config_contents = Get-Content $script:config_path_file
ForEach ($config_line in $config_contents)
{
$first_three = $config_line.Substring(0, 3)
$is_comment = $false
if ($first_three -eq "***" -or $first_three -eq "---" -or $first_three -eq "===")
{
$is_comment = $true
}
if (-Not $is_comment)
{
$config_variable = ""
$config_value = ""
$config_delimiter = $config_line.IndexOf(":")
if ($config_delimiter -gt -1)
{
$config_variable = $config_line.Substring(0, $config_delimiter).Trim()
$config_value = $config_line.Substring($config_delimiter + 1, $config_line.Length - $config_delimiter - 1).Trim()
}
if ($config_variable -eq $setting_name)
{
#For now, this means only the final value of settings duplciated in the config file will be returned.
$setting_value = $config_value.Trim()
}
}
} #ForEach
}
$setting_value
} #Get-Config-Setting
function Initialize-Variables
{
$script:program_name = "Sample Program"
$documents_path = [Environment]::GetFolderPath("MyDocuments")
$local_path = "$documents_path\$script:program_name"
$script:input_path = "$local_path\Input"
$script:config_file = "config.txt"
$script:config_path_file = "$script:input_path\$config_file"
#Create "Libraries\Documents\$script:program_name" folder if it doesn't exist.
if (-Not (Test-Path -LiteralPath $local_path -PathType Container))
{
New-Item -ItemType Directory -Path $local_path | Out-Null
}
#Create "Libraries\Documents\$script:program_name\Input" folder if it doesn't exist.
if (-Not (Test-Path -LiteralPath $script:input_path -PathType Container))
{
New-Item -ItemType Directory -Path $script:input_path | Out-Null
}
#Create a configuration file if it doesn't exist.
if (-Not (Test-Path -LiteralPath $script:config_path_file))
{
Create-Config
}
} #Initialize-Variables
function Prompt-and-Enter-Parameter
{
Write-Host ""
Write-Host "Enter a configuration parameter name: " -NoNewline
$entered_parameter_name = Read-Host
$entered_parameter_name = $entered_parameter_name.Trim()
if ($entered_parameter_name -ne "")
{
Write-Host "Enter the parameter value: " -NoNewline
$entered_parameter_value = Read-Host
$entered_parameter_value = $entered_parameter_value.Trim()
Set-Config-Setting -setting_name $entered_parameter_name -setting_value $entered_parameter_value
}
} #Prompt-and-Enter-Parameter
function Set-Config-Setting
{
param([string]$setting_name, [string]$setting_value)
$setting_value = $setting_value.Trim()
$temp_path_file = "$script:input_path\temp config.txt"
$setting_found = $false
if (Test-Path -LiteralPath $temp_path_file)
{
Remove-Item $temp_path_file
}
if (-Not (Test-Path -LiteralPath $script:config_path_file))
{
Create-Config
}
if (Test-Path -LiteralPath $script:config_path_file)
{
# Return a collection where each object = one line of content.
$config_contents = Get-Content $script:config_path_file
ForEach ($config_line in $config_contents)
{
$new_config_line = ""
$first_three = $config_line.Substring(0, 3)
$is_comment = $false
if ($first_three -eq "***" -or $first_three -eq "---" -or $first_three -eq "===")
{
$is_comment = $true
}
if (-Not $is_comment)
{
$config_variable = ""
$config_value = ""
$config_delimiter = $config_line.IndexOf(":")
if ($config_delimiter -gt -1)
{
$config_variable = $config_line.Substring(0, $config_delimiter).Trim()
$config_value = $config_line.Substring($config_delimiter + 1, $config_line.Length - $config_delimiter - 1).Trim()
}
if ($config_variable -eq $setting_name)
{
$setting_found = $true
$new_config_line = "${config_variable}: $setting_value"
}
else
{
$new_config_line = $config_line
}
}
else
{
$new_config_line = $config_line
}
Write-Output $new_config_line >> $temp_path_file
} #ForEach
if (-Not $setting_found)
{
Write-Output "${setting_name}: $setting_value" >> $temp_path_file
}
if (Test-Path -LiteralPath $script:config_path_file)
{
Remove-Item $script:config_path_file
}
if (Test-Path -LiteralPath $temp_path_file)
{
Rename-Item $temp_path_file $script:config_path_file
}
}
} #Set-Config-Setting
Initialize-Variables
for($loopCounter = 1; $loopCounter -le 3; $loopCounter++)
{
Prompt-and-Enter-Parameter
}
$returned_value = Get-Config-Setting -setting_name "logEmailAddress"
Write-Host ""
Write-Host "logEmailAddress = $returned_value"