Selenium is one of the most powerful tools for web automation testing. Python can run this tool using the Selenium module, which can be installed from the PyPI.
Continue readingAuthor Archives: Kiprono Koech
Running Powershell Script Within Python Script
You can run a PowerShell script in Python using the subprocess or os module. In this article, we will learn how to execute a PowerShell script from Python in Windows, Mac, or Linux. The fundamental prerequisite is to have PowerShell installed on your system.
Windows comes preinstalled with PowerShell (you can update it by downloading the latest version from this link). You can install PowerShell core on Linux and Mac using the information below.
Installing PowerShell core in Linux
You can install PowerShell core on Debian by running the following commands from the terminal (source: Microsoft website). If you are running a different distribution, you can get the installation commands from here.
1 2 3 4 5 6 7 8 |
# Install system components sudo apt update && sudo apt install -y curl gnupg apt-transport-https # Import the public repository GPG keys curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - # Register the Microsoft Product feed sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-bullseye-prod bullseye main" > /etc/apt/sources.list.d/microsoft.list' # Install PowerShell sudo apt update && sudo apt install -y powershell |
Installing PowerShell Core on Mac
You can install PowerShell using Homebrew by running the following command on the terminal.
1 |
brew install --cask powershell |
Or update it using the commands
1 2 |
brew update brew upgrade powershell --cask |
Once done with the installation, you can start PowerShell from Windows, Linux, or Mac terminal by running this command in the terminal
1 |
pwsh |
And execute a PowerShell script using the command
1 |
pwsh <path/to/the/script> |
At this point, we are ready to execute a PowerShell script from within a Python script.
Running PowerShell Script Within Python Script
As an example, we will execute the PowerShell script named script1.ps1 with the content below. The script accepts one parameter, $Name, and prints a greeting message to the console based on the time of the day.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
param( $Name ) $noon = Get-Date -Hour 12 -Minute 0 -Second 0 $current_time = Get-Date Write-Host "The time is $current_time" if ($current_time -gt $noon){ Write-Host "Good afternoon, $Name`!" } else { Wite-Host "Good morning, $Name`!" } Then we can execute <em>script1.ps1</em> from Python code using the code below.<br><br>import subprocess, sys # Running "script1.ps1" script with the -Name parameter set to "Decker". p = subprocess.Popen(["pwsh", "script1.ps1", "-Name", "Decker"], stdout=sys.stdout) # Run this to send the result to stdout. Finish the execution process, in short. p.communicate() |
Output:
The time is 04/14/2023 18:46:50 Good afternoon, Decker!
If you want to save the output of the PowerShell script to a variable, you can send the output to the subprocess.PIPE, as shown below.
1 2 3 4 5 6 7 8 |
import subprocess, sys out = subprocess.Popen(["pwsh", "script1.ps1", "-Name", "Decker"], stdout=subprocess.PIPE) # read the content of the standard output (stdout) and decode it # to a string and save the results into "output" variable output = out.stdout.read().decode("utf-8") print(output) |
Output:
The time is 04/15/2023 17:37:35 Good afternoon, Decker!
You can also run the PowerShell script using the os module as follows.
1 2 3 4 |
import os return_code = os.system("pwsh script1.ps1 -Name Decker") print(return_code) |
If the execution runs successfully with no error, return_code=0. Note that os. system() has limited functionalities. The subprocess module discussed above provides more facilities for spawning new processes.
Conclusion
This guide discussed using the subprocess module to run the PowerShell script using Python across different platforms – Windows, Linux, and Mac.
Run PowerShell Script on Windows Startup
Running PowerShell scripts on Windows startup can automate essential tasks and save time.
Continue readingPowerShell String Contains Operator
PowerShell is a powerful tool for automation and administration tasks in Windows operating systems. It comes with a lot of built-in features and commands.
Continue readingCreate a File Name With the Current Date and Time in Python
You can create a file name with the current date and time in Python using the datetime module by following these steps.
Continue readingChanging File Permission in Python
Using the following syntactical steps, you can change file permissions in Python using chmod() function within the os module.
Continue readingWrite to the Console in Powershell
PowerShell has various methods to write output to the console. This article discusses 4 of these methods:
- Using the Write-Output cmdlet,
- Using the Write-Host cmdlet,
- Using the Write-Error cmdlet, and
- Using System.Console class,
Using PowerShell Regex Replace
There are two methods to use for text replacement in PowerShell – replace() method or -replace operator.
Continue readingMultiply Sequence by Float Error in Python
There are two types of numbers in Python:
- Integers – positive and negative whole numbers, e.g., -6, 0, and 9,
- Floats – positive and negative decimal numbers, for example, -2.3, 0.0, and 3.142
Save Output From a Powershell Command to a Variable
This article discusses two ways of saving PowerShell output into a variable. In each method, we will work on examples to cement the ideas.
Continue reading