这是更好的(PowerShell)方法:https ://stackoverflow.com/a/16617861/863980
在一行中,您可以说(复制/粘贴到豪华中,它会起作用):
(@(([ADSI]"WinNT://./Administrators,group").psbase.Invoke("Members")) | `
foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}) -contains "Administrator"
=>True
当用户属于管理员组时返回(而不是检查用户 IS 管理员)
(注意:反引号或重音 ` 在 PowerShell 中转义回车,在 Ruby 中它执行 shell 命令,如 C++ 的 system('command')..)
所以在 Ruby 中,你可以说(在 irb 中复制/粘贴):
def is_current_user_local_admin?
return `powershell "(@(([ADSI]'WinNT://./Administrators,group').psbase.Invoke('Members')) | foreach {$_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)}) -contains 'Administrator'"`.include? "True"
end
虽然不知道(甚至更好)WMI 的方式。有了这个,你可以做类似的事情(再次在 Ruby 中):
require 'win32ole'
wmi = WIN32OLE.connect('WinNT://./Administrators,group')
# don't know what should come here...