-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetectR_EnhancedLogging
86 lines (76 loc) · 2.98 KB
/
DetectR_EnhancedLogging
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
# Custom detection script for Intune Win32 app with enhanced logging
# Define log file path
$logFilePath = "C:\ProgramData\Microsoft\IntuneApps\UpgradeR\Upgrader.log"
if (-not (Test-Path "C:\ProgramData\Microsoft\IntuneApps\UpgradeR")) {
New-Item -ItemType Directory -Path "C:\ProgramData\Microsoft\IntuneApps\UpgradeR" -Force
}
# Function to write log entries
function Write-Log {
param (
[string]$message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "$timestamp - $message"
Add-Content -Path $logFilePath -Value $logMessage
Write-Host $logMessage
}
Write-Log "Starting detection process."
# Ensure Chocolatey is installed
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
Write-Log "Chocolatey is not installed."
exit 1 # Chocolatey is not installed
} else {
Write-Log "Chocolatey is installed."
}
function Get-ChocoPackageVersion {
param (
[string]$packageName
)
Write-Log "Getting installed version for $packageName..."
$package = choco list --local-only $packageName | Select-String -Pattern $packageName
if ($package) {
$version = $package -replace '.*\|', ''
Write-Log "Installed version of $packageName: $version"
return $version
} else {
Write-Log "$packageName is not installed."
return $null
}
}
function Get-LatestChocoPackageVersion {
param (
[string]$packageName
)
Write-Log "Getting latest version for $packageName from Chocolatey..."
$packageInfo = choco info $packageName
$latestVersion = $packageInfo | Select-String -Pattern "$packageName\s+(\d+(\.\d+)+)" | ForEach-Object { $_.Matches[0].Groups[1].Value }
if ($latestVersion) {
Write-Log "Latest version of $packageName: $latestVersion"
return $latestVersion
} else {
Write-Log "Could not retrieve latest version for $packageName from Chocolatey."
return $null
}
}
$requiredPackages = @("r.project", "rstudio", "rtools")
$allUpToDate = $true
foreach ($package in $requiredPackages) {
$installedVersion = Get-ChocoPackageVersion -packageName $package
$latestVersion = Get-LatestChocoPackageVersion -packageName $package
if (-not $installedVersion) {
Write-Log "$package is not installed."
$allUpToDate = $false
} elseif ([version]$installedVersion -lt [version]$latestVersion) {
Write-Log "$package is installed but not up-to-date. Installed version: $installedVersion, Latest version: $latestVersion"
$allUpToDate = $false
} else {
Write-Log "$package is up-to-date. Installed version: $installedVersion, Latest version: $latestVersion"
}
}
if ($allUpToDate) {
Write-Log "All required packages are installed and up-to-date."
exit 0 # Exit code 0 means the application is detected and up-to-date
} else {
Write-Log "One or more required packages are not installed or not up-to-date."
exit 1 # Exit code 1 means the application is not detected or not up-to-date
}