Skip to content

Commit 796805d

Browse files
committed
Fix #21 JEnv is now asking the user to restart with admin rights so it can change system env
1 parent 2c79036 commit 796805d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/jenv.ps1

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,26 @@ $javaPaths = (Get-Command java -All | Where-Object { $_.source -ne ((get-item $P
5151
if ($javaPaths.Length -gt 0) {
5252
Write-Host "JEnv is changing your environment variables. This process could take longer but it happens only when a java executable is found in your path"
5353
$userPath = [System.Environment]::GetEnvironmentVariable("PATH", "User").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
54+
$systemPath = [System.Environment]::GetEnvironmentVariable("PATH", "MACHINE").split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
5455
# Remove all javas from path
5556
$userPath = ($userPath | Where-Object { !$javaPaths.Contains($_ + "\java.exe") } ) -join ";"
57+
$systemPath = ($systemPath | Where-Object { !$javaPaths.Contains($_ + "\java.exe") } ) -join ";"
5658
[System.Environment]::SetEnvironmentVariable("PATH", $userPath, [System.EnvironmentVariableTarget]::User) # Set globally
57-
$userPath = ([System.Environment]::GetEnvironmentVariable("PATH", "MACHINE")) + $userPath
5859

59-
$Env:PATH = $userPath # Set for powershell users
60-
Set-Content -path "jenv.path.tmp" -value $userPath # Create temp file so no restart of the active shell is required
60+
try {
61+
[System.Environment]::SetEnvironmentVariable("PATH", $systemPath, [System.EnvironmentVariableTarget]::Machine) # Set globally
62+
}
63+
catch [System.Management.Automation.MethodInvocationException] {
64+
Write-Host JEnv wants to change your system environment vars. Therefore you need to restart it with administration rights. This should only once be required. If you dont want to, you have to call JEnv on every terminal opening to change your session vars
65+
}
66+
catch {
67+
Write-Host fuck
68+
}
69+
70+
$path = $userPath + ";" + $systemPath
71+
72+
$Env:PATH = $path # Set for powershell users
73+
Set-Content -path "jenv.path.tmp" -value $path # Create temp file so no restart of the active shell is required
6174
}
6275
#endregion
6376

0 commit comments

Comments
 (0)