-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
331 lines (277 loc) · 11.3 KB
/
install.ps1
File metadata and controls
331 lines (277 loc) · 11.3 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
#Requires -RunAsAdministrator
[CmdletBinding()]
param(
[Parameter()]
[string]$INSTALLFOLDER,
[Parameter()]
[ValidateSet("true", "false")]
[string]$CREATESTARTMENUSHORTCUTS,
[Parameter()]
[ValidateSet("true", "false")]
[string]$CREATEDESKTOPSHORTCUTS,
[Parameter()]
[ValidateSet("true", "false")]
[string]$INSTALLPRINTER,
[Parameter()]
[ValidateSet("true", "false")]
[string]$SILENT,
[Parameter()]
[string]$MSIPATH,
[Parameter()]
[ValidateSet("true", "false")]
[string]$UPGRADE,
[Parameter()]
[ValidateSet("true", "false")]
[string]$LOGGING,
[Parameter()]
[ValidateSet("true", "false")]
[string]$ASSIGN,
[Parameter()]
[string]$TOKEN,
[Parameter()]
[string]$CUSTOMNAME,
[Parameter()]
[string]$ASSIGNMENTFILE
)
# Initialize variables
$ErrorActionPreference = "Stop"
$scriptName = "RustDesk Installation"
$logFolder = "C:\Windows\Temp\RustDeskDeploymentScripts"
$exitCode = 0
# 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 validate true/false parameters
function Validate-BooleanParameter {
param(
[string]$Value,
[string]$ParameterName
)
if ($Value -and $Value -notin @("true", "false")) {
Write-LogMessage "Invalid value '$Value' for parameter $ParameterName. Expected 'true' or 'false'." -Level ERROR
exit 1603
}
}
# Function to convert boolean string to MSI value
function Convert-ToMSIValue {
param(
[string]$Value
)
if ($Value -eq "true") {
return "Y"
} elseif ($Value -eq "false") {
return "N"
}
return $null
}
try {
Write-ColorOutput "====================================" -Color Cyan
Write-ColorOutput " RustDesk Installation Script " -Color Cyan
Write-ColorOutput "====================================" -Color Cyan
Write-Host ""
# Create log folder if it doesn't exist
if (!(Test-Path $logFolder)) {
New-Item -ItemType Directory -Path $logFolder -Force | Out-Null
Write-LogMessage "Created log folder: $logFolder"
}
# Load config.json if exists
$configPath = Join-Path $PSScriptRoot "config.json"
$config = @{}
if (Test-Path $configPath) {
try {
$config = Get-Content $configPath -Raw | ConvertFrom-Json
Write-LogMessage "Loaded configuration from config.json"
} catch {
Write-LogMessage "Failed to load config.json: $_" -Level WARNING
}
}
# Merge parameters (CLI arguments take precedence)
if (!$INSTALLFOLDER -and $config.INSTALLFOLDER) { $INSTALLFOLDER = $config.INSTALLFOLDER }
if (!$CREATESTARTMENUSHORTCUTS -and $config.CREATESTARTMENUSHORTCUTS) { $CREATESTARTMENUSHORTCUTS = $config.CREATESTARTMENUSHORTCUTS }
if (!$CREATEDESKTOPSHORTCUTS -and $config.CREATEDESKTOPSHORTCUTS) { $CREATEDESKTOPSHORTCUTS = $config.CREATEDESKTOPSHORTCUTS }
if (!$INSTALLPRINTER -and $config.INSTALLPRINTER) { $INSTALLPRINTER = $config.INSTALLPRINTER }
if (!$SILENT -and $config.SILENT) { $SILENT = $config.SILENT }
if (!$MSIPATH -and $config.MSIPATH) { $MSIPATH = $config.MSIPATH }
if (!$UPGRADE -and $config.UPGRADE) { $UPGRADE = $config.UPGRADE }
if (!$LOGGING -and $config.LOGGING) { $LOGGING = $config.LOGGING }
if (!$ASSIGN -and $config.ASSIGN) { $ASSIGN = $config.ASSIGN }
if (!$TOKEN -and $config.TOKEN) { $TOKEN = $config.TOKEN }
if (!$CUSTOMNAME -and $config.CUSTOMNAME) { $CUSTOMNAME = $config.CUSTOMNAME }
if (!$ASSIGNMENTFILE -and $config.ASSIGNMENTFILE) { $ASSIGNMENTFILE = $config.ASSIGNMENTFILE }
# Set defaults
if (!$MSIPATH) {
$MSIPATH = Get-ChildItem -Path $PSScriptRoot -Filter "*.msi" | Select-Object -First 1 -ExpandProperty FullName
if (!$MSIPATH) {
Write-LogMessage "No MSI file found in script directory" -Level ERROR
exit 1603
}
}
# Enable logging
$enableLogging = $LOGGING -eq "true"
$logFile = Join-Path $logFolder "install_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
if ($enableLogging) {
Write-LogMessage "Logging enabled. Log file: $logFile"
}
# Validate MSI path
if (!(Test-Path $MSIPATH)) {
Write-LogMessage "MSI file not found: $MSIPATH" -Level ERROR
exit 1603
}
Write-LogMessage "MSI Path: $MSIPATH"
# Validate boolean parameters
Validate-BooleanParameter $CREATESTARTMENUSHORTCUTS "CREATESTARTMENUSHORTCUTS"
Validate-BooleanParameter $CREATEDESKTOPSHORTCUTS "CREATEDESKTOPSHORTCUTS"
Validate-BooleanParameter $INSTALLPRINTER "INSTALLPRINTER"
Validate-BooleanParameter $SILENT "SILENT"
Validate-BooleanParameter $UPGRADE "UPGRADE"
Validate-BooleanParameter $LOGGING "LOGGING"
Validate-BooleanParameter $ASSIGN "ASSIGN"
# Build MSI command
$msiArgs = @("/i", "`"$MSIPATH`"")
# Add silent flag if specified
if ($SILENT -eq "true") {
$msiArgs += "/qn"
Write-LogMessage "Silent installation mode enabled"
}
# Add installation parameters
if ($INSTALLFOLDER) {
$msiArgs += "INSTALLFOLDER=`"$INSTALLFOLDER`""
Write-LogMessage "Install folder: $INSTALLFOLDER"
}
$startMenuValue = Convert-ToMSIValue $CREATESTARTMENUSHORTCUTS
if ($startMenuValue) {
$msiArgs += "CREATESTARTMENUSHORTCUTS=`"$startMenuValue`""
Write-LogMessage "Create start menu shortcuts: $CREATESTARTMENUSHORTCUTS"
}
$desktopValue = Convert-ToMSIValue $CREATEDESKTOPSHORTCUTS
if ($desktopValue) {
$msiArgs += "CREATEDESKTOPSHORTCUTS=`"$desktopValue`""
Write-LogMessage "Create desktop shortcuts: $CREATEDESKTOPSHORTCUTS"
}
$printerValue = Convert-ToMSIValue $INSTALLPRINTER
if ($printerValue) {
$msiArgs += "INSTALLPRINTER=`"$printerValue`""
Write-LogMessage "Install printer: $INSTALLPRINTER"
}
# Always add install.log
$msiLogPath = Join-Path $logFolder "msi_install_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
$msiArgs += "/l*v", "`"$msiLogPath`""
Write-LogMessage "MSI log file: $msiLogPath"
# Display installation type
if ($UPGRADE -eq "true") {
Write-LogMessage "Performing upgrade installation" -Level WARNING
} else {
Write-LogMessage "Performing new installation"
}
# Execute MSI installation
Write-LogMessage "Starting RustDesk installation..."
Write-LogMessage "Command: msiexec.exe $($msiArgs -join ' ')"
$process = Start-Process -FilePath "msiexec.exe" -ArgumentList $msiArgs -Wait -PassThru -NoNewWindow
$exitCode = $process.ExitCode
# Interpret exit codes
switch ($exitCode) {
0 {
Write-LogMessage "RustDesk installed successfully!" -Level SUCCESS
}
1641 {
Write-LogMessage "RustDesk installed successfully. A restart is required to complete the installation." -Level SUCCESS
$exitCode = 3010 # Standard Intune reboot required code
}
3010 {
Write-LogMessage "RustDesk installed successfully. A restart is required to complete the installation." -Level SUCCESS
}
1603 {
Write-LogMessage "Fatal error during installation" -Level ERROR
}
1619 {
Write-LogMessage "This installation package could not be opened" -Level ERROR
}
1638 {
Write-LogMessage "Another version of this product is already installed" -Level ERROR
}
default {
if ($exitCode -ne 0) {
Write-LogMessage "Installation failed with exit code: $exitCode" -Level ERROR
}
}
}
# Run assignment script if ASSIGN is true and installation was successful
if ($ASSIGN -eq "true" -and ($exitCode -eq 0 -or $exitCode -eq 3010)) {
Write-Host ""
Write-ColorOutput "====================================" -Color Cyan
Write-LogMessage "Running assignment script..."
# Check if TOKEN is provided
if (!$TOKEN) {
Write-LogMessage "ASSIGN is set to true but TOKEN is not provided. Skipping assignments." -Level WARNING
} else {
# Build assignment script path
$assignScriptPath = Join-Path $PSScriptRoot "assign.ps1"
if (!(Test-Path $assignScriptPath)) {
Write-LogMessage "Assignment script not found: $assignScriptPath" -Level WARNING
} else {
# Build assignment arguments
$assignArgs = @(
"-ExecutionPolicy", "Bypass",
"-File", "`"$assignScriptPath`"",
"-TOKEN", "`"$TOKEN`""
)
if ($CUSTOMNAME) {
$assignArgs += "-CUSTOMNAME", "`"$CUSTOMNAME`""
}
if ($INSTALLFOLDER) {
$assignArgs += "-INSTALLFOLDER", "`"$INSTALLFOLDER`""
}
if ($LOGGING) {
$assignArgs += "-LOGGING", "`"$LOGGING`""
}
if ($ASSIGNMENTFILE) {
$assignArgs += "-ASSIGNMENTFILE", "`"$ASSIGNMENTFILE`""
}
Write-LogMessage "Executing assignment script..."
# Wait a moment for the installation to fully complete
Start-Sleep -Seconds 2
# Execute assignment script
$assignProcess = Start-Process -FilePath "powershell.exe" -ArgumentList $assignArgs -Wait -PassThru -NoNewWindow
if ($assignProcess.ExitCode -eq 0) {
Write-LogMessage "Assignment script completed successfully" -Level SUCCESS
} else {
Write-LogMessage "Assignment script completed with warnings or errors (Exit Code: $($assignProcess.ExitCode))" -Level WARNING
# Don't override the main exit code if installation was successful
}
}
}
} elseif ($ASSIGN -eq "true" -and $exitCode -ne 0 -and $exitCode -ne 3010) {
Write-LogMessage "Skipping assignments due to installation failure" -Level WARNING
}
} catch {
Write-LogMessage "An error occurred: $_" -Level ERROR
$exitCode = 1603
} finally {
Write-ColorOutput "====================================" -Color Cyan
Write-LogMessage "Installation script completed with exit code: $exitCode"
exit $exitCode
}