Passing Python Variables to Powershell Script - python

I'm looking to pass variables from a Python script into variables of a Powershell script without using arguments.
var_pass_test.py
import subprocess, sys
setup_script = 'C:\\Users\\user\\Desktop\\Code\\Creation\\var_pass_test.ps1'
test1 = "Hello"
p = subprocess.run(["powershell.exe",
setup_script], test1,
stdout=sys.stdout)
var_pass_test.ps1
Write-Host $test1
How would one go about doing this such that the Powershell script receives the value of test1 from the Python script? Is this doable with the subprocess library?

To pass arguments verbatim to the PowerShell CLI, use the -File option: pass the script-file path first, followed by the arguments to pass to the script.
In Python, pass all arguments that make up the PowerShell command line as part of the first, array-valued argument:
import subprocess, sys
setup_script = 'C:\\Users\\user\\Desktop\\Code\\Creation\\var_pass_test.ps1'
test1 = "Hello"
p = subprocess.run([
"powershell.exe",
"-File",
setup_script,
test1
],
stdout=sys.stdout)

Related

Passing a list as variable to python script from bash script

I have a python script like below.
# Import necessary libraries and functions
import sys
import traceback
y = '2020'
querysting = "select {} from table where Year={}".format(col_list,y)
df = pd.read_sql(querystring,db)
if __name__ == '__main__':
if len(sys.argv) != 8:
print('Invalid number of args......')
print('Usage: file.py Input_path Output_path')
exit()
_, col_list, FinalDB, final_table, host, dsl_tbl_name, username, password = tuple(sys.argv)
data_load(col_list, FinalDB, final_table, host, tbl_name, username, password)
Now I am calling this python script inside a shell script like below
#!/bin/bash
col_list='id, name, address, phone_number'
FinalDB='abc'
final_table='xyz'
host='xxxxxx.xxxx'
tbl_name='test'
username='USER'
password='test_pass'
python python_script.py ${col_list} ${FinalDB} ${final_table} ${host} ${tbl_name} ${username} ${password}
Now when I run this bash script I am getting Invalid number of args...... error
I am pretty much sure that it is some thing to do with col_list variable being passed to the python script.
Because instead of having columns in a list if I just pass select * in the query and removing col_list variable then I don't get any errors
What am I doing wrong here and how can I resolve this issue.
The problem comes from how you pass your variables that contains spaces from Bash to Python.
You may note that:
In shell scripts command line arguments are separated by
whitespace, unless the arguments are quoted.
Let's have this simple Python script:
python_script.py:
import sys
if __name__ == '__main__':
print(sys.arv)
Then in your Terminal:
$> export var1="hello"
$> python python_script.py $var1
['python_script.py', 'hello']
However:
$> export var1="hello, hi"
$> python python_script.py $var1
['python_script.py', 'hello,', 'hi'] # Note Here: Two args were passed
$> export var1="hello,hi"
$> python python_script.py $var1
['python_script.py', 'hello,hi'] # One arg passed
So, a solution to your problem, is to pass your Bash args as a string even with spaces, like this example:
$> export var1="hello, hi, hola"
$> python python_script.py "$var1"
['python_script.py', 'hello, hi, hola']
For more informations see this article

python variables as argument in subprocess

I am running below Python code & using subprocess to call one Python script. It is not able to substitute the value of ${ENVIRONMENT}.
import sys
import subprocess
#ENVIRONMENT=sys.argv[1]
ENVIRONMENT='test'
url=subprocess.check_output("python env.py ${ENVIRONMENT}", shell=True)
Use string formatting:
url = subprocess.check_output(
"python env.py {ENVIRONMENT}".format(ENVIRONMENT=ENVIRONMENT), shell=True)
or pass your command as a list:
url = subprocess.check_output(["python", "env.py", ENVIRONMENT])

Running a python scripts and catching argument into another python script

I am unable to get this working .
I have something like this
python executor.py arg1 arg2 arg3
Ant then I have another python script which is not directly called by excecutor.py but by some another script file which is being called in executor.py .Lets it call
script.py
there is a variable name argument in which i want to catch arg1.
How to do do it ?
I am assuming you are doing import script within the executor.py script. If this is the case you have to add to executor.py:
import script
import sys
argument_1 = sys.argv[1] # since [0] is the script name
...
script.yourfunction(argument_1)
assuming script.py and executor.py are in the same folder:
script.py:
def function1(somearg, otherarg):
pass
def function2(moreargs):
pass
and executor.py
import sys
import script
# assign input args; check for valid arguments etc..
arg1 = sys.argv[1]
arg2 = sys.argv[2]
etc...
# call system functions
script.function1(arg1, arg2)
script.function2(arg1)

pass variable in to command in python

i'm writing a python script to execute shell command, and i'm taking arguments and i want to pass the value of that to the command
#!/usr/bin/env python
import commands
import subprocess
import sys
command = commands.getoutput('fs_cli -x "sofia profile external restart"')
this code works fine
when i try to take the argument and pass to command it fails
command = commands.getoutput('fs_cli -x "sofia profile" + sys.argv[1]
+ " restart"')
supp folks
You should write:
command = commands.getoutput('fs_cli -x "sofia profile ' + sys.argv[1] + ' restart"')
Take a look to argparse and subprocess.
One of the way to do this is to convert your command that you want to execute into string and then execute it as eval()
example :
eval(expression/command in string)

calling perl script that takes multiple input args in python

Need help with integrating perl script with main python script.
I have a perl script by name: GetHostByVmname.pl
./GetHostByVmname.pl –server 10.0.1.191 –username Administrator –password P#ssword1 –vmname RHTest
I need to call above script from my python main script. Tried below, but doesn’t work:
param = "--server 10.0.1.191 --username Administrator --password P#ssword1 --vmname RHTest"
pipe = subprocess.Popen(["perl", "./GetHostByVmname.pl", param ], stdout=subprocess.PIPE)
You can either provide a shell command
Popen("./GetHostByVmname.pl –server 10.0.1.191 ...", ...)
Or an array where the the first element is the program and the rest are args.
Popen(["./GetHostByVmname.pl", "–server", "10.0.1.191", ... ], ...)
Currently, you are doing the equivalent of the following shell command:
perl ./GetHostByVmname.pl '–server 10.0.1.191 ...'
I think it will be better when you split string
./GetHostByVmname.pl –server 10.0.1.191 –username Administrator –password P#ssword1 –vmname RHTest
to a list, and after call Popen with this list as a first param.
Example:
import shlex, subprocess
args_str = "./GetHostByVmname.pl –server 10.0.1.191 –username Administrator –password P#ssword1 –vmname RHTest"
args = shlex.split(args_str)
p = subprocess.Popen(args, stdout=subprocess.PIPE)

Categories