Run process as admin with subprocess.run in python

Windows has a command line utility “Run as”, which can be used as   runas [{/profile | /noprofile}] [/env] [{/netonly | /savecred}] [/smartcard] [/showtrustlevels] [/trustlevel] /user:<UserAccountName> “<ProgramName> <PathToProgramFile>” for further reference https://technet.microsoft.com/en-us/library/cc771525.aspx You can use this in code like below import subprocess as sp prog = sp.Popen([‘runas’, ‘/noprofile’, ‘/user:Administrator’, ‘NeedsAdminPrivilege.exe’],stdin=sp.PIPE) prog.stdin.write(‘password’) prog.communicate()

Detect if Java application was run as a Windows admin

I’ve found a different solution that seems to be platform-independent. It tries to write system-preferences. If that fails, the user might not be an admin. As Tomáš Zato suggested, you might want to suppress error messages caused by this method. You can do this by setting System.err: import java.io.OutputStream; import java.io.PrintStream; import java.util.prefs.Preferences; import static … Read more

Limit foreign key choices in select in an inline form in admin

Used request instance as temporary container for obj. Overrided Inline method formfield_for_foreignkey to modify queryset. This works at least on django 1.2.3. class RoomInline(admin.TabularInline): model = Room def formfield_for_foreignkey(self, db_field, request=None, **kwargs): field = super(RoomInline, self).formfield_for_foreignkey(db_field, request, **kwargs) if db_field.name == ‘inside_room’: if request._obj_ is not None: field.queryset = field.queryset.filter(building__exact = request._obj_) else: field.queryset = … Read more

MongoDB – admin user not authorized

I was also scratching my head around the same issue, and everything worked after I set the role to be root when adding the first admin user. use admin db.createUser( { user: ‘admin’, pwd: ‘password’, roles: [ { role: ‘root’, db: ‘admin’ } ] } ); exit; If you have already created the admin user, … Read more