M-script to determine if user has administrator rights
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am seeing an issue where my script crashes for some users on some machines, because that user does not have administrative rights on that machine. Is there any way I can check in the m-script to see if the user has admin rights? I'm not asking if the user is running MatLab as an administrator, but if the user has the ability to run other programs (say a compiler which will be called by a build process later in the script) as an administrator.
0 commentaires
Réponses (3)
raym
le 18 Mai 2019
function out = isadmin
out = System.Security.Principal.WindowsPrincipal(...
System.Security.Principal.WindowsIdentity.GetCurrent()).IsInRole(...
System.Security.Principal.WindowsBuiltInRole.Administrator);
end
1 commentaire
Walter Roberson
le 18 Mai 2019
Interesting, but I would have thought that this is testing whether the user is using Run As Administrator, which the original poster indicates is not quite what they need
Guillaume
le 15 Août 2016
Because access permission is such a complex beast on Windows, the best way to check if the user can do something is simply to attempt to do the something and check that it succeeded:
try
%do operation that may fail due to insufficient permissions
catch
%operation failed, handle any way you want, e.g.:
error('You do not have sufficient permissions to perform the operation. Log in as admin and try again.');
end
%rest of program ...
Note that being administrator may or may not be sufficient to do whatever you want to do and note that even non-administrators may be able to do it. If you really wanted to check before hand if the user can do your operation you would check for the exact permission required among the many possible permissions.
In any case, there is no tool in matlab to manage / check user permissions. If you really want to check permissions, on Windows, you would have to defer to .Net, for example the <https://msdn.microsoft.com/en-us/library/system.security.principal.windowsprincipal(v=vs.110).aspx WindowsPrincipal class. I've not been able to work out how to use it reliably, unfortunately.
As said, try... catch is the best way to deal with this.
0 commentaires
Syed Owais
le 3 Nov 2019
hello
i am trying to add a function in matlab through edit windows but when i save it shows message that you dont have administrative rights to do so what should i do
1 commentaire
Walter Roberson
le 3 Nov 2019
You should rarely rarely save a function underneath the MATLAB installation directory.
About the only exception I have come across is that there is provision for a site-specific startup file that it would make sense for systems administrators to configure. However, in such a case the systems administrators would be able to grant themselves access to write the file.
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!