-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProactiveRemediation_Detection.ps1
More file actions
241 lines (210 loc) · 9.81 KB
/
Copy pathProactiveRemediation_Detection.ps1
File metadata and controls
241 lines (210 loc) · 9.81 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<#
DOCFA_Java_Detection.ps1
Detection script per Intune Proactive Remediations
Obiettivo:
- Verificare se le chiavi di registro "fake Java 1.5" per DOCFA sono presenti
e puntano a un file jvm.dll esistente.
Exit code:
- 0 = Configurazione OK, remediation NON necessaria
- 1 = Configurazione mancante o errata, remediation necessaria
#>
# ==========================
# CONFIGURAZIONE LOG
# ==========================
$LogRoot = "C:\ProgramData\IntuneLogs\DOCFA_Java"
$MaxLogSizeBytes = 1MB # ~1.048.576 byte
$RetentionDays = 30
# Variabile globale per il file di log corrente
$script:LogFile = $null
function Initialize-Log {
try {
if (-not (Test-Path -LiteralPath $LogRoot)) {
New-Item -Path $LogRoot -ItemType Directory -Force | Out-Null
}
# Pulizia log più vecchi di $RetentionDays
$limitDate = (Get-Date).AddDays(-1 * $RetentionDays)
Get-ChildItem -Path $LogRoot -Filter "DOCFA_Java_PR_*.log" -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt $limitDate } |
Remove-Item -Force -ErrorAction SilentlyContinue
# File log giornaliero
$logName = "DOCFA_Java_PR_{0:yyyyMMdd}.log" -f (Get-Date)
$script:LogFile = Join-Path $LogRoot $logName
}
catch {
# In caso di errore nella gestione log, almeno scriviamo su output
Write-Output "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] [WARN] Impossibile inizializzare la gestione log: $($_.Exception.Message)"
}
}
function Write-Log {
param(
[Parameter(Mandatory = $true)][ValidateSet("INFO","WARN","ERROR")] [string] $Level,
[Parameter(Mandatory = $true)][string] $Message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$line = "$timestamp [$Level] $Message"
# Log visibile in Intune
Write-Host $line
try {
if (-not $script:LogFile) {
Initialize-Log
}
if (-not (Test-Path -LiteralPath $script:LogFile)) {
New-Item -Path $script:LogFile -ItemType File -Force | Out-Null
}
else {
$size = (Get-Item -LiteralPath $script:LogFile).Length
if ($size -gt $MaxLogSizeBytes) {
$resetLine = "$timestamp [INFO] Log file ha superato 1 MB. Viene ricreato."
Set-Content -Path $script:LogFile -Value $resetLine
}
}
Add-Content -Path $script:LogFile -Value $line
}
catch {
# Se la scrittura su file fallisce, ignoriamo ma almeno l'output Intune è già stato scritto
}
}
# ==========================
# LOG INIZIALE
# ==========================
Write-Log -Level INFO -Message "==== Avvio DOCFA_Java Detection ===="
Write-Log -Level INFO -Message "Computer: $env:COMPUTERNAME, Utente: $env:USERNAME"
# ==========================
# LOGICA DI DETECTION
# ==========================
# Encapsulate detection in a function so it can be unit-tested.
function Get-DOCFAJavaDetectionStatus {
param(
[string] $RegKey = "HKLM:\\SOFTWARE\\WOW6432Node\\JavaSoft\\Java Runtime Environment\\1.5"
)
try {
$regMissing = $false
$runtimeLib = $null
if (-not (Test-Path -LiteralPath $RegKey)) {
Write-Log -Level WARN -Message "Chiave di registro non trovata: $RegKey"
$regMissing = $true
}
else {
$props = Get-ItemProperty -Path $RegKey -ErrorAction Stop
$runtimeLib = $props.RuntimeLib
if ([string]::IsNullOrWhiteSpace($runtimeLib)) {
Write-Log -Level WARN -Message "Valore RuntimeLib mancante sotto $RegKey. Provo a rilevare jre/jvm dal filesystem."
$runtimeLib = $null
}
}
# Normalizza eventuali virgolette (se presente)
if ($runtimeLib) { $runtimeLib = $runtimeLib.Trim('"') }
# Se il valore punta direttamente al file jvm.dll
if ($runtimeLib -and (Test-Path -LiteralPath $runtimeLib -PathType Leaf)) {
Write-Log -Level INFO -Message "RuntimeLib valido (file): $runtimeLib"
Write-Log -Level INFO -Message "Detection: configurazione OK, nessuna remediation necessaria."
return 0
}
# Se il valore punta a una directory (es. la root della JRE), proviamo i possibili percorsi
$candidates = @()
if ($runtimeLib -and (Test-Path -LiteralPath $runtimeLib -PathType Container)) {
$candidates += Join-Path $runtimeLib "bin\client\jvm.dll"
$candidates += Join-Path $runtimeLib "bin\server\jvm.dll"
$candidates += Join-Path $runtimeLib "jre\bin\client\jvm.dll"
$candidates += Join-Path $runtimeLib "jre\bin\server\jvm.dll"
}
# Se RuntimeLib non è un path esistente, potremmo comunque provare a interpretarlo come una directory padre
else {
$parent = $null
if ($runtimeLib) { $parent = Split-Path -Path $runtimeLib -Parent }
if ($parent) {
$candidates += Join-Path $parent "bin\client\jvm.dll"
$candidates += Join-Path $parent "bin\server\jvm.dll"
}
}
foreach ($cand in $candidates) {
if (Test-Path -LiteralPath $cand -PathType Leaf) {
$pf = $env:ProgramFiles
$pf86 = ${env:ProgramFiles(x86)}
if ($pf86 -and ($cand -like "$pf86*")) {
Write-Log -Level INFO -Message "Trovato jvm.dll in: $cand"
Write-Log -Level INFO -Message "Detection: configurazione OK, nessuna remediation necessaria."
return 0
}
elseif ($pf -and ($cand -like "$pf*")) {
Write-Log -Level WARN -Message "Rilevato jvm.dll in percorso 64-bit: $cand"
Write-Log -Level WARN -Message "È necessario installare una versione di Java x86."
return 1
}
else {
# Non è sotto Program Files (x86) né sotto Program Files: richiediamo Java x86 per coerenza
Write-Log -Level WARN -Message "Trovato jvm.dll in percorso non-x86: $cand"
Write-Log -Level WARN -Message "È necessario installare una versione di Java x86."
return 1
}
}
}
# Se non troviamo jvm.dll nei percorsi previsti, controlliamo se è presente solo una Java x64
$pf = $env:ProgramFiles
$pf86 = ${env:ProgramFiles(x86)}
$found64 = $false
$found86 = $false
try {
# Search exclusively for vendor installation paths (require vendor dir in path)
$vendorDirRegex = '(?i)\\Program Files \(x86\)\\.*(java|eclipse adoptium|adoptopenjdk|zulu|amazon corretto|oracle|temurin|openjdk)\\'
if ($pf86) {
$found86 = (Get-ChildItem -Path $pf86 -Recurse -Filter 'jre.dll' -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -match $vendorDirRegex } | Select-Object -First 1) -ne $null
if (-not $found86) {
$found86 = (Get-ChildItem -Path $pf86 -Recurse -Filter 'jvm.dll' -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -match $vendorDirRegex } | Select-Object -First 1) -ne $null
}
}
if ($pf) {
$found64 = (Get-ChildItem -Path $pf -Recurse -Filter 'jre.dll' -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -match $vendorDirRegex } | Select-Object -First 1) -ne $null
if (-not $found64) {
$found64 = (Get-ChildItem -Path $pf -Recurse -Filter 'jvm.dll' -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -match $vendorDirRegex } | Select-Object -First 1) -ne $null
}
}
}
catch {
# ignore errors while probing filesystem
}
# If we didn't find a candidate earlier, probe Program Files and Program Files (x86)
$found64Path = $null
$found86Path = $null
try {
if ($pf) {
$found64Path = (Get-ChildItem -Path $pf -Recurse -Filter 'jvm.dll' -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -match $vendorDirRegex } |
Select-Object -ExpandProperty FullName -First 1)
}
if ($pf86) {
$found86Path = (Get-ChildItem -Path $pf86 -Recurse -Filter 'jvm.dll' -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -match $vendorDirRegex } |
Select-Object -ExpandProperty FullName -First 1)
}
}
catch {
# ignore errors while probing filesystem
}
if ($found86Path) {
Write-Log -Level INFO -Message "Rilevato jvm.dll x86 in: $found86Path"
Write-Log -Level INFO -Message "Detection: configurazione OK, nessuna remediation necessaria."
return 0
}
if ($found64Path -and -not $found86Path) {
Write-Log -Level WARN -Message "Rilevato solo Java x64 ($pf); DOCFA richiede Java x86. Installare Java x86."
return 1
}
Write-Log -Level WARN -Message "Il file jvm.dll indicato non esiste nei percorsi previsti (RuntimeLib: $runtimeLib)."
Write-Log -Level INFO -Message "Detection: remediation richiesta (file jvm.dll inesistente)."
return 1
}
catch {
Write-Log -Level ERROR -Message "Errore durante la detection: $($_.Exception.Message)"
return 1
}
}
# Se lo script viene eseguito come file, usciamo con il codice restituito dalla funzione
if ($MyInvocation.InvocationName -eq $MyInvocation.MyCommand.Name) {
$status = Get-DOCFAJavaDetectionStatus
exit $status
}