python调用powershell
时间: 2023-09-07 21:04:32
浏览: 221
### 回答1:
你可以使用 Python 的 subprocess 模块来调用 PowerShell。例如:
import subprocess
powerShellCommand = "Write-Host 'Hello from PowerShell'"
# [run](https://geek.csdn.net/educolumn/15da07d43c7a20f5ec210826be35c36c?spm=1055.2569.3001.10083) the PowerShell command using subprocess.[run](https://geek.csdn.net/educolumn/15da07d43c7a20f5ec210826be35c36c?spm=1055.2569.3001.10083)
result = subprocess.[run](https://geek.csdn.net/educolumn/15da07d43c7a20f5ec210826be35c36c?spm=1055.2569.3001.10083)(["powershell", "-Command", powerShellCommand], capture_[output](https://geek.csdn.net/educolumn/3fa40697d361ab41848a3d891b656e90?spm=1055.2569.3001.10083)=True, text=True)
# print the [output](https://geek.csdn.net/educolumn/3fa40697d361ab41848a3d891b656e90?spm=1055.2569.3001.10083)
print(result.stdout)
```