-
Notifications
You must be signed in to change notification settings - Fork 0
/
Select-Plugins.ps1
116 lines (97 loc) · 4.18 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
# Select-Plugins.ps1
# selectively enable / disable sCheck 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 sCheck 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 sCheck as normal.
# Great for testing plugins. When done, untick it...
# If run as a plugin, it will affect the next sCheck run, not the current one,
# as sCheck 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
$Title = "Plugin Selection Plugin"
$Author = "Phil Randal"
$PluginVersion = 2.0
$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 -Recurse | where {$_.name -match '.*\.ps1(?:\.disabled|)$'} |
Sort Directory, Name |
Select Name,
@{Label="Plugin";expression={$_.Directory.Name + " - " + $_.Name -replace '(.*)\.ps1(?:\.disabled|)$', '$1'}},
@{Label="Enabled";expression={$_.Name -notmatch '.*\.disabled$'}}
## 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"
## Add the buttons to the button panel
$buttonPanel.Controls.Add($okButton)
$buttonPanel.Controls.Add($cancelButton)
## 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 ($PluginPath + "\" + $newname)) {
Write-Host "Attempting to rename ""$oldname"" to ""$newname"", which already exists - please delete or rename the superfluous file and try again"
} Else {
Rename-Item ($PluginPath + "\" + $oldname) $newname
}
}
$i++
}
}