-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck.ps1
More file actions
333 lines (280 loc) · 12.1 KB
/
check.ps1
File metadata and controls
333 lines (280 loc) · 12.1 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#Requires -RunAsAdministrator
[CmdletBinding()]
param()
# Initialize variables
$ErrorActionPreference = "Stop"
$scriptName = "RustDesk Installation Check"
$logFolder = "C:\Windows\Temp\RustDeskDeploymentScripts"
$exitCode = 0
# CONFIGURATION - Set these values directly in the script as needed
$enableLogging = $true # Set to $true to enable logging, $false to disable
$checkRegistryPath = $true # Check registry for installation
$checkFilePath = $true # Check file system for installation
$expectedVersion = "" # Leave empty to check for any version, or specify like "1.2.3"
$customName = "" # Set to custom installation name, leave empty for "RustDesk"
$expectedInstallPath = "C:\Program Files\%customName%" # Expected installation path with %customName% placeholder
# Function to write colored output
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White",
[switch]$NoNewline
)
Write-Host $Message -ForegroundColor $Color -NoNewline:$NoNewline
}
# Function to write log
function Write-LogMessage {
param(
[string]$Message,
[string]$Level = "INFO"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "[$timestamp] [$Level] $Message"
if ($enableLogging) {
Add-Content -Path $logFile -Value $logMessage -Force
}
switch ($Level) {
"ERROR" { Write-ColorOutput $logMessage -Color Red }
"WARNING" { Write-ColorOutput $logMessage -Color Yellow }
"SUCCESS" { Write-ColorOutput $logMessage -Color Green }
default { Write-ColorOutput $logMessage -Color White }
}
}
# Function to replace %customName% placeholder
function Replace-CustomNamePlaceholder {
param(
[string]$Path,
[string]$Name
)
return $Path -replace '%customName%', $Name
}
# Function to check registry for installation
function Check-RegistryInstallation {
param(
[string]$SearchName
)
$registryPaths = @(
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
)
foreach ($path in $registryPaths) {
$items = Get-ItemProperty -Path $path -ErrorAction SilentlyContinue |
Where-Object { $_.DisplayName -like "*$SearchName*" }
if ($items) {
return $items[0]
}
}
return $null
}
# Function to check WMI for installation
function Check-WMIInstallation {
param(
[string]$SearchName
)
try {
$product = Get-WmiObject -Class Win32_Product -ErrorAction SilentlyContinue |
Where-Object { $_.Name -like "*$SearchName*" }
if ($product) {
return @{
Name = $product.Name
Version = $product.Version
InstallLocation = $product.InstallLocation
IdentifyingNumber = $product.IdentifyingNumber
}
}
} catch {
Write-LogMessage "WMI query failed: $_" -Level WARNING
}
return $null
}
# Function to check file system for installation
function Check-FileSystemInstallation {
param(
[string]$Path,
[string]$ExecutableName
)
if (Test-Path $Path) {
$exePath = Join-Path $Path "$ExecutableName.exe"
if (Test-Path $exePath) {
try {
$fileInfo = Get-Item $exePath
$versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($exePath)
return @{
Path = $Path
ExecutablePath = $exePath
FileVersion = $versionInfo.FileVersion
ProductVersion = $versionInfo.ProductVersion
LastWriteTime = $fileInfo.LastWriteTime
}
} catch {
Write-LogMessage "Failed to get file version info: $_" -Level WARNING
}
}
}
return $null
}
try {
Write-ColorOutput "====================================" -Color Cyan
Write-ColorOutput " RustDesk Installation Check " -Color Cyan
Write-ColorOutput "====================================" -Color Cyan
Write-Host ""
# Set the actual name to search for (fallback to RustDesk if not set)
$actualName = if ($customName) { $customName } else { "RustDesk" }
# Replace placeholder in expected install path
$expectedInstallPath = Replace-CustomNamePlaceholder -Path $expectedInstallPath -Name $actualName
Write-ColorOutput "Checking for: $actualName" -Color Yellow
Write-Host ""
# Create log folder if it doesn't exist
if ($enableLogging) {
if (!(Test-Path $logFolder)) {
New-Item -ItemType Directory -Path $logFolder -Force | Out-Null
}
$logFile = Join-Path $logFolder "check_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
Write-LogMessage "Starting installation check for: $actualName"
Write-LogMessage "Log file: $logFile"
}
$isInstalled = $false
$installationDetails = @{}
# Check registry
if ($checkRegistryPath) {
Write-LogMessage "Checking registry for $actualName installation..."
$registryInfo = Check-RegistryInstallation -SearchName $actualName
if ($registryInfo) {
$isInstalled = $true
$installationDetails.RegistryName = $registryInfo.DisplayName
$installationDetails.RegistryVersion = $registryInfo.DisplayVersion
$installationDetails.RegistryInstallLocation = $registryInfo.InstallLocation
$installationDetails.RegistryUninstallString = $registryInfo.UninstallString
Write-LogMessage "Found in registry: $($registryInfo.DisplayName) v$($registryInfo.DisplayVersion)" -Level SUCCESS
} else {
Write-LogMessage "Not found in registry"
}
}
# Check WMI
Write-LogMessage "Checking WMI for $actualName installation..."
$wmiInfo = Check-WMIInstallation -SearchName $actualName
if ($wmiInfo) {
$isInstalled = $true
$installationDetails.WMIName = $wmiInfo.Name
$installationDetails.WMIVersion = $wmiInfo.Version
$installationDetails.WMIProductCode = $wmiInfo.IdentifyingNumber
Write-LogMessage "Found in WMI: $($wmiInfo.Name) v$($wmiInfo.Version)" -Level SUCCESS
} else {
Write-LogMessage "Not found in WMI"
}
# Check file system
if ($checkFilePath) {
Write-LogMessage "Checking file system for $actualName installation..."
Write-LogMessage "Expected install path: $expectedInstallPath"
# Check expected path
$fileInfo = Check-FileSystemInstallation -Path $expectedInstallPath -ExecutableName $actualName
# If not found in expected path, check all possible Program Files locations
if (!$fileInfo) {
$alternativePaths = @(
"${env:ProgramFiles}\$actualName",
"${env:ProgramFiles(x86)}\$actualName"
)
# Add ARM Program Files path if it exists
$armProgramFiles = "${env:SystemDrive}\Program Files (Arm)"
if (Test-Path $armProgramFiles) {
$alternativePaths += "$armProgramFiles\$actualName"
Write-LogMessage "Checking ARM Program Files location..."
}
# Also check for ARM64 specific paths
$arm64ProgramFiles = "${env:ProgramFiles(Arm)}"
if ($arm64ProgramFiles -and (Test-Path $arm64ProgramFiles)) {
$alternativePaths += "$arm64ProgramFiles\$actualName"
}
# Also check common variations if custom name is not RustDesk
if ($actualName -ne "RustDesk") {
# Still check for RustDesk as fallback
$alternativePaths += "${env:ProgramFiles}\RustDesk"
$alternativePaths += "${env:ProgramFiles(x86)}\RustDesk"
if (Test-Path $armProgramFiles) {
$alternativePaths += "$armProgramFiles\RustDesk"
}
if ($arm64ProgramFiles -and (Test-Path $arm64ProgramFiles)) {
$alternativePaths += "$arm64ProgramFiles\RustDesk"
}
}
foreach ($altPath in $alternativePaths) {
Write-LogMessage "Checking path: $altPath"
# Determine which executable name to use based on path
$exeName = if ($altPath -like "*RustDesk*" -and $actualName -ne "RustDesk") {
"RustDesk"
} else {
$actualName
}
$fileInfo = Check-FileSystemInstallation -Path $altPath -ExecutableName $exeName
if ($fileInfo) {
Write-LogMessage "Found installation at: $altPath" -Level SUCCESS
if ($exeName -ne $actualName) {
Write-LogMessage "Note: Found as '$exeName' instead of '$actualName'" -Level WARNING
}
break
}
}
}
if ($fileInfo) {
$isInstalled = $true
$installationDetails.FilePath = $fileInfo.Path
$installationDetails.ExecutablePath = $fileInfo.ExecutablePath
$installationDetails.FileVersion = $fileInfo.FileVersion
Write-LogMessage "Found in file system: $($fileInfo.Path) v$($fileInfo.FileVersion)" -Level SUCCESS
} else {
Write-LogMessage "Not found in file system"
}
}
# Version check if specified
if ($isInstalled -and $expectedVersion) {
$currentVersion = $installationDetails.RegistryVersion -or $installationDetails.WMIVersion -or $installationDetails.FileVersion
if ($currentVersion -eq $expectedVersion) {
Write-LogMessage "Version check passed: $currentVersion matches expected version $expectedVersion" -Level SUCCESS
} else {
Write-LogMessage "Version mismatch: Current version $currentVersion does not match expected version $expectedVersion" -Level WARNING
$exitCode = 1 # Non-zero to indicate mismatch
}
}
# Summary
Write-Host ""
Write-ColorOutput "====================================" -Color Cyan
Write-ColorOutput " SUMMARY " -Color Cyan
Write-ColorOutput "====================================" -Color Cyan
if ($isInstalled) {
Write-LogMessage "$actualName IS INSTALLED on this system" -Level SUCCESS
# Display details
if ($installationDetails.RegistryName) {
Write-LogMessage " Registry Name: $($installationDetails.RegistryName)"
}
if ($installationDetails.RegistryVersion) {
Write-LogMessage " Version: $($installationDetails.RegistryVersion)"
}
if ($installationDetails.FilePath) {
Write-LogMessage " Install Path: $($installationDetails.FilePath)"
}
if ($installationDetails.WMIProductCode) {
Write-LogMessage " Product Code: $($installationDetails.WMIProductCode)"
}
# Check if it's an ARM installation
if ($installationDetails.FilePath -and $installationDetails.FilePath -like "*Arm*") {
Write-LogMessage " Architecture: ARM" -Level WARNING
}
if ($exitCode -eq 0) {
$exitCode = 0 # Installed and all checks passed
}
} else {
Write-LogMessage "$actualName IS NOT INSTALLED on this system" -Level WARNING
$exitCode = 1605 # This action is only valid for products that are currently installed
}
} catch {
Write-LogMessage "An error occurred during check: $_" -Level ERROR
$exitCode = 1603
} finally {
Write-ColorOutput "====================================" -Color Cyan
if ($enableLogging) {
Write-LogMessage "Check script completed with exit code: $exitCode"
} else {
Write-Host "Check script completed with exit code: $exitCode"
}
exit $exitCode
}