如何在带有定义变量并打印输出的Python中运行特定的Powershell命令?

问题描述 投票:0回答:1

我正在尝试从Python到Powershell运行特定的命令:

该命令在Powershell中按预期工作。 Powershell中的命令如下:

 gpt .\Method\gpt_scripts\s1_cal_deb.xml -t .\deburst\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E_Cal_deb_script.dim .\images\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E.zip

Powershell输出:

enter image description here

os.getcwd()

'C:\\Users\\Ishack\\Documents\\Neta-Analytics\\Harvest Dates\\S1_SLC_Processing'

当前目录与PowerShell中的相同

我尝试过这样的事情:

import subprocess


process = 'gpt .\Method\gpt_scripts\s1_cal_deb.xml -t .\deburst\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E_Cal_deb_script.dim .\images\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E.zip'

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


process

输出:

<subprocess.Popen at 0x186bb202388>

但是当我按Enter时我没有任何反应,我希望Python像在Powershell中一样打印输出。我研究了其他类似的问题,但仍然无法解决问题。

谢谢,

Ishack

python string powershell subprocess getcwd
1个回答
0
投票
import os
os.system("powershell.exe -ExecutionPolicy Bypass -File .\SamplePowershell.ps1")

SamplePowershell.ps1

Write-Host "Hello world"
hostname

这对我有用。文件SamplePowershell.ps1。]中的命令

© www.soinside.com 2019 - 2024. All rights reserved.