I am trying to set the environment variable in my powershell script.
Heres my code snippet.It sets the system environment variable (System Properties > Environment variable > Path) however, I am unable to start rabbitmq-service in powershell.
'$ENV:PATH' command output doesn't have the newly added path. After system restart $ENV:PATH contains the new path but the command 'rabbitmq-service' still doesnt work.
# SET Erlang and RabbitMQ Home Path
$ERLANG_HOME = "$env:PROGRAMFILES\erl9.2"
[System.Environment]::SetEnvironmentVariable("ERLANG_HOME", $ERLANG_HOME, "Machine")
$ERTS_HOME = "$env:PROGRAMFILES\erts-9.2"
[System.Environment]::SetEnvironmentVariable("ERTS_HOME", $ERTS_HOME, "Machine")
$RABBITMQ_HOME = "$env:PROGRAMFILES\RabbitMQ Server\rabbitmq_server-3.6.11"
[System.Environment]::SetEnvironmentVariable("RABBITMQ_HOME", $RABBITMQ_HOME, "Machine")
# Add Erlang and RabbitMQ to Path
$System_Path_Elems = [System.Environment]::GetEnvironmentVariable("PATH", "Machine").Split(";")
if (!$System_Path_Elems.Contains("$RABBITMQ_HOME\sbin") -and !$System_Path_Elems.Contains("$ERLANG_HOME\bin") -and !$System_Path_Elems.Contains("$ERTS_HOME\bin"))
{
$newPath = [System.String]::Join(";", $System_Path_Elems + "$ERLANG_HOME\bin" + "$ERTS_HOME\bin" + "$RABBITMQ_HOME\sbin")
[System.Environment]::SetEnvironmentVariable("PATH", $newPath, "Machine")
}
If I set the PATH using $env:PATH as below in my script, It works.
$env:Path += ";C:\\Program Files\\erl9.2\\erts-9.2\\bin;
C:\\Program Files\\RabbitMQ Server\\rabbitmq_server-3.6.11\\sbin;C:\\Program Files\\erl9.2\\bin"
I am able to execute the following commands without any issues.
rabbitmq-service remove
rabbitmq-plugins enable rabbitmq_management --offline
rabbitmq-service install
rabbitmq-service start
So, why 'SetEnvironmentVariable' doesn't work. Am I missing something here?