简体   繁体   中英

How to pass python arguments via start-process powershell

I want to open a process as a different user for which I researched a lot and found that no cmd or python script can help me to achieve my goal, but the Start-Process command of powershell can. The issue currently I am facing is while passing arguments to my python script via Start-Process in powershell. I tried using -ArgumentList but that doesn't helped me as I have to pass arguments as a dict to python. Below is my script which I was running:
Start-Process python.exe "E:\\test_installer\\src\\client\\sql_server\\sql_agent_ process.py" {"request_type": "backup", "client_id": 15, "request_id": 39431, "bkpset_id": 18, "ppid": 33796, "fromsource": "True"}
Can someone please help me here? Thanks in advance.

You can try to pass the argument to the script as a string then use the ast.literal_eval to convert it back to dictionary.

In your python script use the sys.argv to get the command line argument.

It should look something like this:

import sys
import ast
command_line_args = ast.literal_eval(sys.argv[1])

While the powershell command is:

Start-Process python.exe "E:\test_installer\src\client\sql_server\sql_agent_
process.py" "{\"request_type\": \"backup\", \"client_id\": 15, \"request_id\": 39431, \"bkpset_id\": 18, \"ppid\": 33796, \"fromsource\": True}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM