简体   繁体   English

如何通过启动进程 powershell 传递 python 参数

[英]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.我想以不同的用户身份打开一个进程,我对其进行了大量研究,发现没有 cmd 或 python 脚本可以帮助我实现我的目标,但是 powershell 的 Start-Process 命令可以。 The issue currently I am facing is while passing arguments to my python script via Start-Process in powershell.我目前面临的问题是通过 powershell 中的 Start-Process 将参数传递给我的 python 脚本。 I tried using -ArgumentList but that doesn't helped me as I have to pass arguments as a dict to python.我尝试使用 -ArgumentList 但这对我没有帮助,因为我必须将参数作为 dict 传递给 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.您可以尝试将参数作为string传递给脚本,然后使用ast.literal_eval将其转换回字典。

In your python script use the sys.argv to get the command line argument.在您的 python 脚本中,使用sys.argv来获取命令行参数。

It should look something like this:它应该是这样的:

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

While the powershell command is:powershell命令是:

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}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何:通过 Powershell 将参数传递给 Python 脚本 - How to: Pass Arguments to Python Script via Powershell 在Powershell中“ Start-Process python ex.py”和“ python ex.py”有什么区别? - What is the difference between “Start-Process python ex.py” and “python ex.py” in powershell? python:如何通过命令行将参数正确传递给可执行文件 - python: how to properly pass arguments to executable via commandline 如何通过python子进程传递两个相同的参数? - How to pass two same arguments via python subprocess? 如何在 Python function 中通过 ZF6F87C9FDCF8B3C3F07F93F1ZEE87 传递字符串 arguments - How to pass string arguments in Python function via C++ 如何通过exec将传递参数传递给代码对象并在python中打印? - How to print pass arguments to code object via exec and print in python? 从Powershell脚本传递python脚本的参数 - pass arguments of a python script from a powershell script 如何在 python 中自动化 powershell 进程? - How to automate a powershell process in python? 如何将参数传递给python greenlet作为附加参数 - How to pass arguments to a python greenlet as additional arguments celerybeat 如何通过调度程序将 arguments 传递给函数? - celerybeat how to pass arguments via scheduler to functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM