forked from DrEmpiricism/Optimize-Offline
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathStart-Optimize.ps1
139 lines (124 loc) · 5.16 KB
/
Start-Optimize.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
<#
.SYNOPSIS
Start-Optimize is a configuration call script for the Optimize-Offline module.
.DESCRIPTION
Start-Optimize automatically imports the configuration JSON file into the Optimize-Offline module.
.EXAMPLE
.\Start-Optimize.ps1
This command will import all values set in the configuration JSON file into the Optimize-Offline module and begin the optimization process.
.NOTES
Start-Optimize requires that the configuration JSON file is present in the root path of the Optimize-Offline module.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $false)] [switch]$populateLists,
[Parameter(Mandatory = $false)] [switch]$populateTemplates,
[Parameter(Mandatory = $false)] [switch]$GUI,
[Parameter(Mandatory = $false)] [int]$FlashUSBDriveNumber = -1
)
$Global:Error.Clear()
# Ensure we are running with administrative permissions.
If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$arguments = @(" Set-ExecutionPolicy Bypass -Scope Process -Force; & '" + $MyInvocation.MyCommand.Definition + "'")
foreach ($param in $PSBoundParameters.GetEnumerator()) {
$arguments += "-"+[string]$param.Key+$(If ($null -ne $param.Value -and $param.Value -ne "" -and $param.Value -notin @("True", "False")) {" '"+$param.Value+"'"} Else {""})
}
If(!$GUI){
$arguments += " ; pause"
}
Start-Process powershell -Verb RunAs -ArgumentList $arguments
Stop-Process -Id $PID
}
$configration_selected = "Configuration.json"
If (Test-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath "Configuration_custom.json")) {
$configration_selected = "Configuration_custom.json"
}
# Ensure the configuration JSON file exists.
If (!(Test-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath $configration_selected))) {
Write-Warning ('The required configuration JSON file does not exist: "{0}"' -f (Join-Path -Path $PSScriptRoot -ChildPath $configration_selected))
Start-Sleep 3
Exit
}
# If the configuration JSON or ordered collection list variables still exists from a previous session, remove them.
If ((Test-Path -Path Variable:\ContentJSON) -or (Test-Path -Path Variable:\ConfigParams)) {
Remove-Variable -Name ContentJSON, ConfigParams -ErrorAction Ignore
}
# Use a Try/Catch/Finally block in case the configuration JSON file URL formatting is invalid so we can catch it, correct its formatting and continue.
Try {
$ContentJSON = Get-Content -Path (Join-Path -Path $PSScriptRoot -ChildPath $configration_selected) -Raw | ConvertFrom-Json
}
Catch [ArgumentException] {
$ContentJSON = (Get-Content -Path (Join-Path -Path $PSScriptRoot -ChildPath $configration_selected) -Raw).Replace('\', '\\') | Set-Content -Path (Join-Path -Path $Env:TEMP -ChildPath $configration_selected) -Encoding UTF8 -Force -PassThru
$ContentJSON = $ContentJSON | ConvertFrom-Json
Move-Item -Path (Join-Path -Path $Env:TEMP -ChildPath $configration_selected) -Destination $PSScriptRoot -Force
$Global:Error.Remove($Error[-1])
}
Finally {
$ContentJSON.PSObject.Properties.Remove('_Info')
}
# Convert the JSON object into a nested ordered collection list. We use the PSObject.Properties method to retain the JSON object order.
$ConfigParams = [Ordered]@{ }
ForEach ($Name In $ContentJSON.PSObject.Properties.Name) {
$Value = $ContentJSON.PSObject.Properties.Item($Name).Value
If ($Value -is [PSCustomObject]) {
$ConfigParams.$Name = [Ordered]@{ }
ForEach ($Property in $Value.PSObject.Properties) {
$ConfigParams.$Name[$Property.Name] = $Property.Value
}
}
Else {
$ConfigParams.$Name = $Value
}
}
$ConfigParams.populateLists = $populateLists
$ConfigParams.populateTemplates = $populateTemplates
If ($FlashUSBDriveNumber -ge 0){
$ConfigParams.FlashUSBDriveNumber = $FlashUSBDriveNumber
}
# Import the Optimize-Offline module and call it by passing the JSON configuration.
If ($PSVersionTable.PSVersion.Major -gt 5) {
Try {
Import-Module Dism -SkipEditionCheck -Force -WarningAction Ignore -ErrorAction Stop
}
Catch {
Write-Warning 'Failed to import the required Dism module.'
Start-Sleep 3
Exit
}
Try {
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath Optimize-Offline.psm1) -SkipEditionCheck -Force -WarningAction Ignore -ErrorAction Stop
}
Catch {
Write-Warning ('Failed to import the Optimize-Offline module: "{0}"' -f (Join-Path -Path $PSScriptRoot -ChildPath Optimize-Offline.psm1))
Start-Sleep 3
Exit
}
}
Else {
Try {
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath Optimize-Offline.psm1) -Force -WarningAction Ignore -ErrorAction Stop
}
Catch {
Write-Warning ('Failed to import the Optimize-Offline module: "{0}"' -f (Join-Path -Path $PSScriptRoot -ChildPath Optimize-Offline.psm1))
Start-Sleep 3
Exit
}
}
If ($GUI){
Try{
Optimize-Offline @ConfigParams
} Catch {
$formatstring = "{0} : {1}`n{2}`n" +
" + CategoryInfo : {3}`n" +
" + FullyQualifiedErrorId : {4}`n"
$fields = $_.InvocationInfo.MyCommand.Name,
$_.ErrorDetails.Message,
$_.InvocationInfo.PositionMessage,
$_.CategoryInfo.ToString(),
$_.FullyQualifiedErrorId
Write-Host -Foreground Red ($formatstring -f $fields)
Exit
}
} Else {
Optimize-Offline @ConfigParams
}