-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetEnvVars.ps1
40 lines (32 loc) · 1.09 KB
/
SetEnvVars.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
function Set-Var{
param([string]$name, [string]$value)
# Can't have environment variable names with spaces
if ($name.Contains(" ")){
"Unable to set `"$name`" because environment variable names can't contain spaces"
return
}
# Set variable
try{
[Environment]::SetEnvironmentVariable($name, $value)
"Successfully set `"$name`" to `"$value`""
}
catch{
"Failed to set `"$name`" to `"$value`""
}
}
$currentLocation = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Definition)
$filePath = [System.IO.Path]::Combine($currentLocation, ".env")
$fileLines = [System.IO.File]::ReadAllLines($filePath)
foreach($line in $fileLines){
# Ignore all comments and lines that don't contain an equals sign
if ($line.StartsWith("#") -or !$line.Contains("=")){
continue
}
# Break apart line data
$lineData = $line.Split("=")
# Remove leading and trailing quotes and spaces
$name = $lineData[0].Trim("`"", " ")
$value = $lineData[1].Trim("`"", " ")
# Set environment variable
Set-Var $name $value
}