2024-06-30 15:48:52 发布
网友
在Python中执行PowerShell脚本时遇到问题。在
Python本身很简单,但它似乎是在调用时传入\n,并输出错误。在
\n
['powershell.exe -ExecutionPolicy Bypass -File', '$Username = "test";\n$Password = "password";\n$URL
以下是完整的代码:
所以我有两个问题:
$script
我相信这是因为你正在打开PowerShell,它会自动以特定的方式格式化它。在
你可以做一个for循环,在不带/n的情况下遍历命令输出和打印
How do I correctly store the script as a variable without writing to disk while avoiding \n?
这个问题本质上是this one的重复。在您的示例中,可以简单地删除换行符。更安全的选择是用分号代替它们。在
script = fetch.read().replace('\n', ';')
What is the correct way to invoke PowerShell from within Python so that it would run the script stored in $script?
命令必须作为数组传递。另外,您不能通过-File参数运行一系列PowerShell语句。使用-Command代替:
-File
-Command
我相信这是因为你正在打开PowerShell,它会自动以特定的方式格式化它。在
你可以做一个for循环,在不带/n的情况下遍历命令输出和打印
这个问题本质上是this one的重复。在您的示例中,可以简单地删除换行符。更安全的选择是用分号代替它们。在
命令必须作为数组传递。另外,您不能通过
^{pr2}$-File
参数运行一系列PowerShell语句。使用-Command
代替:相关问题 更多 >
编程相关推荐