# Master PowerShell script launcher # Windows PowerShell 5.1 / ConstrainedLanguage compatible. # Serve as UTF-8 text; designed to run via: irm scripts.witten.se | iex # Directory listings must expose relative HTML links (e.g. test2/ and HelloWorld). $LauncherVersion = 'v2026-10-30-03-CLM' $BaseUrl = 'https://files.dayv.se/work/pwsh/' $CurrentFolders = @() $BoxWidth = 72 $ProgressPreference = 'SilentlyContinue' function Show-Line { param([string]$Text, [string]$Color = 'Cyan') if ($Text.Length -gt $BoxWidth) { $Text = $Text.Substring(0, $BoxWidth) } $Format = '{0,-' + $BoxWidth + '}' Write-Host ('| ' + ($Format -f $Text) + ' |') -ForegroundColor $Color } function Show-Box { param([string[]]$Lines, [string]$Color = 'Cyan') $Edge = '+' + ('-' * ($BoxWidth + 2)) + '+' Write-Host '' Write-Host $Edge -ForegroundColor $Color foreach ($Line in $Lines) { if ($Line -eq '__SEP__') { Write-Host $Edge -ForegroundColor $Color } else { Show-Line -Text $Line -Color $Color } } Write-Host $Edge -ForegroundColor $Color } function Read-Directory { param([string]$Url) # UseBasicParsing avoids the old Internet Explorer HTML parser in PS 5.1. # In ConstrainedLanguage, no custom .NET types or custom objects are needed. $Response = Invoke-WebRequest -Uri $Url -UseBasicParsing -ErrorAction Stop $Html = [string]$Response.Content $FolderNames = @() $FileNames = @() # Accept href="Name", href='Name', href="Folder/". # Deliberately ignore absolute URLs, parent links and query strings. $Links = Select-String -InputObject $Html -Pattern '(?is)\bhref\s*=\s*["'']([^"''#?]+)["'']' -AllMatches foreach ($Link in @($Links.Matches)) { $Href = [string]$Link.Groups[1].Value $Href = $Href -replace '^\./', '' if ($Href -notmatch '^([a-zA-Z0-9_ .()%+~-]+)(/?)$') { continue } $Name = [string]$Matches[1] $IsFolder = ([string]$Matches[2] -eq '/') if ($Name -eq '.' -or $Name -eq '..') { continue } if ($Name -match '(?i)%2e|%2f|%5c') { continue } if ($IsFolder) { if ($FolderNames -notcontains $Name) { $FolderNames += $Name } } else { # Exclude unrelated file types, but allow extensionless PS scripts. if ($Name -match '\.[^.]+$' -and $Name -notmatch '(?i)\.ps1$') { continue } if ($FileNames -notcontains $Name) { $FileNames += $Name } } } $FolderNames = @($FolderNames | Sort-Object) $FileNames = @($FileNames | Sort-Object) # Return only basic arrays of strings. No [pscustomobject] conversions. # Prefix D:/F: so the caller can distinguish folders from scripts. $Results = @() foreach ($Folder in $FolderNames) { $Results += ('D:' + $Folder) } foreach ($File in $FileNames) { $Results += ('F:' + $File) } return $Results } while ($true) { $CurrentUrl = $BaseUrl $FolderPath = '/pwsh/' if ($CurrentFolders.Count -gt 0) { $Path = $CurrentFolders -join '/' $CurrentUrl = $BaseUrl + $Path + '/' $FolderPath = '/pwsh/' + $Path + '/' } try { $Items = @(Read-Directory -Url $CurrentUrl) } catch { Show-Box -Lines @('ERROR: Cannot list folder', ('URL: ' + $CurrentUrl), ([string]$_)) -Color 'Red' if ($CurrentFolders.Count -eq 0) { break } if ($CurrentFolders.Count -eq 1) { $CurrentFolders = @() } else { $CurrentFolders = @($CurrentFolders[0..($CurrentFolders.Count - 2)]) } continue } $Menu = @() $Menu += (' Launcher ' + $LauncherVersion) $Menu += (' Folder ' + $FolderPath) $Menu += '__SEP__' $Menu += ' Available items' $Menu += '' if ($Items.Count -eq 0) { $Menu += ' (No scripts or subfolders found)' } else { for ($i = 0; $i -lt $Items.Count; $i++) { $Item = [string]$Items[$i] $Name = $Item.Substring(2) if ($Item -like 'D:*') { $Menu += (' {0,2}. [DIR] {1}/' -f ($i + 1), $Name) } else { $Menu += (' {0,2}. [SCRIPT] {1}' -f ($i + 1), $Name) } } } $Menu += '' $Menu += '__SEP__' if ($CurrentFolders.Count -gt 0) { $Menu += ' Number/name = open 0/b = back r = refresh q = quit' } else { $Menu += ' Number/name = open r = refresh q = quit' } Show-Box -Lines $Menu -Color 'Cyan' $Choice = ([string](Read-Host 'Run')).Trim() if ($Choice -eq '') { continue } if ($Choice -ieq 'q') { break } if ($Choice -ieq 'r') { continue } if ($Choice -eq '0' -or $Choice -ieq 'b' -or $Choice -eq '..') { if ($CurrentFolders.Count -eq 0) { Show-Box -Lines @('Already at the root folder.') -Color 'Yellow' } elseif ($CurrentFolders.Count -eq 1) { $CurrentFolders = @() } else { $CurrentFolders = @($CurrentFolders[0..($CurrentFolders.Count - 2)]) } continue } $Selected = '' if ($Choice -match '^[0-9]+$') { $Number = [int]$Choice if ($Number -ge 1 -and $Number -le $Items.Count) { $Selected = [string]$Items[$Number - 1] } } else { $Lookup = $Choice -replace '/$', '' foreach ($Item in $Items) { $Name = ([string]$Item).Substring(2) if ($Name -ieq $Lookup) { $Selected = [string]$Item; break } } } if ($Selected -eq '') { Show-Box -Lines @('Invalid selection: ' + $Choice) -Color 'Yellow' continue } $SelectedName = $Selected.Substring(2) if ($Selected -like 'D:*') { $CurrentFolders += $SelectedName continue } $ScriptUrl = $CurrentUrl + $SelectedName Show-Box -Lines @('RUN: ' + $FolderPath + $SelectedName, ('URL: ' + $ScriptUrl)) -Color 'Green' try { $ScriptText = Invoke-RestMethod -Uri $ScriptUrl -ErrorAction Stop if ($ScriptText -isnot [string]) { throw 'The server did not return a plain-text script.' } if ($ScriptText -match '(?is)^\s*(