Interact with Powershell from Python
Project description
pxpowershell
This module lets you interact with a Powershell Prompt from Python. It is intended to provide similar functionality to "pxssh" or "redexpect" but for Powershell.
Why not subprocess? Subprocess does something different. It executes a single process and captures the output. pxpowershell lets you enstantiate a powershell process and then interact with it. Variables are saved form command to command. Functions defined in one command can be called later and so forth.
Usage
x = pxpowershell()
x.start_process()
x.run("$a = 10000")
print(x.run("$a + 1"))
result = x.run("get-process")
print(result)
x.stop_process()
Installing
I recommending setuping up a virtual machine (although not required)
c:\> py -m venv \path\to\new\venv\folder
c:\> \path\to\new\venv\folder\bin\activate.bat
Then pip install:
c:\> py -m pip install git+https://github.com/markbaggett/pxpowershell
Example
If you used a virtual environment so the install can run properly you now have a new command line utility called "dir2iso" installed:
(pxpowershell) C:\Users\User\Documents\GitHub\pxpowershell>dir2iso --help
usage: dir2iso [-h] source destination title
positional arguments:
source The path to the directory to turn into an ISO
destination The destination ISO file to create (including path).
title The title of the ISO.
optional arguments:
-h, --help show this help message and exit
This utility will create a .ISO file containing the specified directory. This is done using a demonstration script to shows how easily you can interact with Powershell. The Powershell Script that does all the real work came from @wikijm. Buy him a coffee.
https://github.com/wikijm/PowerShell-AdminScripts/blob/master/Miscellaneous/New-IsoFile.ps1
Here is everything the script really does:
def dir2iso(src, dest, title="Created by pxpowershell"):
pshell = pxpowershell.PxPowershell(debug=False)
x = pshell.start_process()
x = pshell.run(powershell_script, 10)
x = pshell.run(f"cd {src}")
result = pshell.run(f"New-IsoFile {src} -path {dest} -title {title} -Force", 60*60)
result = pshell.run(f"ls {dest}")
pshell.stop_process()
return result
You start the powershell process. We send in the new-isofile script, then execute it capturing the results. Notice that each call to .run passes the command to send to powershell and a timeout to wait for the command to finish and return to the Powershell Prompt. If no timeout is provided the default is 3 seconds.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pxpowershell-1.0.0.tar.gz
.
File metadata
- Download URL: pxpowershell-1.0.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c470e48bef39b3aaa90475afbe7805bfa2af44c45a658d48876db7a2af26e0fe |
|
MD5 | d69f932da38b331b0a8e9e99b6ed032a |
|
BLAKE2b-256 | 446013ec142a1e109075d2d0adb52f527b80fae161c9aa2522e2aad363ba2264 |
File details
Details for the file pxpowershell-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: pxpowershell-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5b5a25dcbd6c59a1a7a9a2a380b2a561369f0c223ef942a76d941762b57472e |
|
MD5 | 25018b0337b0b41d2f0fe806b2562373 |
|
BLAKE2b-256 | e388be6d1b5dd4533f877cbe3975df28bd52ad21cc981a8a27a2dfb915fb6e0c |