Previously in the Settings JAVA_HOME with batch post, I had created a script in bash to change my JAVA_HOME
env variable. Since then, I have been trying to use more and more PowerShell so I recreated the script with PowerShell.
The script has several options that provide different functionalities:
e
orecho
: output the current value ofJAVA_HOME
.s
orset
: option to change the current value ofJAVA_HOME
. You must provide the key you want to change to.a
oradd
: add a key value pair to the store file that you can later on changed to.l
orlist
: list the current value in the file.r
orremove
: remove the key-value pair from the store file.
This is still a script that I use for my personal use so there are still things that are not robust. You can add a key with an empty value or an invalid one and the script will not stop you. I've tested every option and all the "happy paths" are working.
This is my first script in PowerShell, so there were a lot of googling and patching all around the script to make it work.
<# Name: javaenv Date: 12 September 2016 Author: Marc-Andre Girard Purpose: Modify the JAVA_HOME env variable based on Java installations on the computer. #> Param( [Parameter(Mandatory=$True)] [string]$action, [string]$key, [string]$value ) <# Show the JAVA_HOME variable to the screen #> function Output() { Get-ChildItem Env:JAVA_HOME } <# Set the JAVA_HOME variable in the Process scope to only influence the current process. #> function SetJavaHome([string]$value_to_set) { [Environment]::SetEnvironmentVariable("JAVA_HOME", $value_to_set, "Process") } <# Get a value from the version file, based on a key provided. #> function GetValueFromFile([string]$key_in_file) { $javaVersions = GetVersionsFromFile if($javaVersions.ContainsKey($key_in_file)) { $javaVersions.$key_in_file } else { Write-Host "The key does not exit in this file" exit } } <# Add a version to the version file, based on a key-value provided. #> function AddVersionToFile([string]$key_in_file, [string]$value_to_add) { $javaVersions = GetVersionsFromFile if (!$javaVersions.ContainsKey($key_in_file)) { Add-Content $dir/java_versions.store "$key_in_file=$value_to_add" } else { Write-Host "Key already exist in the file" exit } Write-Host "$key_in_file=$value_to_add" } function GetVersionsFromFile() { ConvertFrom-StringData (Get-Content $dir/java_versions.store | Out-String) } function RemoveVersionFromFile([string]$key_to_remove) { Write-Host $key_to_remove $javaVersions = GetVersionsFromFile $javaVersions.remove($key_to_remove) $content = HashConvertToString $javaVersions $content | Out-File $dir/java_versions.store } function HashConvertToString($ht) { $content_file = "" foreach($pair in $ht.GetEnumerator()) { $content_file += "{0}={1}`n" -f $($pair.key),$($pair.Value) } $content_file } Set-StrictMode -Version Latest $scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath if($action -eq "e" -or $action -eq "echo") { Output } if($action -eq "s" -or $action -eq "set") { if(![string]::IsNullOrEmpty($key)) { $value_from_file = GetValueFromFile($key) SetJavaHome($value_from_file) Output } else { Write-Host "You must provide the key to set with -key or provide the second argument" } } if($action -eq "a" -or $action -eq "add") { AddVersionToFile $key $value } if($action -eq "l" -or $action -eq "list") { GetVersionsFromFile } if($action -eq "r" -or $action -eq "remove") { RemoveVersionFromFile $key }
This is an example of what a store file should look like :
JAVA6=C:/Program Files/Java/jdk1.6.0_43 JAVA7=C:/Program Files/Java/jdk1.7.0_79 JAVA8=C:/Program Files/Java/jdk1.8.0_73