Python 从GitHub获取PowerShell脚本并执行它
在Python中执行PowerShell脚本时遇到问题 Python本身很简单,但在调用时,它似乎在传入Python 从GitHub获取PowerShell脚本并执行它,python,powershell,urllib2,Python,Powershell,Urllib2,在Python中执行PowerShell脚本时遇到问题 Python本身很简单,但在调用时,它似乎在传入\n,并传出错误 ['powershell.exe-ExecutionPolicy-Bypass-File','$Username=“test”\n$Password=“Password”\n$URL 以下是完整的代码: 导入操作系统 导入子流程 导入urllib2 fetch=urllib2.urlopen('https://raw.githubusercontent.com/test')
\n
,并传出错误
['powershell.exe-ExecutionPolicy-Bypass-File','$Username=“test”\n$Password=“Password”\n$URL
以下是完整的代码:
导入操作系统
导入子流程
导入urllib2
fetch=urllib2.urlopen('https://raw.githubusercontent.com/test')
script=fetch.read()
命令=['powershell.exe-ExecutionPolicy-Bypass-File',脚本]
print command#我相信这是因为您正在打开PowerShell,并且它会自动以特定的方式对其进行格式化
您可能会在命令输出中执行for循环,并在没有a/n的情况下打印。我认为这是因为您正在打开PowerShell,并且它会自动以特定方式格式化它
您可能会执行一个for循环,该循环将遍历命令输出并在不使用a/n的情况下打印
如何正确地将脚本存储为变量而不写入磁盘,同时避免\n
这个问题本质上是重复的。以您的例子来说,删除换行符是可以的。更安全的选择是用分号替换它们
script = fetch.read().replace('\n', ';')
从Python中调用PowerShell以便运行存储在$script
中的脚本的正确方法是什么
您的命令必须作为数组传递。此外,您不能通过-File
参数运行一系列PowerShell语句。请改用-command
:
rc = subprocess.call(['powershell.exe', '-ExecutionPolicy', 'Bypass', '-Command', script])
如何正确地将脚本存储为变量而不写入磁盘,同时避免\n
这个问题本质上是重复的。以您的例子来说,删除换行符是可以的。更安全的选择是用分号替换它们
script = fetch.read().replace('\n', ';')
从Python中调用PowerShell以便运行存储在$script
中的脚本的正确方法是什么
您的命令必须作为数组传递。此外,您不能通过-File
参数运行一系列PowerShell语句。请改用-command
:
rc = subprocess.call(['powershell.exe', '-ExecutionPolicy', 'Bypass', '-Command', script])