-
Notifications
You must be signed in to change notification settings - Fork 6
/
Select-Plugins.ps1
162 lines (135 loc) · 5.62 KB
/
Select-Plugins.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
<#
.NOTES
Select-Plugins.ps1
selectively enable / disable vCheck Plugins
presents a list of plugins whose names match *.ps1 or *.ps1.disabled
disabled plugins will be renamed as appropriate to <pluginname>.ps1.disabled
enabled plugins will be renamed as appropriate to <plugin name>.ps1
To use, run from the vCheck directory or, if you wish to be perverse, copy to the plugins
directory and rename to "ZZ Select Plugins for Next Run.ps1" and run vCheck as normal.
Great for testing plugins. When done, untick it...
If run as a plugin, it will affect the next vCheck run, not the current one,
as vCheck has already collected its list of plugins when it is invoked
so make it the very last plugin executed to avoid counter-intuitive behaviour
based on code from Select-GraphicalFilteredObject.ps1 in
"Windows Powershell Cookbook" by Lee Holmes.
Copyright 2007 Lee Holmes.
Published by O'Reilly ISBN 978-0-596-528492
and used under the 'free use' provisions specified on Preface page xxv
Changelog
============================
2.2 - Kevin Kirkpatrick
[X] Add cmdletbinding/param
[X] Move comments into proper comment based help block so it can be reviewed via standard
PS help system (EX: C:\PS>.\Get-Help Select-Plugins.ps1 -ShowWindow)
[X] Remove Write-Host; converted to Write-Warning
2.1 - Phil Randal
[X] Added Select All/Deselect All buttons - Changed sort to numeric
#>
[cmdletbinding()]
param()
$Title = "Plugin Selection Plugin"
$Author = "Phil Randal"
$PluginVersion = 2.1
$Header = "Plugin Selection"
$Comments = "Plugin Selection"
$Display = "None"
# Start of Settings
# End of Settings
$PluginPath = (Split-Path ((Get-Variable MyInvocation).Value).MyCommand.Path)
If ($PluginPath -notmatch 'plugins$') {
$PluginPath += "\Plugins"
}
$plugins = Get-ChildItem -Path $PluginPath -Include *.ps1, *.ps1.disabled -Recurse |
Sort {[int]($_.Name -replace '\D')} |
Select FullName, Name,
@{Label="Plugin";expression={$_.Name -replace '(.*)\.ps1(?:\.disabled|)$', '$1'}},
@{Label="Enabled";expression={$_.Name -notmatch '.*\.disabled$'}}
$selectallButton_OnClick = {
for($i = 0; $i -lt $listbox.Items.Count; $i++) {
$listbox.SetItemChecked($i,$true)
}
}
$deselectallButton_OnClick = {
for($i = 0; $i -lt $listbox.Items.Count; $i++) {
$listbox.SetItemChecked($i,$false)
}
}
## Load the Windows Forms assembly
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
## Create the main form
$form = New-Object Windows.Forms.Form
$form.Size = New-Object Drawing.Size @(600,600)
## Create the listbox to hold the items from the pipeline
$listbox = New-Object Windows.Forms.CheckedListBox
$listbox.CheckOnClick = $true
$listbox.Dock = "Fill"
$form.Text = "Select the plugins you wish to enable"
# create list box items from plugin list, tick as enabled where appropriate
ForEach ($plugin in $Plugins) {
$i=$listBox.Items.Add($plugin.Plugin)
$listbox.SetItemChecked($i, $Plugin.Enabled)
}
## Create the button panel to hold the OK and Cancel buttons
$buttonPanel = New-Object Windows.Forms.Panel
$buttonPanel.Size = New-Object Drawing.Size @(600,30)
$buttonPanel.Dock = "Bottom"
## Create the Cancel button, which will anchor to the bottom right
$cancelButton = New-Object Windows.Forms.Button
$cancelButton.Text = "Cancel"
$cancelButton.DialogResult = "Cancel"
$cancelButton.Top = $buttonPanel.Height - $cancelButton.Height - 5
$cancelButton.Left = $buttonPanel.Width - $cancelButton.Width - 10
$cancelButton.Anchor = "Right"
## Create the OK button, which will anchor to the left of Cancel
$okButton = New-Object Windows.Forms.Button
$okButton.Text = "Ok"
$okButton.DialogResult = "Ok"
$okButton.Top = $cancelButton.Top
$okButton.Left = $cancelButton.Left - $okButton.Width - 5
$okButton.Anchor = "Right"
## Create the Select All button, which will anchor to the bottom left
$selectallButton = New-Object Windows.Forms.Button
$selectallButton.Text = "Select All"
$selectallButton.Top = $cancelButton.Top
$selectallButton.Left = 10
$selectallButton.Anchor = "Left"
$selectallButton.add_Click($selectallButton_OnClick)
## Create the Deselect All button, which will anchor to the right of Select All
$deselectallButton = New-Object Windows.Forms.Button
$deselectallButton.Text = "Deselect All"
$deselectallButton.Top = $cancelButton.Top
$deselectallButton.Left = $selectallButton.Width + 15
$deselectallButton.Anchor = "Left"
$deselectallButton.add_Click($deselectallButton_OnClick)
## Add the buttons to the button panel
$buttonPanel.Controls.Add($okButton)
$buttonPanel.Controls.Add($cancelButton)
$buttonPanel.Controls.Add($selectallButton)
$buttonPanel.Controls.Add($deselectallButton)
## Add the button panel and list box to the form, and also set
## the actions for the buttons
$form.Controls.Add($listBox)
$form.Controls.Add($buttonPanel)
$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.Add_Shown( { $form.Activate() } )
## Show the form, and wait for the response
$result = $form.ShowDialog()
## If they pressed OK (or Enter,)
## enumerate list of plugins and rename those whose status has changed
if($result -eq "OK") {
$i = 0
ForEach ($plugin in $plugins) {
$oldname = $plugin.Name
$newname = $plugin.Plugin + $(If ($listbox.GetItemChecked($i)) {'.ps1'} else {'.ps1.disabled'})
If ($newname -ne $oldname) {
If (Test-Path (($plugin.FullName | Split-Path) + "\" + $newname)) {
Write-Warning "Attempting to rename ""$oldname"" to ""$newname"", which already exists - please delete or rename the superfluous file and try again"
} Else {
Rename-Item (($plugin.FullName | Split-Path) + "\" + $oldname) $newname
}
}
$i++
}
}