【发布时间】:2017-03-21 03:02:53
【问题描述】:
在 Python 中执行 PowerShell 脚本时遇到问题。
Python 本身很简单,但它似乎在调用时传入了\n 并出现错误。
['powershell.exe -ExecutionPolicy Bypass -File', '$Username = "test";\n$Password = "password";\n$URL
这是完整的代码:
import os
import subprocess
import urllib2
fetch = urllib2.urlopen('https://raw.githubusercontent.com/test')
script = fetch.read()
command = ['powershell.exe -ExecutionPolicy Bypass -File', script]
print command #<--- this is where I see the \n.
#\n does not appear when I simply 'print script'
所以我有两个问题:
- 如何在避免
\n的同时正确地将脚本存储为变量而不写入磁盘? - 从 Python 中调用 PowerShell 以运行存储在
$script中的脚本的正确方法是什么?
【问题讨论】:
标签: python powershell urllib2