tools.winrm_tools module
WinRM and PowerShell remoting (WS-Man) tools for remote Windows management.
Uses pywinrm (HTTP/HTTPS to ports 5985/5986). Per-user named sessions, same ergonomics as ssh_* tools. Requires UNSANDBOXED_EXEC.
- async tools.winrm_tools.check_unsandboxed_exec(ctx)[source]
Gate a WinRM operation behind the
UNSANDBOXED_EXECprivilege.Verifies that the calling user is allowed to run arbitrary remote commands. Every WinRM tool handler calls this first and returns its result verbatim when it is non-
None, short-circuiting the operation.Interactions: imports and calls
tools.alter_privileges.has_privilege()withPRIVILEGES["UNSANDBOXED_EXEC"], readingctx.redis,ctx.configandctx.user_idoff the tool context (the privilege check itself consults the Redis-backed privilege store). On denial it logs aSECURITYwarning naming the user. This helper is also imported and reused by the sibling Windows tool modulestools.ad_dns_tools,tools.ad_gpo_toolsandtools.agpm_tools.Called by: every handler in this module (
_winrm_connect,_winrm_run_ps,_winrm_run_cmd,_winrm_save_credentials, etc.) and by the AD/GPO/AGPM tool handlers listed above.- Parameters:
ctx (
Any) – The activeToolContext(or any object exposingredis,configanduser_idattributes).- Return type:
- Returns:
Nonewhen the user holds the privilege (proceed). Otherwise a JSON string withsuccess: Falsedescribing either the missing privilege or that the privilege system is unavailable.
- tools.winrm_tools.resolve_owned_winrm_session(connection_name, user_id)[source]
Look up a WinRM session and enforce that the caller owns it.
Central ownership-and-existence gate shared by the run handlers: it confirms that
connection_nameis present in the registry and was opened byuser_idbefore any command is allowed to run on it, returning a ready-to-send JSON error string (rather than raising) when it is not.Interactions: reads the module-level
_winrm_sessionsregistry, listing the user’s own connection names via_user_sessions()for a helpful error, and checks ownership with_is_owner(); no network or Redis use. Called byget_owned_winrm_session()and_winrm_run_ps()here, and reused by the sibling Windows tool modulestools.ad_gpo_tools,tools.ad_dns_toolsandtools.agpm_tools.- Parameters:
- Returns:
(session, None)when the named session exists and is owned by the caller; otherwise(None, json_error_string)whose message distinguishes an unknown name (listing the user’s connections, or noting there are none) from a connection owned by someone else.- Return type:
- tools.winrm_tools.get_owned_winrm_session(connection_name, user_id)[source]
Return the owned pywinrm session, discarding the error string.
Thin convenience wrapper over
resolve_owned_winrm_session()for callers that only want the session object and treat any failure (unknown name or wrong owner) uniformly asNone.Interactions: delegates entirely to
resolve_owned_winrm_session()(and thus to the_winrm_sessionsregistry,_user_sessions()and_is_owner()); no other side effects. No callers were found in the repo, so this is a public convenience entry point retained for sibling tool modules and tests.
- async tools.winrm_tools.execute_winrm_ps(session, script, timeout)[source]
Run a PowerShell script on a connected WinRM session without blocking.
Async wrapper around pywinrm’s blocking
run_ps: it derives the WS-Man read/operation timeouts fromtimeout, off-loads the synchronous remote call to a worker thread, and normalises the result (or any failure) into a plain dict so handlers never have to catch exceptions themselves.Interactions: invokes the remote Windows host’s PowerShell over HTTP/HTTPS by running the nested
_runclosure throughasyncio.to_threadunder anasyncio.wait_forguard; decodes the returnedstd_out/std_errbytes with_decode_out(); logs an exception via the moduleloggeron error. Called by_winrm_run_ps()here and by the AD/GPO/AGPM tool handlers intools.ad_gpo_tools,tools.ad_dns_toolsandtools.agpm_tools.- Parameters:
session (
Any) – A connected pywinrmSession(typically fromresolve_owned_winrm_session()).script (
str) – The PowerShell script text to execute remotely.timeout (
int) – Maximum execution time in seconds; the WS-Man and outerwait_fortimeouts are derived from it with margins.
- Returns:
{"success": True, "stdout": ..., "stderr": ..., "status_code": ...}on success, or{"success": False, "error": ...}on a timeout or any pywinrm exception.- Return type: