How to get the user names and domains of all locally stored user profiles?

这一生的挚爱 提交于 2019-12-13 14:21:23

问题


How do I retrieve the user names and domains of all user profiles stored on a computer?

Here's a screenshot of the User Profiles manager to illustrate what I mean:


回答1:


The profiles are mapped by SID. The mapping is stored in this registry key:

[HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList]

You can use WMI to enumerate the SIDs and resolve them to user and domain name:

Const HKLM = &h80000002
Const profiles = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"

Set wmi = GetObject("winmgmts://./root/cimv2")
Set reg = GetObject("winmgmts://./root/default:StdRegProv")

reg.EnumKey HKLM, profiles, subkeys
For Each sid In subkeys
  Set acct = wmi.Get("Win32_SID.SID='" & sid & "'")
  WScript.Echo acct.ReferencedDomainName & "\" & acct.AccountName
Next

If you're looking for the user/domain of existing profile folders only, check if the ProfileImagePath value inside the subkeys points to an existing folder:

Const HKLM = &h80000002
Const profiles = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"

Set sh  = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set wmi = GetObject("winmgmts://./root/cimv2")
Set reg = GetObject("winmgmts://./root/default:StdRegProv")

reg.EnumKey HKLM, profiles, subkeys
For Each sid In subkeys
  reg.GetStringValue HKLM, profiles & "\" & sid, "ProfileImagePath", path
  path = sh.ExpandEnvironmentStrings(path)
  If fso.FolderExists(path) Then
    Set acct = wmi.Get("Win32_SID.SID='" & sid & "'")
    WScript.Echo acct.ReferencedDomainName & "\" & acct.AccountName
  End If
Next


来源:https://stackoverflow.com/questions/15309535/how-to-get-the-user-names-and-domains-of-all-locally-stored-user-profiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!