-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava_last_used.ps1
47 lines (36 loc) · 1.71 KB
/
java_last_used.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
$javaExecutables = @("JAVAWS.EXE", "JAVA.EXE", "JAVAW.EXE")
# reg path for tracking
$registryPath = "HKLM:\SOFTWARE\JavaTracking"
# check the registry path exists
if (-not (Test-Path $registryPath)) {
New-Item -Path $registryPath -Force
}
# start a variable to store the most recent usage
$mostRecentUse = $null
$mostRecentExecutable = $null
foreach ($javaExecutable in $javaExecutables) {
# define path to prefetch default location
$prefetchFilePath = "C:\Windows\Prefetch\$javaExecutable-*.pf"
# find file
$prefetchFile = Get-ChildItem -Path $prefetchFilePath -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if ($prefetchFile -and ($null -eq $mostRecentUse -or $prefetchFile.LastWriteTime -gt $mostRecentUse)) {
$mostRecentUse = $prefetchFile.LastWriteTime
$mostRecentExecutable = $javaExecutable
}
}
if ($mostRecentUse) {
Write-Output "Most recently used Java executable: $mostRecentExecutable with last use on $mostRecentUse"
Set-ItemProperty -Path $registryPath -Name "LastUsed" -Value $mostRecentUse.ToString()
$currentDate = Get-Date
$timeSpan = New-TimeSpan -Start $mostRecentUse -End $currentDate
# check most recents java executable to see if it was used in last 30 days
if ($timeSpan.Days -le 30) {
Set-ItemProperty -Path $registryPath -Name "UsedInLast30Days" -Value "Yes"
} else {
Set-ItemProperty -Path $registryPath -Name "UsedInLast30Days" -Value "No"
}
} else {
Write-Output "No Prefetch files found for any Java executable."
Set-ItemProperty -Path $registryPath -Name "LastUsed" -Value $null
Set-ItemProperty -Path $registryPath -Name "UsedInLast30Days" -Value "No"
}