gpt4 book ai didi

python - 在python中,如何将数组参数传递给Powershell脚本

转载 作者:太空宇宙 更新时间:2023-11-03 20:07:52 25 4
gpt4 key购买 nike

我有一个PowerShell脚本,它具有两个参数,第一个是字符串,第二个是字符串数组。

我想从我的python代码中调用此PowerShell脚本。如何将数组类型参数传递给PowerShell?

如果我写这样的话:

subprocess.run(['powershell.exe', 'script.ps1', 'arg1', '@("str1", "str2")'])


Powershell认为'@(“ str1”,“ str2”)'是字符串,而不是数组。

编辑
我找到了解决方法

subprocess.run(['powershell.exe', 'script.ps1 arg1 @("str1", "str2")'])


它看起来不漂亮,但是可以用。这样,我不能在 -File之后使用 powershell.exe

最佳答案

可以说您的python数组是arr

尝试这样做:

subprocess.run(['powershell.exe', 'script.ps1', 'arg1', '\"{}\"'.format(','.join(arr))])


要使用Powershell脚本发送数组,可以将其作为 "item1,item2,item3"发送
并且 str.join函数使您可以轻松获得此格式

如果这不起作用,我将尝试编辑脚本以在Powershell脚本中使用 $args参数来更改您使用参数的方式

关于python - 在python中,如何将数组参数传递给Powershell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58873732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com