1
votes

My DSC script is setting JAVA_HOME via a custom module, after successful JAVA installation.

I'm then trying to retrieve the updated JAVA home using the following line in a Script resource -

SetScript = "[Environment]::SetEnvironmentVariable('JAVA_HOME',[System.Environment]::GetEnvironmentVariable('JAVA_HOME','Machine'))"

I can see the verbose log stating the resource is executed. However, after the script finishes my console is still holding on to the previous JAVA_HOME.

The issue I have with this is, my script (further down) needs to call a batch file that uses JAVA_HOME but because the session is passing the wrong value it is failing with "System can't find the path specified".

Can anyone please help with some solution to overcome this problem?

Disclaimer: I'm very very new to powershell. Please explain in detail please.

3
You say 'further down' your script fails. It would be good if you posted the complete script including the setting the environment and where you use it. With incomplete repro, I can only tell you how it works and hopefully you can figure out how to fix it.TravisEz13

3 Answers

2
votes

As a cleaner alternative, there is a DSC Resource for environment. From their code in GitHub, it appears it also does [environment]::GetEnvironmentVariable("FOOBAR", "MACHINE") part, if you want to try.

Environment [string] #ResourceName
{
    Name = [string]
    [ Ensure = [string] { Absent | Present }  ]
    [ Path = [bool] ]
    [ DependsOn = [string[]] ]
    [ Value = [string] ]
}

Reference : https://docs.microsoft.com/en-us/powershell/dsc/environmentresource

Code : @Line 647

https://github.com/PowerShell/PSDscResources/blob/2c33e69634aa9c641ab27427d457fe9f49884e7c/DscResources/MSFT_EnvironmentResource/MSFT_EnvironmentResource.psm1#L647

1
votes

The following code shows how you could do this:

configuration envtest
{
    Script s1
    {
        GetScript  = {

        }
        SetScript  = {
            $ENV:FOOBAR > c:\temp\env.txt
        }
        TestScript = {
            [environment]::SetEnvironmentVariable("FOOBAR", "hi there", "MACHINE")
            $ENV:FOOBAR = [environment]::GetEnvironmentVariable("FOOBAR", "MACHINE")
            $false
        }
    }

    Script s2
    {
        GetScript = {   }
        TestScript = { $false }
        SetScript =  {   $ENV:FOOBAR > c:\temp\env2.txt }
        DependsOn = "[script]s1"
    }
}

envtest
Start-DscConfiguration -force -Wait -Verbose -Path envtest

Note that in the DSC script itself, only script resources can take advantage of a runtime change like a new ENV variable however processes spawned by the script will pick up the new variable as long as the script resource runs first.

Also, while the ISE will tell you that the script resource requires a string, we recommend using a scriptblock instead of a string - this is simpler and leads to fewer quoting errors. If you need you script to pick up the value of a compile-time variable, use $using:myCompileTimeVariable instead of $myCompileTimeVariable and everything should just work.

0
votes

SetEvironmentVariable sets the variable for the process it's running in (by default, it can also set it for the machine, or User.) DSC scripts run as a separate process running as a system account and cannot update your console. It appears based on the script that it already updated at the machine level. To pick up the variable in your console you must do one of two things:

  • Launch a new console using the shell
  • Update the variable (As you did in the DSC code above) from the machine environment.