这是更好的 (PowerShell) 方法:https://stackoverflow.com/a/16617861/863980
在一行中,您可以说(复制/粘贴到 posh 中,它会起作用):
(@(([ADSI]"WinNT://./Administrators,group").psbase.Invoke("Members")) | `
foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}) -contains "Administrator"
=> 当用户属于管理员组时返回True(而不是检查用户是管理员)
(注意:反引号或重音符 ` 在 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...