Skip to main content

Wrapper around subprocess to execute powershell commands locally from python code and retrieve its result

Project description

powershell-commands-wrapper

A Python wrapper around subprocess to run PowerShell commands and retrieve their outputs.

Tested on Windows only. The package may work on Linux with PowerShell (pwsh) installed, but this is not tested.

Installation

pip install powershell-commands-wrapper

Usage

Commands are expressed as a list of strings — the first element is the cmdlet name, followed by its arguments.

import powershell

stdout = powershell.run(["Write-Host", "hello world"])
# stdout = "hello world"

JSON output

Pass as_json=True to retrieve the output as a JSON object:

import powershell

disks = powershell.run(["Get-Disk"], as_json=True)
# disks = [{"Size": 10737418240, "DiskNumber": 1, "PartitionStyle": "MBR", ...}]

Error handling

By default a powershell.PowerShellError is raised when the exit code is non-zero. Set raise_on_error=False to suppress this:

try:
    powershell.run(["Some-Command"])
except powershell.PowerShellError as e:
    print(e.result.returncode)
    print(e.result.stderr)

# Or ignore errors entirely
powershell.run(["Some-Command"], raise_on_error=False)

About Nuabee

This package is maintained by Nuabee, a company that helps businesses improve their IT resiliency.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

powershell_commands_wrapper-0.1.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

powershell_commands_wrapper-0.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file powershell_commands_wrapper-0.1.0.tar.gz.

File metadata

  • Download URL: powershell_commands_wrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for powershell_commands_wrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fc9d38ff6040d01950560429acfd6b40ffd66856287401e90851fefe5cc2fe22
MD5 a0fc81797173efefb025162089d413b4
BLAKE2b-256 7f5974a3a2e5a0a204fda91c08481ae82c38e49485355d685468ce62a1c55709

See more details on using hashes here.

File details

Details for the file powershell_commands_wrapper-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: powershell_commands_wrapper-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for powershell_commands_wrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0cd11e2edbb2d4281c040cce9fd92e89652af1cbf5c6d31543b6887ee3398f3b
MD5 ee4fd8223a60952ffec4ac7032e22f24
BLAKE2b-256 cc49b50bd2cca408d60cb142e931266dd9c56e2cb943ef85b6c5c3e39284474d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page