-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSGuiTab4
195 lines (162 loc) · 7.02 KB
/
PSGuiTab4
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
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Function to write log
function Write-Log {
param([string]$message)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path "C:\temp\PSModuleInstaller.log" -Value "$timestamp - $message"
}
# Create the main form
$form = New-Object System.Windows.Forms.Form
$form.Text = 'PowerShell Module Installer'
$form.Size = New-Object System.Drawing.Size(700,550)
# Create TabControl
$tabControl = New-Object System.Windows.Forms.TabControl
$tabControl.Location = New-Object System.Drawing.Point(10,10)
$tabControl.Size = New-Object System.Drawing.Size(670,450)
# Create Tabs
$tabSearch = New-Object System.Windows.Forms.TabPage
$tabSearch.Text = 'Search Modules'
$tabInstalled = New-Object System.Windows.Forms.TabPage
$tabInstalled.Text = 'Installed Modules'
$tabControl.Controls.Add($tabSearch)
$tabControl.Controls.Add($tabInstalled)
$form.Controls.Add($tabControl)
# Progress Bar
$progressBar = New-Object System.Windows.Forms.ProgressBar
$progressBar.Location = New-Object System.Drawing.Point(10,490)
$progressBar.Size = New-Object System.Drawing.Size(670,20)
$progressBar.Style = [System.Windows.Forms.ProgressBarStyle]::Continuous
$form.Controls.Add($progressBar)
# Status Label
$statusLabel = New-Object System.Windows.Forms.Label
$statusLabel.Location = New-Object System.Drawing.Point(10,470)
$statusLabel.Size = New-Object System.Drawing.Size(670,20)
$form.Controls.Add($statusLabel)
# Adding controls to Search Modules Tab
$searchLabel = New-Object System.Windows.Forms.Label
$searchLabel.Location = New-Object System.Drawing.Point(10,10)
$searchLabel.Size = New-Object System.Drawing.Size(650,40)
$searchLabel.Text = 'Please enter the name of the PowerShell module you require. A partial name is acceptable.'
$tabSearch.Controls.Add($searchLabel)
$searchBox = New-Object System.Windows.Forms.TextBox
$searchBox.Location = New-Object System.Drawing.Point(10,50)
$searchBox.Size = New-Object System.Drawing.Size(260,20)
$tabSearch.Controls.Add($searchBox)
$searchButton = New-Object System.Windows.Forms.Button
$searchButton.Location = New-Object System.Drawing.Point(280,50)
$searchButton.Size = New-Object System.Drawing.Size(100,20)
$searchButton.Text = 'Search'
$tabSearch.Controls.Add($searchButton)
$resultsLabel = New-Object System.Windows.Forms.Label
$resultsLabel.Location = New-Object System.Drawing.Point(10,80)
$resultsLabel.Size = New-Object System.Drawing.Size(350,20)
$resultsLabel.Text = 'PowerShell Modules found:'
$tabSearch.Controls.Add($resultsLabel)
$checkedListBox = New-Object System.Windows.Forms.CheckedListBox
$checkedListBox.Location = New-Object System.Drawing.Point(10,100)
$checkedListBox.Size = New-Object System.Drawing.Size(350,280)
$checkedListBox.CheckOnClick = $true
$tabSearch.Controls.Add($checkedListBox)
$addButton = New-Object System.Windows.Forms.Button
$addButton.Location = New-Object System.Drawing.Point(280,390)
$addButton.Size = New-Object System.Drawing.Size(80,20)
$addButton.Text = 'Add'
$tabSearch.Controls.Add($addButton)
$selectedLabel = New-Object System.Windows.Forms.Label
$selectedLabel.Location = New-Object System.Drawing.Point(370,80)
$selectedLabel.Size = New-Object System.Drawing.Size(290,20)
$selectedLabel.Text = 'PowerShell modules selected for installation:'
$tabSearch.Controls.Add($selectedLabel)
$selectedListBox = New-Object System.Windows.Forms.ListBox
$selectedListBox.Location = New-Object System.Drawing.Point(370,100)
$selectedListBox.Size = New-Object System.Drawing.Size(290,280)
$tabSearch.Controls.Add($selectedListBox)
$installButton = New-Object System.Windows.Forms.Button
$installButton.Location = New-Object System.Drawing.Point(580,390)
$installButton.Size = New-Object System.Drawing.Size(80,20)
$installButton.Text = 'Install'
$tabSearch.Controls.Add($installButton)
$selectedModules = New-Object System.Collections.ArrayList
# Search Button Click Event
$searchButton.Add_Click({
$statusLabel.Text = "Searching for modules: $($searchBox.Text)"
$progressBar.Value = 0
$progressBar.PerformStep()
Write-Log "Searching for modules: $($searchBox.Text)"
$checkedListBox.Items.Clear()
$modules = Find-Module -Name "*$($searchBox.Text)*" | Sort-Object Name
foreach ($module in $modules) {
$checkedListBox.Items.Add($module.Name)
}
$progressBar.Value = 100
$statusLabel.Text = "Search complete."
Write-Log "Search completed."
})
# Add Button Click Event
$addButton.Add_Click({
foreach ($item in $checkedListBox.CheckedItems) {
if ($selectedModules -notcontains $item) {
$selectedModules.Add($item) | Out-Null
$selectedListBox.Items.Add($item)
}
}
})
# Install Button Click Event
$installButton.Add_Click({
$statusLabel.Text = "Installing selected modules..."
$progressBar.Value = 0
$progressBar.PerformStep()
Write-Log "Installing modules..."
foreach ($module in $selectedModules) {
Install-Module -Name $module -Force -Scope CurrentUser
}
$progressBar.Value = 100
$statusLabel.Text = "Installation complete."
Write-Log "Installation complete."
})
# Adding controls to Installed Modules Tab
$showInstalledButton = New-Object System.Windows.Forms.Button
$showInstalledButton.Location = New-Object System.Drawing.Point(10,10)
$showInstalledButton.Size = New-Object System.Drawing.Size(160,30)
$showInstalledButton.Text = 'Show Installed Modules'
$tabInstalled.Controls.Add($showInstalledButton)
$installedModulesBox = New-Object System.Windows.Forms.CheckedListBox
$installedModulesBox.Location = New-Object System.Drawing.Point(10,50)
$installedModulesBox.Size = New-Object System.Drawing.Size(640,300)
$installedModulesBox.CheckOnClick = $true
$tabInstalled.Controls.Add($installedModulesBox)
$uninstallButton = New-Object System.Windows.Forms.Button
$uninstallButton.Location = New-Object System.Drawing.Point(10,360)
$uninstallButton.Size = New-Object System.Drawing.Size(160,30)
$uninstallButton.Text = 'Uninstall Selected Modules'
$tabInstalled.Controls.Add($uninstallButton)
# Show Installed Modules Button Click Event
$showInstalledButton.Add_Click({
$statusLabel.Text = "Loading installed modules..."
$progressBar.Value = 0
$progressBar.PerformStep()
Write-Log "Loading installed modules..."
$installedModulesBox.Items.Clear()
$installedModules = Get-Module -ListAvailable | Sort-Object Name
foreach ($module in $installedModules) {
$installedModulesBox.Items.Add($module.Name)
}
$progressBar.Value = 100
$statusLabel.Text = "Loaded installed modules."
})
# Uninstall Button Click Event
$uninstallButton.Add_Click({
$statusLabel.Text = "Uninstalling selected modules..."
$progressBar.Value = 0
$progressBar.PerformStep()
Write-Log "Uninstalling modules..."
foreach ($module in $installedModulesBox.CheckedItems) {
Uninstall-Module -Name $module -Force
}
$progressBar.Value = 100
$statusLabel.Text = "Uninstallation complete."
Write-Log "Uninstallation complete."
})
# Show the form
$form.ShowDialog()