|
| 1 | +function Create-Config |
| 2 | + { |
| 3 | + Write-Output "======================================" > $script:config_path_file |
| 4 | + Write-Output "*** $script:program_name configuration ***" >> $script:config_path_file |
| 5 | + Write-Output "======================================" >> $script:config_path_file |
| 6 | + } #Create-Config |
| 7 | + |
| 8 | + |
| 9 | +function Get-Config-Setting |
| 10 | + { |
| 11 | + param([string]$setting_name) |
| 12 | + |
| 13 | + $setting_value = "" |
| 14 | + |
| 15 | + if (-Not (Test-Path -LiteralPath $script:config_path_file)) |
| 16 | + { |
| 17 | + Create-Config |
| 18 | + } |
| 19 | + else |
| 20 | + { |
| 21 | + # Return a collection where each object = one line of content. |
| 22 | + $config_contents = Get-Content $script:config_path_file |
| 23 | + |
| 24 | + ForEach ($config_line in $config_contents) |
| 25 | + { |
| 26 | + $first_three = $config_line.Substring(0, 3) |
| 27 | + $is_comment = $false |
| 28 | + |
| 29 | + if ($first_three -eq "***" -or $first_three -eq "---" -or $first_three -eq "===") |
| 30 | + { |
| 31 | + $is_comment = $true |
| 32 | + } |
| 33 | + |
| 34 | + if (-Not $is_comment) |
| 35 | + { |
| 36 | + $config_variable = "" |
| 37 | + $config_value = "" |
| 38 | + $config_delimiter = $config_line.IndexOf(":") |
| 39 | + |
| 40 | + if ($config_delimiter -gt -1) |
| 41 | + { |
| 42 | + $config_variable = $config_line.Substring(0, $config_delimiter).Trim() |
| 43 | + $config_value = $config_line.Substring($config_delimiter + 1, $config_line.Length - $config_delimiter - 1).Trim() |
| 44 | + } |
| 45 | + |
| 46 | + if ($config_variable -eq $setting_name) |
| 47 | + { |
| 48 | + #For now, this means only the final value of settings duplciated in the config file will be returned. |
| 49 | + $setting_value = $config_value.Trim() |
| 50 | + } |
| 51 | + } |
| 52 | + } #ForEach |
| 53 | + } |
| 54 | + $setting_value |
| 55 | + } #Get-Config-Setting |
| 56 | + |
| 57 | + |
| 58 | +function Initialize-Variables |
| 59 | + { |
| 60 | + $script:program_name = "Sample Program" |
| 61 | + |
| 62 | + $documents_path = [Environment]::GetFolderPath("MyDocuments") |
| 63 | + $local_path = "$documents_path\$script:program_name" |
| 64 | + |
| 65 | + $script:input_path = "$local_path\Input" |
| 66 | + |
| 67 | + $script:config_file = "config.txt" |
| 68 | + $script:config_path_file = "$script:input_path\$config_file" |
| 69 | + |
| 70 | + #Create "Libraries\Documents\$script:program_name" folder if it doesn't exist. |
| 71 | + if (-Not (Test-Path -LiteralPath $local_path -PathType Container)) |
| 72 | + { |
| 73 | + New-Item -ItemType Directory -Path $local_path | Out-Null |
| 74 | + } |
| 75 | + |
| 76 | + #Create "Libraries\Documents\$script:program_name\Input" folder if it doesn't exist. |
| 77 | + if (-Not (Test-Path -LiteralPath $script:input_path -PathType Container)) |
| 78 | + { |
| 79 | + New-Item -ItemType Directory -Path $script:input_path | Out-Null |
| 80 | + } |
| 81 | + |
| 82 | + #Create a configuration file if it doesn't exist. |
| 83 | + if (-Not (Test-Path -LiteralPath $script:config_path_file)) |
| 84 | + { |
| 85 | + Create-Config |
| 86 | + } |
| 87 | + } #Initialize-Variables |
| 88 | + |
| 89 | + |
| 90 | +function Prompt-and-Enter-Parameter |
| 91 | + { |
| 92 | + Write-Host "" |
| 93 | + Write-Host "Enter a configuration parameter name: " -NoNewline |
| 94 | + |
| 95 | + $entered_parameter_name = Read-Host |
| 96 | + $entered_parameter_name = $entered_parameter_name.Trim() |
| 97 | + |
| 98 | + if ($entered_parameter_name -ne "") |
| 99 | + { |
| 100 | + Write-Host "Enter the parameter value: " -NoNewline |
| 101 | + $entered_parameter_value = Read-Host |
| 102 | + $entered_parameter_value = $entered_parameter_value.Trim() |
| 103 | + |
| 104 | + Set-Config-Setting -setting_name $entered_parameter_name -setting_value $entered_parameter_value |
| 105 | + } |
| 106 | + } #Prompt-and-Enter-Parameter |
| 107 | + |
| 108 | + |
| 109 | +function Set-Config-Setting |
| 110 | + { |
| 111 | + param([string]$setting_name, [string]$setting_value) |
| 112 | + |
| 113 | + $setting_value = $setting_value.Trim() |
| 114 | + $temp_path_file = "$script:input_path\temp config.txt" |
| 115 | + $setting_found = $false |
| 116 | + |
| 117 | + if (Test-Path -LiteralPath $temp_path_file) |
| 118 | + { |
| 119 | + Remove-Item $temp_path_file |
| 120 | + } |
| 121 | + |
| 122 | + if (-Not (Test-Path -LiteralPath $script:config_path_file)) |
| 123 | + { |
| 124 | + Create-Config |
| 125 | + } |
| 126 | + |
| 127 | + if (Test-Path -LiteralPath $script:config_path_file) |
| 128 | + { |
| 129 | + # Return a collection where each object = one line of content. |
| 130 | + $config_contents = Get-Content $script:config_path_file |
| 131 | + |
| 132 | + ForEach ($config_line in $config_contents) |
| 133 | + { |
| 134 | + $new_config_line = "" |
| 135 | + |
| 136 | + $first_three = $config_line.Substring(0, 3) |
| 137 | + $is_comment = $false |
| 138 | + |
| 139 | + if ($first_three -eq "***" -or $first_three -eq "---" -or $first_three -eq "===") |
| 140 | + { |
| 141 | + $is_comment = $true |
| 142 | + } |
| 143 | + |
| 144 | + if (-Not $is_comment) |
| 145 | + { |
| 146 | + $config_variable = "" |
| 147 | + $config_value = "" |
| 148 | + $config_delimiter = $config_line.IndexOf(":") |
| 149 | + |
| 150 | + if ($config_delimiter -gt -1) |
| 151 | + { |
| 152 | + $config_variable = $config_line.Substring(0, $config_delimiter).Trim() |
| 153 | + $config_value = $config_line.Substring($config_delimiter + 1, $config_line.Length - $config_delimiter - 1).Trim() |
| 154 | + } |
| 155 | + |
| 156 | + if ($config_variable -eq $setting_name) |
| 157 | + { |
| 158 | + $setting_found = $true |
| 159 | + $new_config_line = "${config_variable}: $setting_value" |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + $new_config_line = $config_line |
| 164 | + } |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + $new_config_line = $config_line |
| 169 | + } |
| 170 | + |
| 171 | + Write-Output $new_config_line >> $temp_path_file |
| 172 | + } #ForEach |
| 173 | + |
| 174 | + if (-Not $setting_found) |
| 175 | + { |
| 176 | + Write-Output "${setting_name}: $setting_value" >> $temp_path_file |
| 177 | + } |
| 178 | + |
| 179 | + if (Test-Path -LiteralPath $script:config_path_file) |
| 180 | + { |
| 181 | + Remove-Item $script:config_path_file |
| 182 | + } |
| 183 | + |
| 184 | + if (Test-Path -LiteralPath $temp_path_file) |
| 185 | + { |
| 186 | + Rename-Item $temp_path_file $script:config_path_file |
| 187 | + } |
| 188 | + } |
| 189 | + } #Set-Config-Setting |
| 190 | + |
| 191 | + |
| 192 | +Initialize-Variables |
| 193 | + |
| 194 | +for($loopCounter = 1; $loopCounter -le 3; $loopCounter++) |
| 195 | + { |
| 196 | + Prompt-and-Enter-Parameter |
| 197 | + } |
| 198 | + |
| 199 | +$returned_value = Get-Config-Setting -setting_name "logEmailAddress" |
| 200 | +Write-Host "" |
| 201 | +Write-Host "logEmailAddress = $returned_value" |
0 commit comments