Powershell GUI fronted (WPF) to run categorized console scripts

⌈⌋ branch:  ClickyColoury


Check-in [74c2c89475]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Shorten starter.ps1 with `Is-PluginCompatible` function, scanning user plugins with standard scripts, and moving -Filter to separate script.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:74c2c894759aa562ef70b16f71465b6280bbf04a
User & Date: mario 2018-05-19 22:47:21
Context
2018-05-19
22:49
New menu/plugin which adds dummy entries for unclassified scripts. Leaf check-in: 168d091f87 user: mario tags: trunk
22:47
Shorten starter.ps1 with `Is-PluginCompatible` function, scanning user plugins with standard scripts, and moving -Filter to separate script. check-in: 74c2c89475 user: mario tags: trunk
2018-05-18
17:58
Attempt at making TreeViewItems more solid (metro/burger style menu) check-in: 16ea606fae user: mario tags: trunk
Changes

Changes to modules/menu.psm1.

174
175
176
177
178
179
180







181
182
183
184
185
186
187
...
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
        $meta.fn = "$fn"
        $meta.id = ($fn -replace "^.+[\\/]|\.\w+$","") -replace "[^\w]","_"
        $meta.doc = ($doc -join "`r`n")
    }
    return $meta  # or return as (New-Object PSCustomObject -Prop $meta)
}









#-- script header
function Init-Screen() {
    param($x=100,$y=45)
    #-- screen size // better than `mode con` as it retains scrolling:
    if ($host.Name -match "Console") {
        $con = $host.UI.RawUI
................................................................................
    try { $host.UI.RawUI.WindowTitle = $meta.title } catch { }
}

#-- group plugin list by category, sort by sort: / key: / title:
function Sort-Menu($menu) {
    $usort_cat = { (@{cmd=1; user=2; powershell=3; onbehalf=11; exchange=4; empirum=5; network=6; info=7; wmi=8}[$_.category], $_.category) -ne $null }
    $usort_key = { if ($_.key -match "(\d+)") { [int]$matches[1] } else { $_.key } }
    return ($menu | Sort-Object $usort_cat, {$_.sort}, $usort_key, {$_.title})
}

#-- string cutting
function substr($str, $from, $to) {
    if ($to -lt $str.length) {
        $str.substring($from, $to)
    }







>
>
>
>
>
>
>







 







|







174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
...
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
        $meta.fn = "$fn"
        $meta.id = ($fn -replace "^.+[\\/]|\.\w+$","") -replace "[^\w]","_"
        $meta.doc = ($doc -join "`r`n")
    }
    return $meta  # or return as (New-Object PSCustomObject -Prop $meta)
}


#-- general PMD check
function Is-PluginCompatible {
    Param($m)
    $m.type -and $m.title -and $m.category -and ($m.api -match "mt|multitool|clicky|ps|powershell|automat|^$")
}


