Skip to main content
 首页 » 编程设计

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

2024年10月01日7wuhuacong

关于此主题的所有先前帖子都针对其用例的具体挑战进行了处理。我认为有一个帖子只处理从 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.