How To Pass Arguments From Powershell Into Python Scripts
1 min readNov 2, 2021
The aim of this page📝 is to demonstrate how to pass arguments to python scripts via PowerShell scripts. In PowerShell terminal, you can just literally type them. But in PowerShell scripts, you must use variables. I provide examples of positional arguments as well as optionals using argparse (the implementation of python’s internal keyword arguments).
1. positional arguments using sys.argv within python code
- in the first one I use
sys.argv
to parse a argument to signify a non-default behavior - the example is from a script I use for in-place patching of Snowplow Analytics schemas (daily job/different story) — in edge cases, I am passing
delete
for the actual deletion via API - run
patch_schema -delete
to perform the deletion orpatch-schema
for the default patch
2. optionals (keyword arguments) using argparse modul within python code
- in the second, I am using argparse with named parameters to parse the arguments
- not that also the argparse named parameters (
-k
, and-v
) are part of the$<arg>
variable - the usecase is my python script with which I update Hashicorp Consul from powershell terminal (daily job/different story)
- for more on argparse, see Python Arguments With Argparse module: The Template I Use for CLI Scripting