Repository navigation
Expand file tree
/
Copy pathSys_Inst.ps1
More file actions
222 lines (199 loc) · 8.1 KB
/
Copy pathSys_Inst.ps1
File metadata and controls
222 lines (199 loc) · 8.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
<#
.SYNOPSIS
Automated Windows 11 Upgrade, Software Installation, and System Optimization Script
.DESCRIPTION
This script provides a fully automated process for upgrading Windows 11, installing software, and customizing system settings.
Supports both automatic and advanced modes for efficient and flexible system deployment. Includes robust error handling and improved logging.
.NOTES
Version: 2.1
Author: AI Assistant
Date: 2024-12-07
#>
# Global Settings
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$LogFilePath = "C:\WindowsUpgrade.log" # Centralized log file
$ChocolateyInstallPath = "$env:LOCALAPPDATA\Programs\chocolatey" #Path to check if Chocolatey is installed
# Create Log Directory if it doesn't exist
if (!(Test-Path -Path (Split-Path $LogFilePath -Parent))) {
New-Item -ItemType Directory -Force -Path (Split-Path $LogFilePath -Parent)
}
# Logging Function
function Write-Log {
param(
[string]$Message,
[string]$Level = 'Info'
)
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$LogMessage = "[$Level] [$Timestamp] $Message"
Write-Host $LogMessage
Add-Content -Path $LogFilePath -Value $LogMessage
}
# --- Modules ---
# 1. System Upgrade Module
function Upgrade-Windows {
param(
[string]$IsoPath,
[string]$Edition = "Pro",
[switch]$AutoMode
)
try {
Write-Log "Starting Windows Upgrade..."
# Validate ISO Path
if (!(Test-Path $IsoPath)) { throw "Error: ISO file not found at '$IsoPath'. Please provide a valid path." }
$mountResult = Mount-DiskImage -ImagePath $IsoPath -PassThru
if (-not $mountResult) { throw "Error: Failed to mount ISO image. Please ensure the ISO is valid and accessible." }
$driveLetter = ($mountResult | Get-Volume).DriveLetter
$args = "/auto upgrade /dynamicupdate disable /noreboot"
if ($AutoMode) { $args += "/quiet" }
Start-Process -FilePath "$($driveLetter):\setup.exe" -ArgumentList $args -Wait
Write-Log "Windows Upgrade Successful!"
}
catch { Write-Log "Windows Upgrade Failed: $_" -Level "Error" }
finally {
try { Dismount-DiskImage -ImagePath $IsoPath }
catch { Write-Log "Error dismounting ISO: $_" -Level "Warning" }
}
}
# 2. Software Installation Module (uses Winget for consistency)
function Install-Software {
param(
[string[]]$Packages
)
foreach ($package in $Packages) {
try {
Write-Log "Installing $package..."
winget install "$package" --silent --accept-source-agreements
}
catch { Write-Log "Failed to install $package: $_" -Level "Warning" }
}
}
# 3. System Optimization Module
function Optimize-System {
try {
Write-Log "Optimizing System Settings..."
Set-ExecutionPolicy RemoteSigned -Force
Set-WinSystemLocale zh-TW
Set-WmiInstance -Class Win32_PageFileSetting -Arguments @{Name = 'C:\pagefile.sys'; InitialSize = 0; MaximumSize = 0}
bcdedit /set hypervisorlaunchtype auto
powercfg -S 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
Powercfg /h /type full
Powercfg /h on
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -NoRestart
Write-Log "System Optimization Complete!"
}
catch { Write-Log "System Optimization Failed: $_" -Level "Error" }
}
# 4. System Update and Cleanup Module
function Update-And-Cleanup {
try {
Write-Log "Updating and Cleaning System..."
# Check if PSWindowsUpdate is installed; install if necessary.
if (!(Get-Module -Name PSWindowsUpdate -ErrorAction SilentlyContinue)) {
Write-Log "Installing PSWindowsUpdate module..."
try {
Install-Module -Name PSWindowsUpdate -Force -Scope CurrentUser
}
catch {
Write-Log "PSWindowsUpdate installation failed: $_" -Level "Error"
return
}
}
Get-WindowsUpdate -AcceptAll -Install
winget upgrade --all --silent
dism /Online /Cleanup-Image /StartComponentCleanup /ResetBase
Write-Log "System Update and Cleanup Complete!"
}
catch { Write-Log "System Update or Cleanup Failed: $_" -Level "Error" }
}
# 5. Chocolatey Installation and Management (with error handling)
function Manage-Chocolatey {
try {
if (!(Test-Path $ChocolateyInstallPath)) {
Write-Log "Installing Chocolatey..."
Set-ExecutionPolicy Bypass -Scope Process -Force;
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
Write-Log "Chocolatey installation successful."
} else {
Write-Log "Chocolatey already installed."
}
#Upgrade Chocolatey (optional)
choco upgrade chocolatey -y
# Perform Chocolatey cleanup (optional - uncomment if needed)
#powershell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\tools\BCURRAN3\choco-cleaner.ps1' %*" # Replace with your choco-cleaner path if used.
}
catch {
Write-Log "Chocolatey installation or management failed: $_" -Level "Error"
}
}
# --- Main Execution ---
function Main {
# Check for Winget and install if necessary
if (!(Get-Command winget -ErrorAction SilentlyContinue)) {
Write-Log "Winget not found, attempting to install..."
try {
Install-PackageProvider -Name NuGet -Force | Out-Null
Install-Module -Name Microsoft.WinGet.Client -Force -Repository PSGallery | Out-Null
Repair-WinGetPackageManager
} catch {
Write-Log "Winget installation failed: $_" -Level "Error"
return
}
}
Manage-Chocolatey #Install or manage Chocolatey before proceeding
# Interactive Menu
Write-Host "Windows 11 Upgrade and Optimization Tool v2.1"
Write-Host "1. Automatic Mode"
Write-Host "2. Advanced Mode"
$choice = Read-Host "Select Mode (1/2):"
switch ($choice) {
"1" {
$isoPath = Read-Host "Enter path to Windows 11 ISO:"
#Validate ISO Path input (simple check)
if (-not (Test-Path $isoPath)) {
Write-Log "Invalid ISO path. Exiting." -Level "Error"
return
}
Upgrade-Windows -IsoPath $isoPath -AutoMode
Install-Software -Packages @("7zip.7zip", "Google.Chrome", "VideoLAN.VLC", "Microsoft.WindowsTerminal")
Optimize-System
Update-And-Cleanup
}
"2" {
# Advanced Mode
do {
Write-Host ""
Write-Host "Advanced Mode Menu:"
Write-Host "1. Upgrade Windows"
Write-Host "2. Install Software"
Write-Host "3. Optimize System"
Write-Host "4. Update and Cleanup"
Write-Host "5. Manage Chocolatey" # Added option
Write-Host "6. Exit"
$moduleChoice = Read-Host "Select a module (1-6):"
switch ($moduleChoice) {
"1" {
$isoPath = Read-Host "Enter path to Windows 11 ISO:"
if (-not (Test-Path $isoPath)) {
Write-Log "Invalid ISO path. Skipping Upgrade." -Level "Warning"
continue
}
Upgrade-Windows -IsoPath $isoPath
}
"2" { Install-Software -Packages @("7zip.7zip", "Google.Chrome", "VideoLAN.VLC", "Microsoft.WindowsTerminal") }
"3" { Optimize-System }
"4" { Update-And-Cleanup }
"5" { Manage-Chocolatey }
"6" { break }
default { Write-Log "Invalid choice. Please try again." -Level "Warning" }
}
} while ($moduleChoice -ne "6")
}
default { Write-Log "Invalid choice. Exiting." -Level "Error" }
}
Write-Log "Script Execution Complete!"
}
# Run Main Function
Main