Python starting subprocess as admin without calling process being admin - python

I am trying to write a program in python that consists of several parts:
a config utility
a hardware monitor
a background process
The idea being that once installed (using cx_freeze) the hardware monitor is constantly running in the background, when a piece of compatible hardware (using d2xx driver for FTDI devices) is connected it checks the registry to see if it has been previously configured, if it has then it starts the background process with the serial number as an argument, however if not it starts the config utility.
However the hardware monitor needs to be running from start-up and as it only reads from the registry doesn't need full admin privileges, and the background process only reads so also does not need admin provileges, but the config utility needs to be able to write to the registry and hence needs admin.
My question is this:
How can I call another program from within python as admin and with arguments?
I considered using os.startfile as I have set the frozen program as needing admin, however i then can't pass arguments to it.
I also considered using subprocess.Popen but i can't work out how, or even if you can, elevate this to admin level, so while it will open the program and pass it the arguments it can't write to the registry.
Any help would be appreciated, for further information my set-up is:
Windows 7 64 bit (but also plan to do XP 32 bit)
python2.7.6 (again 64 bit but plan to also do 32 bit)
PyUSB-1.6
psutil-1.2.1
cx_freeze-4.3.2
Thanks
James

After much hunting I have found a solution, I tried using:
os.popen
os.startfile
subprocess.call
subprocess.Popen
and finally os.system
as os.system is essentially the same as typing on the command line or putting the arguments into a batch file and then executing it this asks for the executables default permissions, the only downside to this is that I get a shell window when the UAC window comes up, which remains until the program it opened is closed.
the problem with the other solutions are:
1 - passes only the permissions of the calling application, regardless of what the called application requires.
2 - asks for higher level of permissions but no mechanism to pass arguments
3 - same as 1
4 - same as 1
if anyone can recommend a mechanism to prevent the shell window it would be appreciated.
James

Related

Prevent exe file from being closed/Crash windows os if a program is closed

I'm trying to achieve something ; I created a exe file, that automatically force shutdown the computer at 11 pm.
I would like to make this script impossible to stop or crash, or even make the entire system crash if the program is closed.
How can i achieve this ?
Note :
I'm on a laptop, running windows 10. I made a python file, and i converted it in an exe file with py installer. Then i created a shortcut to that exe file that run the program with admin rights
If you mark the process as critical Windows will trigger a blue screen crash if that process is stopped/killed.
There is information about how to do this here
Note: Although it is possible to do this, it is not a good idea to do so. For example as suggested by Anders, use a Scheduled Task. Having the system crash could result in information loss, or other unintended consequences.
Create a Windows service. You can deny Administrators trying to stop/pause the service, that should slow them down a little bit.
Or since we are talking about triggering something at a specific time, you might want to use the task scheduler instead.
In any case, you will never fully lock down something like this from an Administrator since they can always take ownership and modify the ACL.

Launching a Windows task using Python event?

