-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.ps1
377 lines (260 loc) · 12.6 KB
/
install.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#-----------------------------------------------
# NOTES
#-----------------------------------------------
<#
# To get this script going, make sure to allow the exeuction of scripts with
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# To get the download of the script going, you maybe need TLS12 support with
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
#>
#-----------------------------------------------
# GENERAL
#-----------------------------------------------
$isError = $false
$scriptSourceUrl = "https://raw.githubusercontent.com/Apteco/AptecoPSFramework/main/install.ps1"
$tempScriptFile = Join-Path $Env:Temp -ChildPath "install_aptecopsframework.ps1"
#-----------------------------------------------
# LOAD CURRENT PATH
#-----------------------------------------------
<#
if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript") {
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
} else {
$scriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0])
}
#>
$scriptPath = ( Resolve-Path -Path "." ).Path
Write-Verbose "Current Path: $( $scriptPath )" -Verbose
#-----------------------------------------------
# ADD MODULE PATH, IF NOT PRESENT
#-----------------------------------------------
$modulePaths = @( [System.Environment]::GetEnvironmentVariable("PSModulePath") -split ";" ) + @(
"C:\Program Files\WindowsPowerShell\Modules"
#C:\Program Files\powershell\7\Modules
"$( [System.Environment]::GetEnvironmentVariable("ProgramFiles") )\WindowsPowerShell\Modules"
"$( [System.Environment]::GetEnvironmentVariable("ProgramFiles(x86)") )\WindowsPowerShell\Modules"
"$( [System.Environment]::GetEnvironmentVariable("USERPROFILE") )\Documents\WindowsPowerShell\Modules"
"$( [System.Environment]::GetEnvironmentVariable("windir") )\system32\WindowsPowerShell\v1.0\Modules"
)
$Env:PSModulePath = ( $modulePaths | Sort-Object -unique ) -join ";"
# Using $env:PSModulePath for only temporary override
Write-Verbose "[OK] Adding module path" -Verbose
#-----------------------------------------------
# ADD SCRIPT PATH, IF NOT PRESENT
#-----------------------------------------------
#$envVariables = [System.Environment]::GetEnvironmentVariables()
$scriptPaths = @( [System.Environment]::GetEnvironmentVariable("Path") -split ";" ) + @(
"$( [System.Environment]::GetEnvironmentVariable("ProgramFiles") )\WindowsPowerShell\Scripts"
"$( [System.Environment]::GetEnvironmentVariable("ProgramFiles(x86)") )\WindowsPowerShell\Scripts"
"$( [System.Environment]::GetEnvironmentVariable("USERPROFILE") )\Documents\WindowsPowerShell\Scripts"
)
$Env:Path = ( $scriptPaths | Sort-Object -unique ) -join ";"
# Using $env:Path for only temporary override
Write-Verbose "[OK] Adding script path" -Verbose
#-----------------------------------------------
# CHECKING PS AND OS
#-----------------------------------------------
Write-Verbose "Check PowerShell and Operating system" -Verbose
# Check if this is Pwsh Core
$isCore = ($PSVersionTable.Keys -contains "PSEdition") -and ($PSVersionTable.PSEdition -ne 'Desktop')
Write-Verbose -Message "Using PowerShell version $( $PSVersionTable.PSVersion.ToString() ) and $( $PSVersionTable.PSEdition ) edition" -Verbose
# Check the operating system, if Core
if ($isCore -eq $true) {
$os = If ( $IsWindows -eq $true ) {
"Windows"
} elseif ( $IsLinux -eq $true ) {
"Linux"
} elseif ( $IsMacOS -eq $true ) {
"MacOS"
} else {
throw "Unknown operating system"
}
} else {
# [System.Environment]::OSVersion.VersionString()
# [System.Environment]::Is64BitOperatingSystem
$os = "Windows"
}
Write-Verbose -Message "Using OS: $( $os )" -Verbose
#-----------------------------------------------
# CHECKING ELEVATION
#-----------------------------------------------
# Check elevation
$isElevated = $false
if ( $os -eq "Windows" ) {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal]::new($identity)
$isElevated = $principal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
Write-Verbose -Message "User: $( $identity.Name )" -Verbose
Write-Verbose -Message "Elevated: $( $isElevated )" -Verbose
} else {
Write-Warning -Message "[FAIL] No user and elevation check due to OS. Leaving here!"
Exit 1
}
# TODO think about giving the choice to not install with elevated rights, but on own risk
If ( $isElevated -eq $false ) {
# Open PowerShell with elevated rights
Write-Warning -Message "For the initial setup it would be better to use elevated rights."
Write-Warning -Message "This is now opening a new powershell window with elevated rights."
Write-Warning -Message "Please repeat ""iwr https:// -useb | iex"" there"
try {
#Start-Process powershell -Verb RunAs
# Download the script, unblock it, create arguments with current directory and open elevated PowerShell with that folder and execute the script again
Invoke-WebRequest -Uri $scriptSourceUrl -UseBasicParsing -Method GET -OutFile $tempScriptFile
Unblock-File -Path $tempScriptFile # This does not seem to be needed, but doesn't hurt
$CommandLineArgumentList = "-NoExit", "-Command", "Set-Location ""$( $scriptPath )"";. ""$( $tempScriptFile )"""
Start-Process -FilePath "powershell.exe" -Verb RunAs -ArgumentList $CommandLineArgumentList
} catch {
throw "[FAIL] Failed to start PowerShell with elevated rights"
Exit 1
}
} else {
Write-Verbose "[OK] Using elevation" -Verbose
}
# Then proceed
#-----------------------------------------------
# CHECKING EXECUTION POLICY
#-----------------------------------------------
# Check your executionpolicy: https:/go.microsoft.com/fwlink/?LinkID=135170
#$execPolicyUser = Get-ExecutionPolicy -Scope CurrentUser
#$execPolicyMachine = Get-ExecutionPolicy -Scope LocalMachine
Write-Verbose "Current exeucution policies:" -Verbose
Get-ExecutionPolicy -List | ForEach-Object {
Write-Verbose " $( $_.Scope ): $( $_.ExecutionPolicy )" -Verbose
}
<#
try {
Write-Verbose "Setting the current exeuction policy to 'RemoteSigned'"
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
} catch {
$isError = $true
# Setting the exeuction policy back
Set-ExecutionPolicy -ExecutionPolicy $execPolicyUser -Scope CurrentUser
throw "There is a problem setting the execution policy"
Exit 1
} finally {
}
#>
#-----------------------------------------------
# UPDATE YOUR POWERSHELLGET
#-----------------------------------------------
try {
# Make sure to have PowerShellGet >= 1.6.0
#Get-InstalledModule -Name PowerShellGet -MinimumVersion 1.6.0
$psg = @( Get-InstalledModule | Where-Object { $_.Name -eq "PowerShellGet" -and $_.Version -gt "1.6.0" } )
If ( $psg.Count -eq 0 ) {
Install-Module -Name PowerShellGet -AllowClobber -Force
$psg = @( Get-InstalledModule | Where-Object { $_.Name -eq "PowerShellGet" -and $_.Version -gt "1.6.0" } )
Write-Verbose "[OK] PowerShellGet $( $psg.Version ) >= 1.6.0 available" -Verbose
} else {
Write-Verbose "[OK] PowerShellGet $( $psg.Version ) >= 1.6.0 available" -Verbose
}
} catch {
$isError = $true
# Setting the exeuction policy back
#Set-ExecutionPolicy -ExecutionPolicy $execPolicyUser -Scope CurrentUser
throw "[FAIL] There is a problem with the installation of PowerShellGet"
} finally {
}
#-----------------------------------------------
# INSTALL VCREDIST
#-----------------------------------------------
If ( $os -eq "Windows" ) {
# Set the paths
$vcredistPermalink = "https://aka.ms/vs/17/release/vc_redist.x64.exe"
$vcredistTargetFile = Join-Path -Path ( [System.Environment]::GetEnvironmentVariable("TMP")) -ChildPath "vc_redist.x64.exe"
# Download file - iwr is a bit slow, but works on all operating system
#Invoke-WebRequest -UseBasicParsing -Uri $vcredistPermalink -Method Get -OutFile $vcredistTargetFile
# Downlading with Bits as this package is windows only
Start-BitsTransfer -Destination $vcredistTargetFile -Source $vcredistPermalink
# Install file quietly
Start-Process -FilePath $vcredistTargetFile -ArgumentList "/install /q /norestart" -Verb RunAs -Wait
}
#-----------------------------------------------
# INSTALL BASE SCRIPTS AND MODULES
#-----------------------------------------------
try {
install-module writelog
Write-Verbose "[OK] Installed module WriteLog" -Verbose
install-script install-dependencies, import-dependencies
Write-Verbose "[OK] Installed scripts 'Install-Dependencies' and 'Import-Dependencies'" -Verbose
Install-Dependencies -module aptecopsframework
Write-Verbose "[OK] Installed module AptecoPSFramework and dependencies" -Verbose
} catch {
$isError = $true
# Setting the exeuction policy back
#Set-ExecutionPolicy -ExecutionPolicy $execPolicyUser -Scope CurrentUser
throw "[FAIL] There is a problem with the installation of base dependencies"
} finally {
}
#-----------------------------------------------
# IMPORT THE MODULE AND INSTALL MORE DEPENDENCIES
#-----------------------------------------------
# No try/catch here as the first import of aptecopsmodule will cause an error because of missing dependencies
$targetPathDefault = Resolve-Path -Path "." # $resolvedLogPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($PSCustom."logfile")
$targetPath = Read-Host -Prompt "Please enter the path where you want to put the scripts and settings to. Default is '[$( $targetPathDefault )]'"
# If prompt is empty, just use default path
if ( $targetPath -eq "" -or $null -eq $targetPath) {
$targetPath = $targetPathDefault
}
# Test the path and proceed
If ( Test-Path -Path $targetPath -IsValid ) {
Write-Verbose "The target path is valid" -verbose
If ( Test-Path -Path $targetPath ) {
Set-Location -Path $targetPath
Write-Verbose "[OK] Using path: '$( $targetPath )'" -Verbose
Import-Module AptecoPSFramework
Install-AptecoPSFramework -verbose
Write-Verbose "[OK] Loaded and installed AptecoPSFramework" -Verbose
} else {
throw "[FAIL] The target path '$( $targetPath )' is not existing"
}
} else {
throw " [FAIL] The target path '$( $targetPath )' is not valid"
}
Write-Verbose "[OK] All done here, you are good to go" -Verbose
#-----------------------------------------------
# CREATE A FIRST DEMO CHANNEL
#-----------------------------------------------
Write-Verbose "Creating a first Demo Channel to use" -Verbose
# Re-Import the module
Import-Module -Name "AptecoPSFramework" -Force
Write-Verbose "[OK] Re-Imported module AptecoPSFramework" -Verbose
# Default file
$settingsFileDefault = Join-Path -Path $scriptPath -ChildPath "/demo.yaml"
# Ask for another path
$settingsFile = Read-Host -Prompt "Where do you want the demo settings file to be saved? Just press Enter for this default '[$( $settingsFileDefault )]'"
# If prompt is empty, just use default path
if ( $settingsFile -eq "" -or $null -eq $settingsFile) {
$settingsFile = $settingsFileDefault
}
# Check if filename is valid
if(Test-Path -LiteralPath $settingsFile -IsValid ) {
Write-Verbose "[OK] SettingsFile '$( $settingsFile )' is valid" -Verbose
} else {
throw "[FAIL] SettingsFile '$( $settingsFile )' contains invalid characters"
}
# Set the plugin and export settings
$plugin = Get-Plugins | Where-Object { $_.name -eq "Demo" }
Write-Verbose "[OK] Loaded Demo plugin info with guid '$( $plugin.guid )'" -Verbose
Import-Plugin -Guid $plugin.guid
Write-Verbose "[OK] Imported Demo plugin" -Verbose
$s = Get-Settings
Write-Verbose "[OK] Got the settings of plugin in variable `$s" -Verbose
# You could change settings now! Or edit them in the yaml file.
$s.logfile = ".\file.log"
Write-Verbose "[OK] Changing logfile path in settings to: $( $s.logfile )" -Verbose
Set-Settings -PSCustom $s
Write-Verbose "[OK] Settings set!" -Verbose
Export-Settings -Path $settingsFile
Write-Verbose "[OK] Exported settings to $( $settingsFile )" -Verbose
Write-Verbose "Next time you simply could just enter" -Verbose
Write-Verbose "Import-Settings ""$( $settingsFile )""" -Verbose
Write-Verbose "And for example get a messagelist with" -Verbose
Write-Verbose "Get-Messages" -Verbose
#-----------------------------------------------
# REMOVE TEMP FILE, IF EXISTS
#-----------------------------------------------
If ( (Test-Path -Path $tempScriptFile ) -eq $true ) {
Remove-Item -Path $tempScriptFile -Force
Write-Verbose "[OK] Removed temporary script file" -Verbose
}