Skip to content

Sample postinstall scripts

Simon Tushev edited this page Nov 26, 2020 · 2 revisions

These scripts are sample scripts that could be used for postinstall automation of various tasks.

USE THEM AT YOUR OWN RISK ⚠. They may or may not suit you. Some of them contain potentially dangerous commands, like deleting directories. Regard them as boilerplates for your own script, not as final solutions.

ℹ Improvements are welcome. Open a new issue for this.


Update junction directory to the last found AdoptOpenJDK in registry

This script allows for automatic update of junction directory. Junction directory scheme allows for automatic pickup of the recently updated versions, without OS restart to propagate changes in environment variables.

The script will give you two variables: candidate_JDK and candidate_JRE (not necessary the highest version ones, just the last encountered in the registry).

⚠ May not work correctly if you have more than one installation of the same feature version (say, 11), same image type (say, JDK), but with different versions (say, you have two installations 11.0.9+1 and 11.0.9.1+1 - in this case this very script will not suit you). Improvements are welcome. Open a new issue for this.

$feature_branch = '11'
$your_junction = 'c:\path\to\your-junction'

$adopt_registry = "Registry::HKLM\Software\AdoptOpenJDK"
# use the following line if you need 32-bit installations
# $adopt_registry = "Registry::HKLM\Software\WOW6432Node\AdoptOpenJDK"

$image_types = Get-ChildItem -Path $adopt_registry
foreach ($type in $image_types) {
    $matching_installations = Get-ChildItem -Path $type.PSPath | Where Name -Like "*\$($feature_branch)*"

    $c = ''
    foreach ($inst in $matching_installations) {
        $c = (Get-ItemProperty (Get-ChildItem -Path $inst.PSPath -Recurse | Where Name -Like '*\MSI*').PSPath).Path
    }

    New-Variable -Force -Name "candidate_$($type.PSChildName)" -Value $c
    Write-Host "The most likely $($type.PSChildName) candidate for $($feature_branch) branch is $($c)"
}
Write-Host "(not necessary the highest version ones, just the last encountered in the registry)"
Write-Host 

# uncomment these lines and change them as needed

# Remove-Item "$($your_junction)" -Force -Confirm:$False
# cmd /c mklink /j "$($your_junction)" "$($candidate_JDK)"

Write-Host "THE COMMAND LOOKS LIKE: << cmd /c mklink /j ""$($your_junction)"" ""$($candidate_JDK)"" >>, please edit the source to enable it"

PLEASE DO NOT JUST 'COPY-PASTE-RUN' THE SCRIPT, most likely you have to tweak something