Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Last active August 4, 2021 00:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mavaddat/a16d0afa0c567a3215cfbbee5e8e0aba to your computer and use it in GitHub Desktop.
Save mavaddat/a16d0afa0c567a3215cfbbee5e8e0aba to your computer and use it in GitHub Desktop.
Automated update of JDK path for AdoptOpenJDK triggered by update in Chocolatey. PowerShell directives are provided to the VBS Windows Script Host as encoded commands so as to avoid a shell screen popping up on trigger.
# This contains two lines (#4, #7) which will be run as an encoded PowerShell command inside a Visual Basic Script (VBScript) scheduled task below.
# Check if 'updateJDK.vbs' is already running in another process; if so, let's quit and just let that finish the task
Get-Process -Name '*WScript*' | foreach{if($_.CommandLine -imatch 'updateJDK.vbs' -and $_.Id -ne $PID){ exit } }
# We will update the %JAVA_HOME% path upon observing discrepency between the newest available JDK (local directory) and the %JAVA_HOME%
$jdk = Get-ChildItem (Resolve-Path 'C:\Program Files\AdoptOpenJDK\') -Depth 0 -Directory | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1; if(($null -ne ([System.Environment]::GetEnvironmentVariable('JAVA_HOME'))) -and ($env:JAVA_HOME -ne $jdk)) { [System.Environment]::SetEnvironmentVariable('JAVA_HOME',$($jdk.FullName),[System.EnvironmentVariableTarget]::User) }
# Whenever we change the script (i.e., the above line of PowerShell commands), we generate the new encoded command by copying line 4 then running
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes( $(Get-Clipboard -Raw))) | Set-Clipboard
# Paste the new encoded command into the updateJDK.vbs after '-encodedCommand'
# Then we digitally sign the VBS using the following command:
$VBSpath = Resolve-Path "${env:OneDriveConsumer}\updateJDK.vbs"; $cert = (Get-Item -Path 'Cert:\CurrentUser\My\*' | Where-Object -FilterScript {$_.Thumbprint -eq '1C3263285FB617CBC20B9A8E31A3A29448167AFD'}); Set-AuthenticodeSignature -Certificate $cert -FilePath "$VBSpath"
# For instructions on how to generate a self-signed code-signing certificate in PowerShell (for the last step above), see: http://woshub.com/how-to-create-self-signed-certificate-with-powershell/
# The final step is to import the task into Windows task scheduler.
command = "powershell.exe -NoProfile -NonInteractive -windowstyle Hidden -nologo -encodedCommand ""RwBlAHQALQBQAHIAbwBjAGUAcwBzACAALQBOAGEAbQBlACAAJwAqAFcAUwBjAHIAaQBwAHQAKgAnACAAfAAgAEYAbwByAEUAYQBjAGgALQBPAGIAagBlAGMAdAAgAHsAIABpAGYAIAAoACQAXwAuAEMAbwBtAG0AYQBuAGQATABpAG4AZQAgAC0AaQBtAGEAdABjAGgAIAAnAHUAcABkAGEAdABlAEoARABLAC4AdgBiAHMAJwAgAC0AYQBuAGQAIAAkAF8ALgBJAGQAIAAtAG4AZQAgACQAUABJAEQAKQAgAHsAIABlAHgAaQB0ACAAfQAgAH0AOwAgACQAagBkAGsAIAA9ACAARwBlAHQALQBDAGgAaQBsAGQASQB0AGUAbQAgACgAUgBlAHMAbwBsAHYAZQAtAFAAYQB0AGgAIAAnAEMAOgBcAFAAcgBvAGcAcgBhAG0AIABGAGkAbABlAHMAXABBAGQAbwBwAHQATwBwAGUAbgBKAEQASwBcACcAKQAgAC0ARABlAHAAdABoACAAMAAgAC0ARABpAHIAZQBjAHQAbwByAHkAIAB8ACAAUwBvAHIAdAAtAE8AYgBqAGUAYwB0ACAALQBQAHIAbwBwAGUAcgB0AHkAIABMAGEAcwB0AFcAcgBpAHQAZQBUAGkAbQBlACAALQBEAGUAcwBjAGUAbgBkAGkAbgBnACAAfAAgAFMAZQBsAGUAYwB0AC0ATwBiAGoAZQBjAHQAIAAtAEYAaQByAHMAdAAgADEAOwAgAGkAZgAgACgAKAAkAG4AdQBsAGwAIAAtAG4AZQAgACgAWwBTAHkAcwB0AGUAbQAuAEUAbgB2AGkAcgBvAG4AbQBlAG4AdABdADoAOgBHAGUAdABFAG4AdgBpAHIAbwBuAG0AZQBuAHQAVgBhAHIAaQBhAGIAbABlACgAJwBKAEEAVgBBAF8ASABPAE0ARQAnACkAKQApACAALQBhAG4AZAAgACgAJABlAG4AdgA6AEoAQQBWAEEAXwBIAE8ATQBFACAALQBuAGUAIAAkAGoAZABrACkAKQAgAHsAIABbAFMAeQBzAHQAZQBtAC4ARQBuAHYAaQByAG8AbgBtAGUAbgB0AF0AOgA6AFMAZQB0AEUAbgB2AGkAcgBvAG4AbQBlAG4AdABWAGEAcgBpAGEAYgBsAGUAKAAnAEoAQQBWAEEAXwBIAE8ATQBFACcALAAgACQAKAAkAGoAZABrAC4ARgB1AGwAbABOAGEAbQBlACkALAAgAFsAUwB5AHMAdABlAG0ALgBFAG4AdgBpAHIAbwBuAG0AZQBuAHQAVgBhAHIAaQBhAGIAbABlAFQAYQByAGcAZQB0AF0AOgA6AFUAcwBlAHIAKQAgAH0A"""
' Get-Process -Name '*WScript*' | foreach{if($_.CommandLine -imatch 'updateJDK.vbs' -and $_.Id -ne $PID){ exit } }
' $jdk = Get-ChildItem (Resolve-Path 'C:\Program Files\AdoptOpenJDK\') -Depth 0 -Directory | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1; if(($null -ne ([System.Environment]::GetEnvironmentVariable('JAVA_HOME'))) -and ($env:JAVA_HOME -ne $jdk)) { [System.Environment]::SetEnvironmentVariable('JAVA_HOME',$($jdk.FullName),[System.EnvironmentVariableTarget]::User) }
' [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes( $(Get-Clipboard -Raw))) | Set-Clipboard
' $VBSpath = Resolve-Path "${env:OneDriveConsumer}\updateJDK.vbs"; $cert = (Get-Item -Path 'Cert:\CurrentUser\My\*' | Where-Object -FilterScript {$_.Thumbprint -eq '1C3263285FB617CBC20B9A8E31A3A29448167AFD'}); Set-AuthenticodeSignature -Certificate $cert -FilePath "$VBSpath"
'
set shell = CreateObject("WScript.Shell")
shell.Run command,0
'' SIG '' Begin signature block
'' SIG '' MIIFaAYJKoZIhvcNAQcCoIIFWTCCBVUCAQExCzAJBgUr
'' SIG '' [SNIP ✂]
'' SIG '' ynKqgmULvv+e7ZZqKBWbRyerlXeQtzODSbIf3v+0dRkAfQc=
'' SIG '' End signature block
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2020-03-02T21:10:25.7270549</Date>
<Author>AzureAD\MavaddatJavid</Author>
<Description>Update the JDK to the latest AdoptJDK whenever updated by Chocolatey</Description>
<URI>\Update JDK Path</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Microsoft-Windows-PowerShell/Admin"&gt;&lt;Select Path="Microsoft-Windows-PowerShell/Admin"&gt;*[System[(Level=4 or Level=0) and (EventID=400 or EventID=403 or EventID=600)]]&lt;/Select&gt;&lt;Select Path="Microsoft-Windows-PowerShell/Operational"&gt;*[System[(Level=4 or Level=0) and (EventID=400 or EventID=403 or EventID=600)]]&lt;/Select&gt;&lt;Select Path="PowerShellCore/Operational"&gt;*[System[(Level=4 or Level=0) and (EventID=400 or EventID=403 or EventID=600)]]&lt;/Select&gt;&lt;Select Path="Windows PowerShell"&gt;*[System[(Level=4 or Level=0) and (EventID=400 or EventID=403 or EventID=600)]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Windows PowerShell"&gt;&lt;Select Path="Windows PowerShell"&gt;*[System[Provider[@Name='PowerShell'] and EventID=400]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Windows PowerShell"&gt;&lt;Select Path="Windows PowerShell"&gt;*[System[Provider[@Name='PowerShell'] and EventID=403]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Windows PowerShell"&gt;&lt;Select Path="Windows PowerShell"&gt;*[System[Provider[@Name='PowerShell'] and EventID=600]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>true</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT1H</Interval>
<Count>3</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
<Command>%OneDriveConsumer%\updateJDK.vbs</Command>
</Exec>
</Actions>
</Task>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment