Integrate powercli commands with Python scripts - python-2.7

When we run commands on power cli it displays the operations we have performed.
For example
Start-VM –VM “VM1”
simply starts the VM in the v center.
I want to write such code in Python that we can call these commands in code and store the output and display to the user.
Is there any way to link our Python code with power cli commands or we can say can we bind power cli code inside Python?

You want two things: dot source the script (which is (as far as I know) similar to python's import), and subprocess.call.
import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePowershell\";", "&hello"])
So what happens here is that we start up powershell, tell it to import your script, and use a semicolon to end that statement. Then we can execute more commands, namely, hello.
You also want to add parameters to the functions, so let's use the one from the article above (modified slightly):
Function addOne($intIN)
{
Write-Host ($intIN + 1)
}
and then call the function with whatever parameter you want, as long as powershell can handle that input. So we'll modify the above python to:
import subprocess
subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./SamplePowershell\";", "&addOne(10)"])
this gives me the output:
PowerShell sample says hello.
11
You'll need to edit the above to include the PowerCLI library, but it should work.

Related

How to run a C++ Program interactive using external tools in gedit

I'm trying to run my C++ program interactively straight in gedit using external tools.
I've already written a Python tool for compiling it, but when using os.system("./program.out") all input for the program is set to 0 (but it is getting executed right, though).
Since using Python for executing it interactive would be rather difficult, im looking for another solution.
#!/usr/bin/env python3
import os
filename = os.getenv("GEDIT_CURRENT_DOCUMENT_NAME")
fout = "%s%s"%(filename[:len(filename)-4], ".out") #
os.system("c++ -o %s %s" % (fout,filename))
os.system("./%s" % fout)
the compiling works flawless, but ./program.out also doesn't give the wanted result using bash.
Any suggestions?
I guess i've found an answer. It may not be the most beautiful solution, but it works.
When defining a tool you can choose the input in a drop-down menu, there is an option for selected text as input.
So for example, if you have a program that reads 3 variables from stdin, you can add a comment to your code like //1 3 2, only select the numbers and then run your tool.
These numbers will be used as input in this order, somehow it only works with spaces.
A cruel way of implementing this would be to scan for input calls in the source code and ask for them via zenity in the gedit tool, i guess.
And here is another solution:
I can just use echo 3 2 1 | ./program.out in the tool
this makes everything way more easy

Running Python programs in python.exe, the command prompt, and IDLE

Let me preface this post that I am incredibly new to python. I took a course in codeacademy but haven't actually used python on my own computer until now. Let me also explain to everyone that my coding experience is limited to VBA and excel. I have very little knowledge of how to use the command prompt, how libraries work, etc, etc.
I'm having some issues. I've changed my directory to recognize python when I type python in my command prompt. Then I can do simple things like
print "hello"
however if i write something like this
def firstfunction(t):
print t+5
return
firstfunction(5)
I would expect this to print the number 10, however I get the following error:
File"stdin", line 4
first function(5)
^
syntaxerror: invalid syntax
However if I use this in IDLE and run it it works fine.
Next I've saved some python programs I wrote. One I saved as Hello.py
i want to run this code from my python exe or from the command prompt but can't figure out how.
Please any answers helpful, if you use computer lingo, try and keep it as simple as possible. I'm super new!
Thanks
If the location of python is not included in your path, add it. But in any case, you should be able to run this successfully from the directory containing your python.exe.
e.g, if python lives in C:\Python27:
cd C:\Python27
python mypythonscript.py

gdb script, assign functions' return value in user variable

I would like to assign the "info sharedlibrary" value to a variable in user define function.
Such as,
define customFunction
set $i = info sharedlibrary
end
But it seems impossible in gdb
And also i cannot use python script too...
Is there any way to do this??
Thank you
ps. I trying to do this because i want to print only selected library's instructions.
You don't mention why you can't use a Python function. That's by far the simplest way to program gdb.
However, if you really must do it, and really must avoid Python, there is a way. It's gross! But it does work. It's like this:
Use the various set logging commands to redirect output to a temporary file.
Invoke the command you want.
Use set logging again to disable logging.
Use the shell command to run sed or perl or what-have-you on this temporary file to turn it into a sequence of gdb commands, say commands to set a variable, or commands to print exactly the output you want.
source the resulting file

how to get value from spinctrl and send it to .exe file in python script

I am new to python and wxpython, I am making a automated tool using python and for user interface wxpython and i use shell script.shell script can be called from the python. but now I am facing problem with the spinctrl value. whenever that spinctrl value changes it have to send that value into one txt.exe file which is written in BASH .{ if we run txt.exe file in command line it will ask for number then it will accept that value whenever we press enter}. but i am not able to understand that how to send value from spinctrl to txt.exe whenever i press "ok" button in GUI. please share your thoughts and knowledge.
Thank you
To call an executable in Python, you need to look at Python's subprocess module: http://docs.python.org/2/library/subprocess.html
If your exe doesn't accept arguments, then it won't work. If you created the exe yourself using something like PyInstaller or py2Exe, then you need to make it so your app can accept arguments. The simplest way is using sys.argv, but there are also the optparse and argparse libraries in Python as well.
I do not get a good feeling about your concept. It is hard to say, but I suspect you would benefit from talking with someone experienced about what you are trying to achieve and how it could best be done.
However, to get you started down your current path, I think you should take a look at wxExecute ( http://docs.wxwidgets.org/2.8/wx_processfunctions.html#wxexecute ). This will allow you to run your txt.exe utility from inside your GUI.
wxExecute is a wxWidgets utility. If you are working in python then there will be a more direct way to do this using a python utility - some-one else will have to advise on that. I suggest you edit your question for clarity so a python expert can help you.

Program Interaction and testing via bash script

I've just completed the coding section of simple homework assignment for my C++ class. The second part of the assignment requires us to verify our code's input validation. (The program takes several different values as inputs from a user and prints those values to a file)
I was hoping that I could use bash script for this. Is there any way to use bash script to run and interact with a program? How can I put the program's output into a variable (note that program has a series of input requests and outputs).
Thanks
To build on #Travis' answer, create two files: one holds your inputs (input.txt) and one holds the expected output (expected_output.txt). Then do the following:
./myprogram <input.txt >output.txt
diff output.txt expected_output.txt
If the diff command has any output, there's a problem.
You can do much of this with a shell script but you might want to consider using some other testing tools instead like CppUnit or expect.