フィルターのクリア

M-script to determine if user has administrator rights

13 ビュー (過去 30 日間)
Jeremy
Jeremy 2016 年 8 月 15 日
コメント済み: Walter Roberson 2019 年 11 月 3 日
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.

回答 (3 件)

raym
raym 2019 年 5 月 18 日
function out = isadmin
out = System.Security.Principal.WindowsPrincipal(...
System.Security.Principal.WindowsIdentity.GetCurrent()).IsInRole(...
System.Security.Principal.WindowsBuiltInRole.Administrator);
end
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 5 月 18 日
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
Guillaume 2016 年 8 月 15 日
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.

Syed Owais
Syed Owais 2019 年 11 月 3 日
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 11 月 3 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by