Share via

Installing jav worked locally but not in target vm using provisoner block

Varma 1,560 Reputation points
2024-02-17T19:03:01.6533333+00:00

INstalling java worked locally using following powershell command Start-Process -FilePath 'msiexec.exe' -ArgumentList '/I c:\Java\jdk-21_windows-x64_bin.msi /quiet' But when I put in provisioner to install in target machine it is not working , Please assist

provisioner "powershell" {
   inline = ["New-Item -ItemType Directory -Path 'c:/Java'",
    "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12",
     "Invoke-RestMethod -uri 'https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.msi' -OutFile 'c:/Java/jdk-21_windows-x64_bin.msi'",
     "Start-Process -FilePath 'msiexec.exe' -ArgumentList '/I c:\\Java\\jdk-21_windows-x64_bin.msi /quiet'",
    "[System.Environment]::SetEnvironmentVariable('JAVA_HOME','C:/Program Files/Java/jdk-21/bin','Machine')",
     "Write-Host $Env:JAVA_HOME 'has been added to the environmental variable JAVA_HOME'" 
   ]
}
Azure Virtual Machines
Azure Virtual Machines

An Azure service that is used to provision Windows and Linux virtual machines.


Answer accepted by question author

  1. Martin Therkelsen 1,415 Reputation points MVP
    2024-02-17T20:30:34.69+00:00

    Update your code to this, and it should work.

    provisioner "powershell" {
        inline = ["New-Item -ItemType Directory -Path 'c:/Java'",
          "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12",
          "Invoke-RestMethod -uri 'https://download.oracle.com/java/21/latest/jdk-21_windows-x64_bin.msi' -OutFile 'c:/Java/jdk-21_windows-x64_bin.msi'",
          "Start-Process msiexec.exe -Wait -ArgumentList '/I c:\\Java\\jdk-21_windows-x64_bin.msi /quiet'",
          "[System.Environment]::SetEnvironmentVariable('JAVA_HOME','C:/Program Files/Java/jdk-21/bin','Machine')",
          "Write-Host $Env:JAVA_HOME 'has been added to the environmental variable JAVA_HOME'" 
        ]
      }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.