-
Notifications
You must be signed in to change notification settings - Fork 1
/
Populate-LocalVars.ps1
60 lines (53 loc) · 2.34 KB
/
Populate-LocalVars.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
# PowerShell script to set user-level environment variables with predefined values for Linux
# Define a hashtable with key-value pairs for environment variables
$predefinedVariableValues = @{
"WORKING_DIRECTORY" = "example_working_directory"
"RUN_PACKER_BUILD" = "true"
"RUN_PACKER_VALIDATE" = "true"
"RUN_PACKER_INIT" = "true"
"ENABLE_DEBUG_MODE" = "true"
"PACKER_VERSION" = "latest"
"PKR_VAR_registry_username" = "example"
"PKR_VAR_registry_password" = "example"
}
# Set each predefined variable
foreach ($varName in $predefinedVariableValues.Keys) {
$value = $predefinedVariableValues[$varName]
if ($isLinux) {
# Ensure the PowerShell profile directory exists
$profileDirectory = [System.IO.Path]::GetDirectoryName($PROFILE)
if (-not (Test-Path $profileDirectory)) {
New-Item -ItemType Directory -Path $profileDirectory -Force
}
# Ensure the PowerShell profile file exists
if (-not (Test-Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
# Append to PowerShell profile and set in current session
$exportCommand = "`n`$Env:$varName = `"$value`""
Add-Content -Path $PROFILE -Value $exportCommand
Set-Variable -Name $varName -Value $value -Scope Global
}
elseif ($IsMacOS)
{
# Ensure the PowerShell profile directory exists
$profileDirectory = [System.IO.Path]::GetDirectoryName($PROFILE)
if (-not (Test-Path $profileDirectory)) {
New-Item -ItemType Directory -Path $profileDirectory -Force
}
# Ensure the PowerShell profile file exists
if (-not (Test-Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
# Append to PowerShell profile and set in current session
$exportCommand = "`n`$Env:$varName = `"$value`""
Add-Content -Path $PROFILE -Value $exportCommand
Set-Variable -Name $varName -Value $value -Scope Global
}
else {
# On other systems, set user environment variable
[System.Environment]::SetEnvironmentVariable($varName, $value, [System.EnvironmentVariableTarget]::User)
}
}
Write-Host "User-level environment variables have been set."
Write-Host "Please close your powershell window and reopen to refresh environment" -ForegroundColor Yellow