Powershell GUI fronted (WPF) to run categorized console scripts

โŒˆโŒ‹ โŽ‡ branch:  ClickyColoury


Artifact [41951f00f1]

Artifact 41951f00f18d96de35abe0c284629fae4af9e2f1:

  • File modules/starter.ps1 — part of check-in [b84bc30494] at 2018-04-10 15:27:24 on branch trunk — Add $menu -Filter support (for restricted wrappers). (user: mario size: 4420)

# api: ps
# type: main
# title: ClickyColoury + TextyTypey - GUI+CLI tool frontend
# description: Convenience invocation of various Powershell and CMD scripts
# url: http://fossil.include-once.org/clickycoloury/
# version: 0.9.0
# depends: menu, wpf, clipboard
# category: misc
# config:
#   { name: cfg.gridview, type: select, value: Format-Table, select: Format-Table|Out-GridView, description: default table display mode }
#   { name: cfg.cli, type: bool, value: 0, description: Start console (CLI) version per default? }
#   { name: cfg.cached, type: bool, value: 0, description: Use CLIXML script cache on startup }
# author: mario
# license: MITL
# priority: core
# status: stable
#
# Note that this is the WindowsPresentationForm version, but also renders a
# classic -CLI menu otherwise. Utilizes:
#
#   ยท guimenu.psm1 = graphical toolkit features
#   ยท menu.psm1 = mostly CLI features
#   ยท clipboard.psm1 = for the HTML output
#
# Whereas scripts (and plugins) reside in tools*/*.ps1
#
# Starting up with `-cli` parameter should yield the text version.
#
# Requires powershell.exe -STA -File ... startup (for GUI mode).
#


#-- params
[CmdletBinding()]
Param(
    [switch]$CLI = $false,
    $Filter = ".*", $NotFilter = "admin"   # to cut down $menu list ("ClickyInfo")
)

#-- config
$global:cfg = @{
    domain = "WORKWORKWORKWORKWORK"  # (Get-ADDomain).Netbiosname
    threading = 1
    autoclear = 300
    exchange = @{
        ConfigurationName = "Microsoft.Exchange"
        ConnectionUri = "http://YOUREXCHANGESRV/psremote/"
    }
    main = @{} 
    theme = "dark"
    curr_script_fn = $MyInvocation.MyCommand.Path
    multitool_base = (Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path))
    user_config_fn  = "$env:APPDATA\multitool\config.ps1"
    user_plugins_d  = "$env:APPDATA\multitool"
    tools_cache_fn = "./data/tools.cache.clixml"
    tool_dirs = @("./tools/*/*.ps1")  # add custom dirs here!
}
$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
$global:GUI = [hashtable]::Synchronized(@{Host=$Host})
Import-Module -DisableNameChecking "$($cfg.multitool_base)\modules\guimenu.psm1"
Write-Splash "Loadiing 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) -notmatch $NotFilter }
}
#-- add user plugins
if (Test-Path ($cfg.user_plugins_d)) {
    $menu += @(Get-Item "$($cfg.user_plugins_d)/*.ps1" | % { Extract-PluginMeta $_ } | ? { $_.type -and $_.api -eq "multitool" })
}
#-- run `type:init` plugins here
Write-Splash "Init Plugins.." 55
$menu | ? { $_.type -eq "init" -and $_.fn } | % { . $_.fn }


#-- CLI mode
if ($CLI) {
    Write-Splash "Done" 1000
    cls ; Init-Screen
    $menu = $menu + @{key="m|menu"; category="extras"; title="print menu"; func='Print-Menu $menu'}
    Print-Menu $menu
    Process-Menu -Menu $menu -Prompt "Typey"
}
#-- WPF multi-tool
else {
    Write-Splash "Starting GUI version.." 50
    $cfg.main.title = "Clicky"
    $shell = Start-Win (Sort-Menu $menu)
}

#-- cleanup
if ($Debug) {
    Remove-Module wpf
    $Error
}