AnsweredAssumed Answered

Python: how can I run a system command as administrator and capture the command output?  Not answered, but provided summary.

Question asked by rastrauch Champion on Oct 20, 2016
Latest reply on Dec 6, 2016 by JQuinn-esristaff

Just to summarize what I need right away....

Bottom line:  I need help running the "wmic" OS command or the "psutil.Process(<number>).cmdline()" with administrator credentials, in my script, capturing the output to a variable.


In what seems like an endless pursuit of discovering and dealing with .lock file preventing replacement of a full gdb, I've now narrowed it down to trying to discover the .lock files that are being created by an ArcGIS Enterprise gp service.  My script gives me a list of these .lock files named in this manner

parcing this, I'm able to get the PID (process ID) 11256, in this case. With tech support help, using the Windows Task Manager "Processes" tab, and turning on the PID and "Command Line" columns (thru View->Select Columns), I can see the service for the PID.


I have tested a number of commands to try to capture this "Command Line" information so I can parse out the service I need to stop.  Some of these commands include "net file", "tasklist", "wcim", "psutil" (a python mod), fsmgmt.msc, and compmgmt.msc.   


The most promising of the OS commands is the wcim, but only if I can run it "As Administrator".  Testing it manually, in a cmd.exe window started as administrator, I get the fun "Command line" info.  Running in a non-admin cmd.exe window does not return the info (i.e. blank)


Using psutil module also very promising, but returns an AccessDenied error when trying to return the cmdline info.  So I think that would work if I could pass-it or run-it with admin credentials.



what it should return is a long string that includes  "-Dservice=servicefolder.servicename.MapService"  (in this test case).  I want to grab the value for  -Dservice so I can stop it, therefore releasing the .lock files.


I'm been researching and trying  subprocess (mainly...a few other found suggestions too), and have had no luck.  My script will have variables with valid user pass, but I do not know how to get this command to run properly.


My preference would be getting the info using the psutil.cmdline()  for the PID.  But would also be happy if I could get

wmic process where "ProcessID = 11140" get CommandLine

with whichever saving the return string to a variable (then I can parse it myself).


Bottom line:  I need help running the "wmic" OS command or the "psutil.Process(<number>).cmdline()" with administrator credentials, in my script, capturing the output to a variable.


Thanks to all you python gurus that want to take a shot at this.  If you have other suggestions for capturing the AGS service information base just on the .lock filename, within my script, I'm open to suggestions.   I've already tagged the AGS group (near top), so maybe some ArcGIS Server administrators have a trick on where I can grab this info. (??)


BTW - Once I get my script running with all my .lock tests, I hope to summarize in a blog post or document.  not there yet.