-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInit.ps1
64 lines (61 loc) · 2.84 KB
/
Init.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
[CmdletBinding()]
param (
[Parameter()]
[string]
$SourcePath
)
$RequiredModules = @('Helpers', 'Chocolatey', 'Windows')
foreach ($RequiredModule in $RequiredModules) {
Import-Module -Name $(Join-Path -Path $(Join-Path -Path $PSScriptRoot -ChildPath "Modules") -ChildPath "$RequiredModule.psm1") -Force
}
Import-Module -Name $(Join-Path -Path $(Join-Path -Path $PSScriptRoot -ChildPath "Modules") -ChildPath "RestartManager") -Force
Write-Host -ForegroundColor Black -BackgroundColor Green "Initialising..."
if (!$(Test-ElevatedPermission)) {
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList " -File $SourcePath"
$Global:Live = $false
exit
}
if ($host.Version.Major -lt 7){
if (Test-Path $(Join-Path -Path $(Join-Path -Path $(Join-Path -Path $env:ProgramFiles -ChildPath "Powershell") -ChildPath "7") -ChildPath "pwsh.exe")){
Start-Process -FilePath $(Join-Path -Path $(Join-Path -Path $(Join-Path -Path $env:ProgramFiles -ChildPath "Powershell") -ChildPath "7") -ChildPath "pwsh.exe") -ArgumentList " -File $SourcePath"
throw "Relaunch in PowerShell 7"
$Global:Live = $false
exit
}
elseif ($(Join-Path -Path $(Join-Path -Path $(Join-Path -Path ${env:ProgramFiles(x86)} -ChildPath "Powershell") -ChildPath "7") -ChildPath "pwsh.exe")){
Start-Process -FilePath $(Join-Path -Path $(Join-Path -Path $(Join-Path -Path ${env:ProgramFiles(x86)} -ChildPath "Powershell") -ChildPath "7") -ChildPath "pwsh.exe") -ArgumentList " -File $SourcePath"
throw "Relaunch in PowerShell 7"
$Global:Live = $false
exit
}
else {
Invoke-Expression "& { $(Invoke-RestMethod "https://aka.ms/install-powershell.ps1") } -UseMSI -Quiet"
if ($LASTEXITCODE -ne 0) {
Throw "Install of PowerShell 7 has failed ..."
exit
}
Start-Process pwsh.exe -ArgumentList " -File $SourcePath"
throw "Relaunch in PowerShell 7"
$Global:Live = $false
exit
}
}
if (Test-PendingReboot) {
Write-Host -ForegroundColor Yellow "A reboot is pending"
$Exit = $false
while (!$Exit) {
$Answer = Read-Host "Do you want to continue ? (Y/N)"
if ($Answer -eq "N") {
throw "There is a pending reboot."
exit
}
elseif ($Answer -eq "Y") { $Exit = $true }
}
}
$envFile = $(Join-Path -Path $(Join-Path -Path $(Split-Path -Parent -Path $MyInvocation.MyCommand.Definition) -ChildPath "Config") -ChildPath "Vars.ps1")
$stateFile = $(Join-Path -Path $(Join-Path -Path $(Split-Path -Parent -Path $MyInvocation.MyCommand.Definition) -ChildPath "Config") -ChildPath "State.json")
Check -Path $envFile
Check -Path $stateFile
. $envFile
$State = Get-Content -Path $stateFile | ConvertFrom-Json -AsHashtable
$TempFolder = (New-Item -Path $env:TEMP -Name "PC-Config" -ItemType Directory -Force).FullName