python - 从 Python 执行 PowerShell 脚本的最佳方式是什么

标签 python python-2.7 powershell

关于此主题的所有先前帖子都针对其用例的具体挑战进行了处理。我认为有一个帖子只处理从 Python 运行 PowerShell 脚本的最干净的方法并询问是否有人有比我发现的更好的解决方案。

似乎普遍接受的解决方案是解决 PowerShell 试图以不同方式解释命令中的不同控制字符的问题,即在使用文件时提供 Powershell 命令:

ps = 'powershell.exe -noprofile'
pscommand = 'Invoke-Command -ComputerName serverx -ScriptBlock {cmd.exe \
/c "dir /b C:\}'
psfile = open(pscmdfile.ps1, 'w')
psfile.write(pscommand)
psfile.close()
full_command_string = ps + ' pscmdfile.ps1'
process = subprocess.Popen(full_command_string , shell=True, \
stdout=subprocess.PIPE, stderr=subprocess.PIPE)

当您的 python 代码需要在每次调用 Powershell 命令时更改参数时,您最终会编写和删除大量临时文件以供 subprocess.Popen 运行。它工作得很好,但它是不必要的,而且不是很干净。能够整理并希望获得有关我可以对找到的解决方案进行的任何改进的建议真是太好了。

不是将包含 PS 命令的文件写入磁盘,而是使用 io 模块创建一个虚拟文件。假设“日期”和“服务器”字符串作为包含此代码的循环或函数的一部分输入,当然不包括导入:

import subprocess
import io
from string import Template
raw_shellcmd = 'powershell.exe -noprofile '

--填充服务器和日期变量的循环开始--

raw_pslistcmd = r'Invoke-Command -ComputerName $server -ScriptBlock ' \
        r'{cmd.exe /c "dir /b C:\folder\$date"}'

pslistcmd_template = Template(raw_pslistcmd)
pslistcmd = pslistcmd_template.substitute(server=server, date=date)
virtualfilepslistcommand = io.BytesIO(pslistcmd)
shellcmd = raw_shellcmd + virtualfilepslistcommand.read()

process = subprocess.Popen(shellcmd, shell=True, stdout=subprocess.PIPE, \
stderr=subprocess.PIPE)

--循环结束--

最佳答案

可以说最好的方法是使用 powershell.exe -Command 而不是将 PowerShell 命令写入文件:

pscommand = 'Invoke-Command ...'
process = subprocess.Popen(['powershell.exe', '-NoProfile', '-Command', '"&{' + pscommand + '}"'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

确保 pscommand 字符串中的双引号被正确转义。

请注意,shell=True 仅在某些极端情况下是必需的,不应在您的场景中使用。来自documentation :

On Windows with shell=True, the COMSPEC environment variable specifies the default shell. The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

关于python - 从 Python 执行 PowerShell 脚本的最佳方式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47094207/

相关文章:

python - 分组值并使用 Python 在 Pandas 上合并

python - 使用 BeautifulSoup 获取属性值

powershell - 验证 PowerShell 中是否加载了类型

powershell - foreach-object if return , 返回所有值问题

performance - Where-Object或Compare-Object哪个更好?

python - 将 Mayavi 鼠标拖动限制为绕其轴旋转地球

python - 我想打印以奇数或偶数作为索引的字母(字符串)并将它们保存为列表

python - 如何提示 python3.6 参数必须具有大小且可迭代?

python-2.7 - 验证 Cloudformation 模板时 cfn-lint 失败

python - 从python中的两个数组查找的最快方法