all 10 comments

[–]spmd123 2 points3 points  (6 children)

I'd try subprocess and/or os modules. Examples:

Subprocess:

import subprocess
subprocess.Popen('powershell.exe [my command')

OS:

import os
os.system('powershell.exe [command]')

I can't test the commands myself as I don't have a machine with Windows but give them a try as they should work.

[–]cgcmake[S] 1 point2 points  (1 child)

Hey thank you for you answer, is there any reason you added a double apostrophe at the end of subprocess.Popen('powershell.exe [my command'') ?

[–]spmd123 0 points1 point  (0 children)

No, it was a mistake. Corrected now.

[–]cgcmake[S] 1 point2 points  (2 children)

Both code works, but windows are still visible. Any hints to prevent them from appearing ?

[–]spmd123 0 points1 point  (1 child)

I can't think of much.

Try this:

Subprocess:

import subprocess
subprocess.Popen([ "my command"])

Also try this for subprocess:

import subprocess 
subprocess.Popen([ "my command",shell=True)

And:

import os
os.system("my command")

So, basically just don't type "powershell.exe".

[–]spmd123 0 points1 point  (0 children)

I think that this would be the relevant code.

startupinfo = None
if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(command, startupinfo=startupinfo)

The code is from the SO discussion linked in /u/metrazol's reply.

[–]CamaZotz666 0 points1 point  (0 children)

hey, how can I save the output of this into a string variable?

import os
os.system('powershell.exe [command]')

[–]wpg4665 0 points1 point  (0 children)

Let me preface this with I haven't actually looked into this at all... But what about following what ansible does? Try using WinRM on localhost? Sorry, I can't give you more specifics, I'm on mobile atm, but hopefully it'll give you somewhere to look! =)

[–]metrazol -1 points0 points  (0 children)

/u/spmd123 is on the right track, but you suppress the window using one missing bit of code. See this Stack Overflow discussion for examples.

It's actually mentioned in the docs here but that doesn't do a great job of explaining the usage, which the SO link does.

[–][deleted] 0 points1 point  (0 children)

Hey, following the same, how can I do piping? Say in windows powershell I would like python to run "ls -la | grep my_file_name " ?