how to run Get-WindowsOptionalFeature -Online command in Python On Windows server2016 - python

When I execute "Get-WindowsOptionalFeature -Online" command in Windows2016 server powershell, the output is all fine, but when I execute with the Python script below,
cmd = os.popen("Get-WindowsOptionalFeature -Online")
print(cmd.read())
the output is empty.
is there something wrong with my code?
I tried to exec ipconfig using this code, it worked fine

My error comes from my ignorance of powershell. I regard powershell as cmd, so I directly use os.popen to call the command to be executed. In fact, you need to call powershell first to execute the command that needs to be executed, the following is an example:
import subprocess;
process=subprocess.Popen(["powershell","Get-WindowsOptionalFeature -Online"],stdout=subprocess.PIPE);
result=process.communicate()[0]

Related

How to run Windows powershell script from python?

I would like to run a script like in the picture in the Powershell but from python. [1]: https://i.stack.imgur.com/K7XGI.png For instance, I would like to have a code like this:
def run(namefile):
command that opens powershell
command that type in the powershell .\spim.exe .\namefile.txt
command that run the script
Does someone know how to do this ?
If you just want to run the exe file you can use subprocess module.
If you really want to run it inside the Powershell terminal you can run the Powershell binary with -Command option.

Why is my python script not running from command line?

I am trying to run a simple python script from my command line. It works fine when I run the python script on its own but when I call it from the command line nothing happens. I don't even get an error. Python is added to my path. This is what I'm doing.
cmd C:\users\RGilsburg\Desktop\a\untitled1.py
The command prompt just proceeds to print the Microsoft Version and copyright details.
Anyone know what is causing this?
To run the script
python C:\users\RGilsburg\Desktop\a\untitled1.py

execute a command from remotedly using paramiko

I have board server, in which i want to execute a gstreamer command from parmiko which will take the input file as a argument
this one is working
stdin,stdout,stderr=ssh_client.exec_command('ls')
want to execute below command but not working
stdin,stdout,stderr=ssh_client.exec_command('gst_app /media/card/pipeline.cfg')
want to execute a gst_app /media/card/pipeline.cfg command from ssh_client.exec_command
can anyone please help
If a command does not work, start by reading its error output.
Use stderr.readlines() for that.
Quite often the error is "<command> not found". For that see
Some Unix commands fail with "<command> not found", when executed using Python Paramiko exec_command

Terminal Prompt Not Changing To '>>>' When Executing Python File

I am running the following command on terminal:
python SpellingCorrector.py
No error is thrown by terminal and it simply progresses to the next line with the same prompt showing my current working directory, rather than the Python '>>>' terminal prompt.
I would like to run a function within the program with an argument and I only have the option to attempt that as such:
[my/current/directory/]$ correction('speling')
This then throws the error
bash: syntax error near unexpected token `'speling'`
I'm guessing I need to run it with this prompt in order for it to work:
>>> correction('speling')
The Python version is 2.7.5.
Does anyone know why the prompt is not changing when I run the program or how I can run the function?
you need to execute your script in the interactive mode, like the following :
python -i SpellingCorrector.py
and then execute your function from there :
correction('speling')
You are launching the program instead of launching python interpreter.
To use interpreter, launch it as follows (without arguments):
python
then use import SpellingCorrectorto import your program to interpreter.
Now you can use its functions etc.
Please note, that import statement has no .py extension.
The python script.py command simply executes the script and returns control to the shell, which is not what you want.
You could use ipython's %run command to run a python script from inside the interpeter:
%run ./script.py
This is similar to running at a system prompt python file args, but
with the advantage of giving you IPython’s tracebacks, and of loading
all variables into your interactive namespace for further use.

mac python, terminal “print command not found”

I try use the super admin and use #!/usr/bin/python at the top however it also doesn't work
Actually, I want to open the .py document but terminal shows that print: command not found.
Just type python in your terminal to invoke the interactive shell. You gave the command in the bash terminal beginning with #, which inactivated the whole line.
If you want to run a python script, then put your code in a file like this:
script.py
#!/usr/bin/python
print "2"
Now you can run the code typing python script.py.
The first line of the code is the shebang line, which specifies which interpreter to use, so you can run your script typing ./script.py.

Categories

Resources