Skip to content

Use powershell for non-localized returns#68826

Open
twangboy wants to merge 3 commits intosaltstack:3006.xfrom
twangboy:fix/58361/3006.x
Open

Use powershell for non-localized returns#68826
twangboy wants to merge 3 commits intosaltstack:3006.xfrom
twangboy:fix/58361/3006.x

Conversation

@twangboy
Copy link
Copy Markdown
Contributor

What does this PR do?

The problem is that the return values for netsh are localized to the language of the system they are run on. This is problematic for any system other than English. PowerShell objects, however, are never localized. Using the ConvertTo-Json cmdlet forces the PowerShell object to be converted to text and is always in English.

This also creates a context manager for the PowerShell session. This allows us to run multiple commands without launching PowerShell.exe each time. The session is cleaned up when the context closes.

What issues does this PR fix or reference?

Fixes #58361

Previous Behavior

Remove this section if not relevant

New Behavior

Remove this section if not relevant

Merge requirements satisfied?

[NOTICE] Bug fixes or features added to Salt require tests.

Commits signed with GPG?

Yes

@twangboy twangboy added this to the Sulpher v3006.24 milestone Mar 17, 2026
@twangboy twangboy self-assigned this Mar 17, 2026
@twangboy twangboy requested a review from a team as a code owner March 17, 2026 19:56
@twangboy twangboy added the test:full Run the full test suite label Mar 17, 2026
The problem is that the return values for netsh are localized to the
language of the system they are run on. This is problematic for any
system other than English. PowerShell objects, however, are never
localized. Using the ConvertTo-Json cmdlet forces the PowerShell object
to be converted to text and is always in English.

This also creates a context manager for the powershell session which
lets us run multiple commands without launching powershell.exe each
time. The session is cleaned up when the context closes.
Replace netsh-based implementations with PowerShell .NET SDK calls
(via pythonnet) to eliminate locale-dependent output parsing.

Key changes:
- win_ip.py: add __virtual__ checks for HAS_CLR and HAS_PWSH_SDK;
  rename _get_interfaces_netsh to _get_interfaces_legacy_format;
  add _normalize_gateway_fields to handle PS 5.1 single-element array
  unwrap bug; add get_interface_new, get_interface_index, set_interface,
  get_default_gateway; refactor set_static_ip, set_dhcp_ip, set_dhcp_dns,
  set_dhcp_all, list_interfaces to use PowerShellSession; wrap CIM cmdlet
  GET calls (Get-NetRoute, Get-NetIPAddress) in try/catch to suppress
  terminating errors when no objects exist; fix docstrings throughout

- win_pwsh.py: fix HAS_CLR flag; add ClearStreams() before each Invoke();
  add run_strict() with sentinel-variable approach to distinguish caught
  errors (script ran to completion) from uncaught errors (script terminated
  mid-way); add class-level docstring to PowerShellSession; expand
  __init__ docstring

- test_win_ip.py: add functional tests covering set_interface,
  get_interface_new, set_static_ip, set_dhcp_ip/dns/all,
  get_default_gateway, protocol binding toggles, and forwarding;
  add _normalize_gateway_fields and default_static fixtures

Fixes: saltstack#58361
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant