0

I am trying to run a specific command from Python to Powershell:

The command works as expected in Powershell. The command in Powershell is as following:

 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 Output:

enter image description here

os.getcwd()

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

The current directory is the same as in PowerShell

I tried something like this:

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

Output:

<subprocess.Popen at 0x186bb202388>

But when I press enter I get no response, I would like Python to print out the output just like in Powershell. I researched other similar questions but still no solution to the problem.

Thanks,

Ishack

1 Answers1

2
import os
os.system("powershell.exe -ExecutionPolicy Bypass -File .\SamplePowershell.ps1")

SamplePowershell.ps1

Write-Host "Hello world"
hostname

This worked for me. Commands in file SamplePowershell.ps1.

and if you want to use subprocess

import subprocess
k=subprocess.Popen('powershell.exe hostname', stdout=subprocess.PIPE, stderr=subprocess.PIPE);
print(k.communicate())
sharmag
  • 189
  • 2
  • 7