I created a Python script that opens a certain software on my PC (the MetaTrader 5 software to be specific), does some processing, saves a file to disk, and closes the software. To have the script running in the background (in hidden / headless / background mode), I was advised to convert the script to a Windows task, since my initial approach using subprocess.Popen() and the STARTUPINFO instance seems to fail for any complex application (see John Hennig's comment under his reply here). Therefore, I created a .bat file and connected it with my new Windows task. Now I hit a roadblock though, since Windows tasks are typically set up with triggering time/frequency to be run automatically. In my case, however, I want the trigger to be an external Python script. Is this possible? If not, is there any other way to have applications run without their windows flashing up on screen?

It is possible to use the CMD to send commands in the active Maya window?

I am a beginner in Maya, and I wanted to run commands in a cmd in the active window of maya to modify things in the scene, like put a sphere for example, be in python
from pymel.all import *
sphere()
or MEL
polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -cuv 2 -ch 1;
I found the mayapi, and I found several content related to "headless" (without the GUI), but I found nothing to run in the open window yet, maybe because I don't know the terms very much. I would like it to be in python, but if you know any solution in MEL you can put it here too!
Is there any way to do this without specifying the open document path?
You have four basic options for programmatic control of maya.
Running scripts inside the script editor in Maya. This requires that you have an open GUI maya instance running and you'd either type the commands yourself, or initiate scripts by loading and executing them in the script editor. This works fine for automating repetitive tasks but requires manual intervention on your part.
You can send individual commands to Maya over a TCP connection using the maya command port This is basically like connecting to another computer over telnet: you can control the Maya sessions but you'll be communicating entirely via text. It's commonly used, for example, by people who are writing scripts in Sublime Text to test them out in Maya without switching windows
You can run a commandline-only copy of Maya using the MayaPy python interpreter that ships with Maya and the maya.standalone module, which hosts a non-GUI maya session. That lets you excute python commands in a Maya without needing the GUI at all -- it's a common tool for automation tasks.
You can pass a script argument to Maya at startup with the '-c' (for "command") flag. Maya will open and run that script. For legacy reasons the commands are only MEL, not python, but you can get around that by using the MEL command "python" along with a Python command in quotes.
All of these are useful, the right one really depends on what you need to do. For long running tasks however #3 is probably the most reliable method because it's easy to iterate on and test.

Windows Task Scheduler running Python script: how to prevent taskeng.exe pop-up?

Windows 7 Task Scheduler is running my Python script every 15 minutes. Command line is something like c:\Python\python.exe c:\mypath\myscript.py. It all works well, script is called every 15 minues, etc.
However, the task scheduler pops up a huge console window titled taskeng.exe every time, blocking the view for a few seconds until the script exits.
Is there a way to prevent the pop-up?
Simply save your script with .pyw extension.
As far as I know, .pyw extension is the same as .py, only difference is that .pyw was implemented for GUI programs and therefore console window is not opened.
If there is more to it than this I wouldn't know, perhaps somebody more informed can edit this post or provide their own answer.
This helps me:
In the “General” tab, under the “Security options” section, select the Run whether user is logged on or not option. (This is the option that will make the command window not to appear when the task runs automatically.)
Clear the Do not store password option. (This is an optional step, but if the task isn’t working, check this option and supply the appropriate credentials to run the task.)

How do I schedule a Python script to run as long as Windows XP is running?

I wrote a temperature logger Python script and entered it as a scheduled task in Windows XP. It has the following command line:
C:\Python26\pythonw.exe "C:\path\to\templogger.py"
It writes data to a file in local public folder (e.g. fully accessible by all who login locally).
So far, I was able to achieve this objective:
1. Get the task to run even before anyone logs in (i.e. at the "Press Ctrl+Alt+Del" screen)
But I'm having problems with these:
1. When I log in, log out, then log back in, the scheduled task is no longer active. I can no longer see it in the Task Manager's Processes tab. I suspect it closes when I log out.
2. I tried to set the task's "Run As..." property to DOMAIN\my-username and also tried SYSTEM, but problem #1 above still persists.
SUMMARY:
I want my program to be running as long as Windows is active. It should not matter whether anyone has logged in or out.
P.S.
I asked the same question in Super User, and I was advised to write it as a service, which I know nothing about (except starting and stopping them). I hope to reach a wider audience here at SO.
Is it possible to run a Python script as a service in Windows? If possible, how?
http://agiletesting.blogspot.com/2005/09/running-python-script-as-windows.html
Your scenario is exactly the required use case for a service, unfortunately tasks are ill suited for what you are looking to do. That said writing services in python is not a walk in the park either, to ease the pain here is a few links I have perused in the past:
http://agiletesting.blogspot.com/2005/09/running-python-script-as-windows.html
http://mail.python.org/pipermail/python-win32/2008-April/007298.html
I used the second link in particular to create a windows scripts that was then compiled to a executable service with py2exe and installed with SrvAny.

Categories