Introduction¶
When invoking PowerShell on Windows through DSH’s ops_bash, you’ll encounter a series of “small issues” caused by environment differences: quotes being consumed by bash, $env:TEMP turning into :TEMP, 2>/dev/null producing errors, and error exit codes returning 0. The root cause of all these problems is that DSH executes commands via bash, while the default powershell/powershell.exe on Windows is version 5.1, which lacks the syntax and encoding options available in PowerShell 7.
dsh-ps51-guard is a plugin designed to correct these issues before commands reach PowerShell. It intercepts calls that ultimately resolve to 5.1 within the bash process, providing command transport protection, compatibility rewrites, and result normalization, along with a built-in, disableable powershell → pwsh 7 redirect.
Plugin Overview¶
- Name: dsh-ps51-guard
- Maintainer: TLEWA
- Category: admin-security
- Version: 0.1.0
- License: MIT
It provides command transport protection, syntax compatibility rewrites, and execution result normalization for Windows PowerShell 5.1 invoked via ops_bash. By default, it redirects calls that resolve to 5.1 to pwsh 7; this can be disabled via an environment variable when 5.1 is required.
Core Features¶
1. Command Transport Protection and Rewriting¶
The plugin intercepts 5.1 calls within the bash process and works through the following mechanisms:
- Sets
BASH_ENVwithin the DSH process to point to the bundledbash-env.sh, while chaining the originalBASH_ENVwithout overwriting it - Wraps
subprocess.spawnto inject a shim on every bash launch, linking the originalBASH_ENVinto the chain - The shim uses bash’s
DEBUGtrap to read the raw$BASH_COMMAND(before expansion) and only activates the guardrails when the command word ultimately resolves to 5.1 and hasn’t been redirected lib/client.jsparses the raw command, restores values that bash expanded per argv, applies rewrite rules, then hands off to the resident host
2. Compatibility Rewrite Rules¶
The plugin includes 10 built-in rewrite rules and prints [ps51] A → B to stderr so the model knows the command was adjusted:
| Rule | Trigger | Behavior |
|---|---|---|
| R1 | $name inside double quotes of -Command |
Bash variables expand per bash rules; PowerShell variables ($env:TEMP, etc.) are preserved as literals |
| R2 | 2>/dev/null |
Rewritten to 2>$null |
| R3 | export FOO=bar |
Rewritten to $env:FOO = 'bar' |
| R4 | curl |
Rewritten to curl.exe (to avoid the PowerShell alias) |
| R5 | Join-Path a b c |
Rewritten to nested Join-Path (Join-Path a b) c |
| R6 | ls -la, rm -rf |
Rewritten to Get-ChildItem -Force, Remove-Item -Recurse -Force |
| R7 | -Encoding utf8NoBOM |
Advisory only (5.1’s parameter set does not accept this value) |
| R8 | > file within PowerShell |
Advisory only (5.1 writes UTF-16LE with BOM; use .NET APIs for BOM-free output) |
| R9 | &&, \|\| |
Advisory suggesting the alternative a; if ($?) { b } |
| R10 | ??, ?: |
Advisory suggesting the equivalent if/else construct |
High-risk rules (R7–R10) and R8’s advisory are only emitted when necessary.
3. Resident Host and Result Normalization¶
The plugin uses a resident PowerShell 5.1 host (named pipe, 4-byte little-endian length prefix + JSON frames) to execute commands:
- stdout/stderr are returned as raw bytes (including output from
[Console]::Out/Error) - stdin is provided to the script via
[Console]::In - Exit code is taken from
$?of the script’s last command, consistent with-Commandbehavior - Falls back to the original invocation if the host is unavailable during the connection phase; host failures after a request has been sent only report an error and exit (exit code 125) without replaying
- When the client disconnects or
ops_bashis cancelled, the host detects it within 100 ms and exits; long-running commands in progress will not continue executing in the background
4. powershell → pwsh 7 Redirect¶
Starting from milestone 0.2, the plugin includes the redirect previously provided by dsh-pwsh7-bash, enabled by default. Set PS51_PWSH7=0 when 5.1 is required. After migration, the old plugin should be removed to prevent both plugins from competing over BASH_ENV.
5. Interception Scope¶
- By default,
powershell/powershell.exeis redirected topwsh 7; whenPS51_PWSH7=0, guardrails are enabled for calls that resolve to 5.1 instead - Both the redirect and guardrails are only installed when the command word ultimately resolves to 5.1 (or the command does not yet exist) and the
command -vresult matches a known 5.1 path; wrapper scripts, aliases, other plugins, or user-defined functions inPATHare not overridden pwshis passed through directly; absolute path invocations (/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe ...) and calls insidecmd /ccannot be intercepted
Configuration¶
The plugin provides the following environment variables:
| Variable | Default | Purpose |
|---|---|---|
PS51_GUARD |
1 |
Set to 0 to completely disable guardrails and redirect, restoring the original invocation |
PS51_PWSH7 |
1 |
Set to 0 to disable the powershell → pwsh 7 redirect, keeping the 5.1 guardrails |
PS51_PWSH7_PATH |
Auto-detect | Specify the path to pwsh.exe |
PS51_SPAWN_GUARD |
1 |
Set to 0 to disable subprocess.spawn injection, retaining only the process-level BASH_ENV (later-loaded plugins may override it) |
PS51_MODE |
rewrite |
annotate only advises without rewriting; off passes through directly |
PS51_STRICT |
0 |
Set to 1 so that commands hitting advisory-only rules (R7–R10) are blocked with exit code 2 |
PS51_VERBOSE |
0 |
Set to 1 to output all advisories, including low-priority notes like R8 |
PS51_DIAGNOSTICS |
1 |
Set to 0 to stop writing diagnostic logs |
PS51_SPAWN_GUARD only affects subprocess.spawn injection: the bridge only handles bash -c or spawns that already carry a non-empty BASH_ENV; if the caller explicitly sets BASH_ENV to an empty string, it is treated as an intentional disable. PS51_GUARD, PS51_PWSH7, and PS51_SPAWN_GUARD are all read at DSH process startup; a DSH restart is required after changes.
Diagnostic logs are written to %TEMP%\dsh-ps51-guard-<username>\session-<pid>\diagnostics.log (JSON lines, including matched rules and before/after commands). It is recommended to disable logging after troubleshooting. Setting PS51_TRACE=1 enables the host to additionally write host-trace.log.
Installation and Enablement¶
Install with the following command:
dsh plugin add github:TLEWA/dsh-ps51-guard
After installation, the plugin enables guardrails and redirect by default. If you need 5.1, set the environment variable before starting DSH:
set PS51_PWSH7=0
dsh start
To disable everything, set:
set PS51_GUARD=0
dsh start
All configuration values are read at DSH process startup; a restart is required after changes.
Typical Usage¶
Here is a simple usage example: suppose the model generates ls -la. The plugin rewrites it to Get-ChildItem -Force and prints [ps51] ls -la → Get-ChildItem -Force to stderr, letting the model know the command was adjusted.
For export FOO=bar, the plugin rewrites it to $env:FOO = 'bar'; for 2>/dev/null, it rewrites to 2>$null.
If the model generates -Encoding utf8NoBOM, the plugin advises that this parameter is not accepted in 5.1 and suggests using .NET APIs instead.
Applicable Scenarios and Caveats¶
- Suited for invoking PowerShell on Windows via
ops_bash, especially in environments that need 5.1 compatibility - The plugin runs with the current DSH process privileges; review the source code and license before installing
- Known limitations: the redirect is installed as a function at bash startup;
command powershell, absolute paths, andcmd /cbypass the redirect; only-Command/-cpayloads are processed; scripts containing theexitkeyword run in a one-shot process; each command requires anodeclient launch
Conclusion¶
dsh-ps51-guard solves the problems of command transport protection, compatibility rewriting, and result normalization by intercepting PowerShell 5.1 calls within the bash process, while also providing a disableable powershell → pwsh 7 redirect. It is well-suited for scenarios where stable ops_bash invocation of PowerShell on Windows is required.
- Directory page: https://www.skillhub.cn/plugins/TLEWA/dsh-ps51-guard
- GitHub: https://github.com/TLEWA/dsh-ps51-guard