| 1 |
# Test plugins: run PowerShell on the controller (Diátaxis)<!-- omit in toc --> |
| 2 |
|
| 3 |
These test-only plugins provide a custom connection and shell layer to run Ansible PowerShell modules directly on the controller with `pwsh` (Linux/macOS/Windows) without modifying module code. |
| 4 |
|
| 5 |
- [Tutorials (start here)](#tutorials-start-here) |
| 6 |
- [How-to guides (recipes)](#how-to-guides-recipes) |
| 7 |
- [Fix SystemPolicy errors on Linux/macOS (ansible-core 2.19+)](#fix-systempolicy-errors-on-linuxmacos-ansible-core-219) |
| 8 |
- [Fall back to remote Windows execution](#fall-back-to-remote-windows-execution) |
| 9 |
- [Reference (what it is)](#reference-what-it-is) |
| 10 |
- [Explanation (background, rationale)](#explanation-background-rationale) |
| 11 |
- [Background](#background) |
| 12 |
- [Compatibility notes (ansible-core 2.19+)](#compatibility-notes-ansible-core-219) |
| 13 |
- [Scope and impact](#scope-and-impact) |
| 14 |
- [Troubleshooting](#troubleshooting) |
| 15 |
|
| 16 |
## Tutorials (start here) |
| 17 |
|
| 18 |
Goal: run collection integration tests that exercise PowerShell modules locally on the controller via `pwsh`. |
| 19 |
|
| 20 |
Steps (high level): |
| 21 |
|
| 22 |
- Ensure `pwsh` (PowerShell 7+) is installed on the controller. |
| 23 |
- Run the collection's integration tests; this target auto-enables the custom plugins in this folder. |
| 24 |
- Tests execute PowerShell modules on the controller, while those modules still manage remote SQL Server hosts as usual. |
| 25 |
|
| 26 |
Notes: |
| 27 |
|
| 28 |
- These plugins are scoped to this integration test target and are not installed with the collection. |
| 29 |
- Prefer a Windows host if you want the standard, remote PowerShell execution path. |
| 30 |
|
| 31 |
## How-to guides (recipes) |
| 32 |
|
| 33 |
### Fix SystemPolicy errors on Linux/macOS (ansible-core 2.19+) |
| 34 |
|
| 35 |
Nothing for you to change: the test shell plugin (`shell_plugins/pwsh.py`) automatically injects a small compatibility stub before any PowerShell code runs. If you still hit errors: |
| 36 |
|
| 37 |
- Confirm this test shell plugin is active for your run (this target enables it by default). |
| 38 |
- Ensure the script is sent with `-EncodedCommand` (not stdin), which the plugin uses by default. |
| 39 |
|
| 40 |
### Fall back to remote Windows execution |
| 41 |
|
| 42 |
If local controller execution isn't desired, point your playbooks at a Windows host so modules run remotely as usual. |
| 43 |
|
| 44 |
## Reference (what it is) |
| 45 |
|
| 46 |
Components in this target: |
| 47 |
|
| 48 |
- `shell_plugins/pwsh.py` — custom PowerShell shell plugin. |
| 49 |
- Encodes scripts with `-EncodedCommand` and prepends a compatibility stub when needed. |
| 50 |
- For ansible-core < 2.13 includes a local bootstrap path to load test module utils for cross-platform use. |
| 51 |
- `connection_plugins/` — connection helper used in these tests. |
| 52 |
- `module_utils/` — local test-only helpers used by the bootstrap path for older ansible-core. |
| 53 |
|
| 54 |
Behavior/Options (subset): |
| 55 |
|
| 56 |
- Typical PowerShell shell options such as `async_dir`, `remote_tmp`, `environment` (see the plugin's `DOCUMENTATION` block). |
| 57 |
- No persistent user profiles or global state are modified. |
| 58 |
|
| 59 |
## Explanation (background, rationale) |
| 60 |
|
| 61 |
### Background |
| 62 |
|
| 63 |
PowerShell modules in this collection are typically run on a remote Windows host. For CI and developer convenience, we sometimes run the modules on the controller via `pwsh` instead. |
| 64 |
|
| 65 |
### Compatibility notes (ansible-core 2.19+) |
| 66 |
|
| 67 |
Starting with ansible-core 2.19.0b5, the internal PowerShell wrappers reference `[SystemPolicy]` very early (e.g., `SystemPolicy.ExecutionPolicy`, `SystemPolicy::GetSystemLockdownPolicy()`). PowerShell Core on Linux/macOS does not provide that type. |
| 68 |
|
| 69 |
The test shell plugin injects a minimal stub only when `[SystemPolicy]` is missing: |
| 70 |
|
| 71 |
- Defines `SystemPolicy` with an `ExecutionPolicy` enum (Bypass, Unrestricted, RemoteSigned, AllSigned, Restricted). |
| 72 |
- Provides a static `GetSystemLockdownPolicy()` that returns `"None"` (so checks like `-eq 'Enforce'` remain false). |
| 73 |
- On Windows PowerShell, the real `[SystemPolicy]` exists; the stub is not injected. |
| 74 |
|
| 75 |
### Scope and impact |
| 76 |
|
| 77 |
- Stub is local to these integration tests; it does not modify ansible-core or collection modules. |
| 78 |
- Behavior for ansible-core < 2.19 is unchanged; the stub is a no-op if not needed. |
| 79 |
|
| 80 |
### Troubleshooting |
| 81 |
|
| 82 |
- If you see CLIXML errors mentioning `[SystemPolicy]`, ensure this test shell plugin is active and that scripts are sent via `-EncodedCommand`. |
| 83 |
- Editor warnings about unresolved `ansible.*` imports inside the plugin are local environment issues and do not affect runtime under ansible-core. |
| 84 |
|
| 85 |
Supportability: |
| 86 |
|
| 87 |
- These plugins are experimental and test-scoped. They avoid changing collection modules and keep behavior deterministic for CI. |