#-- script header
function Init-Screen() {
    param($x=100,$y=45)
    #-- screen size // better than `mode con` as it retains scrolling:
    if ($host.Name -match "Console") {
        $con = $host.UI.RawUI
................................................................................
    try { $host.UI.RawUI.WindowTitle = $meta.title } catch { }
}

#-- group plugin list by category, sort by sort: / key: / title:
function Sort-Menu($menu) {
    $usort_cat = { (@{cmd=1; user=2; powershell=3; onbehalf=11; exchange=4; empirum=5; network=6; info=7; wmi=8}[$_.category], $_.category) -ne $null }
    $usort_key = { if ($_.key -match "(\d+)") { [int]$matches[1] } else { $_.key } }
    return ($menu | ? { $_.title -and $_.category } | Sort-Object $usort_cat, {$_.sort}, $usort_key, {$_.title})
}

#-- string cutting
function substr($str, $from, $to) {
    if ($to -lt $str.length) {
        $str.substring($from, $to)
    }

Changes to modules/starter.ps1.

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
}
$global:plugins = @{
    "init" = @()
    "before" = @()
    "after" = @()
    "menu" = @()
}


#-- load user config
cd ($cfg.multitool_base)
if (Test-Path ($cfg.user_config_fn)) {
    . ($cfg.user_config_fn)
}
if ($Debug) { $cfg|FL }


#-- restart if not in single-thread-apartment mode
if (-not [System.Management.Automation.Runspaces.Runspace]::DefaultRunSpace.ApartmentState -eq "STA") {
#    powershell.exe -STA -ExecutionPolicy unrestricted -File $curr_script_fn
#    break 2
}

#-- modules
$ENV:PSModulePath += ";$($cfg.multitool_base)\modules"
$global:GUI = [hashtable]::Synchronized(@{Host=$Host})
Import-Module -DisableNameChecking "$($cfg.multitool_base)\modules\guimenu.psm1"
Write-Splash "Loading Modules.." 30
Import-Module -DisableNameChecking "$($cfg.multitool_base)\modules\menu.psm1"
Import-Module -DisableNameChecking "$($cfg.multitool_base)\modules\clipboard.psm1"
if (!(Get-Module -Name ActiveDirectory)) { Import-Module ActiveDirectory }


#-- scan tools/plugins
Write-Splash "Scanning Plugins.." 150
$cfg.main = (Extract-PluginMeta $cfg.curr_script_fn)

#-- predefined menu entries
$menu = @(
)
#-- load cache?
if ($cfg.cached -and (Test-Path ($cfg.tools_cache_fn))) {
    $menu = Import-CliXml ($cfg.tools_cache_fn)
}
#-- add menu entries from scripts
else {
    $menu += @(Get-Item ($cfg.tool_dirs) | % { Extract-PluginMeta $_ } | ? { $_.api -and $_.type -and $_.title })
}
#-- filter entries (e.g. $Filter="info|user|wmi" to show only a subset of safe/read-only scripts)
if ($Filter) {
    $menu = $menu | ? { (($_.category, $_.fn, $_.description, $_.title) -match $Filter) } | ? { -not (($_.category, $_.fn, $_.description, $_.title) -match $NotFilter) }
}
#-- add user plugins
if (Test-Path ($cfg.user_plugins_d)) {
    $menu += @(Get-Item "$($cfg.user_plugins_d)/*.ps1" | % { Extract-PluginMeta $_ } | ? { $_.type -and ($_.api -match "mt|multitool|clicky|ps|powershell|automat|^$") -and $_.title })
}

#-- run `type:init` plugins here
Write-Splash "Init Plugins.." 55
$menu | ? { $_.type -eq "init" -and $_.fn } | % { 
  try {
    . $_.fn
  }
  catch {
    Write-Host -f Red "Failure init plugin: $($_.fn)"
    $PSItem | FL | Out-String | Write-Host -f Gray
  }
}


#-- CLI mode
if ($CLI) {
    Write-Splash "Done" 1000
    cls ; Init-Screen







>
>
|




|
>
|
<
|
<
<
|












|


|
|
<
<
<
<
<
<
<
|
|
<
<
<
<
<
<
<
<
<



|
|
|
|
|
|
|







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
}
$global:plugins = @{
    "init" = @()
    "before" = @()
    "after" = @()
    "menu" = @()
}


#-- add user config
cd ($cfg.multitool_base)
if (Test-Path ($cfg.user_config_fn)) {
    . ($cfg.user_config_fn)
}
if (Test-Path ($cfg.user_plugins_d)) {
    $cfg.tool_dirs += @($cfg.user_plugins_d+"/*.ps1")
}

if ($Debug) { $cfg|FL }




#-- modules
$ENV:PSModulePath += ";$($cfg.multitool_base)\modules"
$global:GUI = [hashtable]::Synchronized(@{Host=$Host})
Import-Module -DisableNameChecking "$($cfg.multitool_base)\modules\guimenu.psm1"
Write-Splash "Loading Modules.." 30
Import-Module -DisableNameChecking "$($cfg.multitool_base)\modules\menu.psm1"
Import-Module -DisableNameChecking "$($cfg.multitool_base)\modules\clipboard.psm1"
if (!(Get-Module -Name ActiveDirectory)) { Import-Module ActiveDirectory }


#-- scan tools/plugins
Write-Splash "Scanning Plugins.." 120
$cfg.main = (Extract-PluginMeta $cfg.curr_script_fn)

#-- collect entries
$menu = @()







$menu += @(Get-Item ($cfg.tool_dirs) | % { Extract-PluginMeta $_ } | ? { Is-PluginCompatible $_ })










#-- run `type:init` plugins here
Write-Splash "Init Plugins.." 55
$menu | ? { $_.type -eq "init" -and $_.fn } | % { 
    try {
        . $_.fn
    }
    catch {
        Write-Host -f Red "Failure init plugin: $($_.fn)"
        $PSItem | FL | Out-String | Write-Host -f Gray
    }
}


#-- CLI mode
if ($CLI) {
    Write-Splash "Done" 1000
    cls ; Init-Screen

Added tools/plugins/menu_filter.ps1.





































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# api: multitool
# type: init
# title: apply $menu -Filter
# description: handles cmdline args -Filter and -NotFilter
# version: 0.1
# category: core
# priority: core
# status: stable
#
# Removes entries from global $menu[] if the fit -Filter parameter. Which is
# used for "ClickyInfo" starter.
#
# e.g. $Filter="info|user|wmi" to show only a subset of safe/read-only scripts


if ($Filter) {
    $menu = $menu | ? { (($_.category, $_.fn, $_.description, $_.title) -match $Filter) } | ? { -not (($_.category, $_.fn, $_.description, $_.title) -match $NotFilter) }
}