-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcwin11.ps1
241 lines (209 loc) · 8.58 KB
/
mcwin11.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
# Load Windows Forms assembly
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Function to check if Chocolatey is installed
function Check-Chocolatey {
try {
# Check if Chocolatey is installed
$chocoVersion = choco --version 2>$null
if ($chocoVersion) {
return $true
}
} catch {
# Chocolatey is not installed
return $false
}
return $false
}
# Function to install Chocolatey
function Install-Chocolatey {
$chocoInstallScriptUrl = "https://community.chocolatey.org/install.ps1"
try {
# Download and install Chocolatey
Invoke-WebRequest -Uri $chocoInstallScriptUrl -UseBasicParsing -OutFile "$env:TEMP\choco-install.ps1"
& "$env:TEMP\choco-install.ps1"
Write-Output "Chocolatey installed successfully."
} catch {
Write-Output "Failed to install Chocolatey."
[System.Windows.Forms.MessageBox]::Show("Failed to install Chocolatey. Exiting.")
exit
}
}
# Function to get installed Chocolatey packages
function Get-InstalledPackages {
$installedPackages = choco list --no-color | ForEach-Object {
$packageName = $_.Split("|")[0].Trim().ToLower()
$packageName -replace '[\.\s\d].*$', '' # Remove all text after a period, space, or number
}
return $installedPackages
}
# Check if Chocolatey is installed, and install if not
if (-Not (Check-Chocolatey)) {
Install-Chocolatey
}
# Get the list of installed packages
$installedPackages = Get-InstalledPackages
# Define the URL for the software list
$url = "https://raw.githubusercontent.com/mckayc/McWindows/master/McChocolateyList.txt"
# Download the software list file
$response = Invoke-WebRequest -Uri $url
$softwareListContent = $response.Content
# Check if the software list content was retrieved successfully
if (-Not $softwareListContent) {
[System.Windows.Forms.MessageBox]::Show("Software list could not be retrieved!")
exit
}
# Read and parse the software list content
$softwareList = $softwareListContent -split "`n" | ForEach-Object {
$parts = $_.Trim().Split("=")
if ($parts.Length -eq 2) {
[PSCustomObject]@{ Software = $parts[0].Trim(); Category = $parts[1].Trim() }
}
}
# Group software by category
$groupedSoftware = $softwareList | Group-Object Category
# Separate "Install First" category and the rest
$installFirstGroup = $groupedSoftware | Where-Object { $_.Name -eq "Install First" }
$otherGroups = $groupedSoftware | Where-Object { $_.Name -ne "Install First" } | Sort-Object Name
# Create and configure the form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Software Provisioning"
$form.Size = New-Object System.Drawing.Size(600, 800)
$form.StartPosition = "CenterScreen"
$form.MinimumSize = New-Object System.Drawing.Size(600, 800)
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Sizable
$form.MaximizeBox = $true
$form.MinimizeBox = $true
$form.ControlBox = $true
# Create a container for categories
$categoryPanel = New-Object System.Windows.Forms.FlowLayoutPanel
$categoryPanel.Dock = [System.Windows.Forms.DockStyle]::Fill
$categoryPanel.AutoScroll = $true
$categoryPanel.FlowDirection = [System.Windows.Forms.FlowDirection]::TopDown
$categoryPanel.WrapContents = $false
$form.Controls.Add($categoryPanel)
# Create a container for buttons
$buttonPanel = New-Object System.Windows.Forms.Panel
$buttonPanel.Dock = [System.Windows.Forms.DockStyle]::Bottom
$buttonPanel.Height = 60
$form.Controls.Add($buttonPanel)
# Create a container for progress bar
$progressPanel = New-Object System.Windows.Forms.Panel
$progressPanel.Dock = [System.Windows.Forms.DockStyle]::Bottom
$progressPanel.Height = 40
$form.Controls.Add($progressPanel)
# Create a text box for output
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Multiline = $true
$outputBox.Dock = [System.Windows.Forms.DockStyle]::Bottom
$outputBox.Height = 100
$outputBox.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical
$outputBox.ReadOnly = $true
$outputBox.BackColor = [System.Drawing.Color]::LightGray
$form.Controls.Add($outputBox)
# Create and configure the Submit button
$submitButton = New-Object System.Windows.Forms.Button
$submitButton.Text = "Submit"
$submitButton.Width = 100
$submitButton.Height = 40
$submitButton.Font = New-Object System.Drawing.Font("Segoe UI", 12)
$submitButton.BackColor = [System.Drawing.Color]::RoyalBlue
$submitButton.ForeColor = [System.Drawing.Color]::White
$submitButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$submitButton.Top = 10
$submitButton.Left = 10
$buttonPanel.Controls.Add($submitButton)
# Create and configure the progress bar
$progressBar = New-Object System.Windows.Forms.ProgressBar
$progressBar.Style = [System.Windows.Forms.ProgressBarStyle]::Continuous
$progressBar.Dock = [System.Windows.Forms.DockStyle]::Fill
$progressBar.Height = 20
$progressPanel.Controls.Add($progressBar)
# Add "Install First" category at the top
if ($installFirstGroup) {
$categoryGroupBox = New-Object System.Windows.Forms.GroupBox
$categoryGroupBox.Text = "Install First"
$categoryGroupBox.Width = 580
$categoryGroupBox.AutoSize = $true
$categoryGroupBox.Padding = "10,10,10,10"
$categoryGroupBox.Font = New-Object System.Drawing.Font("Segoe UI", 10)
$topPosition = 20
foreach ($item in $installFirstGroup.Group) {
$checkbox = New-Object System.Windows.Forms.CheckBox
$checkbox.Text = $item.Software
if ($installedPackages -contains ($item.Software.ToLower() -replace '[\.\s\d]+.*$', '')) {
$checkbox.Checked = $true
$checkbox.BackColor = [System.Drawing.Color]::LightGreen
}
$checkbox.Top = $topPosition
$checkbox.Left = 10
$checkbox.Width = 550
$topPosition += 30
$categoryGroupBox.Controls.Add($checkbox)
}
$categoryPanel.Controls.Add($categoryGroupBox)
}
# Add other categories, alphabetized
foreach ($group in $otherGroups) {
$categoryGroupBox = New-Object System.Windows.Forms.GroupBox
$categoryGroupBox.Text = $group.Name
$categoryGroupBox.Width = 580
$categoryGroupBox.AutoSize = $true
$categoryGroupBox.Padding = "10,10,10,10"
$categoryGroupBox.Font = New-Object System.Drawing.Font("Segoe UI", 10)
# Add checkboxes for each software in the category
$topPosition = 20
foreach ($item in $group.Group | Sort-Object Software) {
$checkbox = New-Object System.Windows.Forms.CheckBox
$checkbox.Text = $item.Software
if ($installedPackages -contains ($item.Software.ToLower() -replace '[\.\s\d]+.*$', '')) {
$checkbox.Checked = $true
$checkbox.BackColor = [System.Drawing.Color]::LightGreen
}
$checkbox.Top = $topPosition
$checkbox.Left = 10
$checkbox.Width = 550
$topPosition += 30
$categoryGroupBox.Controls.Add($checkbox)
}
$categoryPanel.Controls.Add($categoryGroupBox)
}
# Add event handler for Submit button
$submitButton.Add_Click({
# Collect selected software
$selectedSoftware = @()
foreach ($groupBox in $categoryPanel.Controls) {
foreach ($checkbox in $groupBox.Controls) {
if ($checkbox.Checked -and $checkbox.BackColor -ne [System.Drawing.Color]::LightGreen) {
$selectedSoftware += $checkbox.Text
}
}
}
# Function to install selected software asynchronously
function Install-SelectedSoftware {
param ([string[]]$softwareList)
$progressBar.Maximum = $softwareList.Count
$progressBar.Value = 0
foreach ($software in $softwareList) {
$outputBox.AppendText("Installing $software..." + "`r`n")
$outputBox.AppendText("Starting installation..." + "`r`n")
$form.Refresh()
# Execute the install command asynchronously
Start-Job -ScriptBlock {
param ($software)
$process = Start-Process -FilePath "choco" -ArgumentList "install $software -y" -NoNewWindow -PassThru -RedirectStandardOutput "$env:TEMP\choco-install-output.log"
$process.WaitForExit()
# Read output and update progress
$output = Get-Content "$env:TEMP\choco-install-output.log"
[System.Windows.Forms.MessageBox]::Show($output -join "`r`n", "Installation Output")
} -ArgumentList $software | Out-Null
# Update progress bar
$progressBar.Value++
$form.Refresh()
}
}
# Start installation
Install-SelectedSoftware -softwareList $selectedSoftware
})
# Show the form
[System.Windows.Forms.Application]::Run($form)