Features: 1) Add collection of environment variables into a hashtable for improved handling; 2) Format and display all loaded environment variables as a single string output after processing;
Fixes: 1) Suppress unnecessary output from New-Item calls to improve script clarity; Extra: 1) Minor refactor to ensure sorted and formatted output of environment variables; 2) Add consistent structure and efficiency improvements to the script;
This commit is contained in:
parent
3ce54ef027
commit
f18c456419
1 changed files with 11 additions and 5 deletions
|
|
@ -10,13 +10,14 @@ if (-not (Test-Path $envFile))
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$envVars = @{ }
|
||||||
|
|
||||||
Get-Content $envFile | ForEach-Object {
|
Get-Content $envFile | ForEach-Object {
|
||||||
$line = $_.Trim()
|
$line = $_.Trim()
|
||||||
if ($line -eq '' -or $line.StartsWith('#'))
|
if ($line -eq '' -or $line.StartsWith('#'))
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($line -notmatch '=')
|
if ($line -notmatch '=')
|
||||||
{
|
{
|
||||||
return
|
return
|
||||||
|
|
@ -37,9 +38,14 @@ Get-Content $envFile | ForEach-Object {
|
||||||
$value = $value.Substring(1, $value.Length - 2)
|
$value = $value.Substring(1, $value.Length - 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
New-Item -Path Env:\$key -Value $value
|
New-Item -Path Env:\$key -Value $value | Out-Null
|
||||||
|
$envVars[$key] = $value
|
||||||
Write-Host "Set environment variable: $key"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Host "All variables loaded from $envFile."
|
if ($envVars.Count -gt 0)
|
||||||
|
{
|
||||||
|
$formatted = ($envVars.GetEnumerator() | Sort-Object Name | ForEach-Object {
|
||||||
|
"$( $_.Key )=$( $_.Value )"
|
||||||
|
}) -join ';'
|
||||||
|
Write-Host $formatted
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue