locked
VB.NET Access and resolve ReferencedDomainName and AccountName from SID RRS feed

  • Question

  • Hi

    I'd very much like to do exactly what the following script does, just from inside VB!


    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

    JUST go through the SID keys in ProfileList and pull out the

    ReferencedDomainName & "\" & Accountname

    I'm sure some of you guys will find it trivial but I cannot make it work :-/

    With Kind regards and high hopes

    Samoht


    • Edited by Samohtrelhe Wednesday, March 13, 2013 12:16 PM
    Wednesday, March 13, 2013 12:07 PM

Answers

  • Hi Samohtrelhe,

    Please try this VB.net code:

        Private Const profiles As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
        Public Sub GetUserName()
    
            Dim sh = CreateObject("WScript.Shell")
            Dim wmi = GetObject("winmgmts://./root/cimv2")
            Dim reg = My.Computer.Registry.LocalMachine.OpenSubKey(profiles, False,
                                Security.AccessControl.RegistryRights.EnumerateSubKeys Or Security.AccessControl.RegistryRights.QueryValues)
    
            For Each sid As String In reg.GetSubKeyNames
                Dim subkey = reg.OpenSubKey(sid)
                Dim path = subkey.GetValue("ProfileImagePath")
                If Directory.Exists(path) Then
                    Dim acct = wmi.Get("Win32_SID.SID='" & sid & "'")
                    Console.WriteLine(acct.ReferencedDomainName & "\" & acct.AccountName)
                End If
            Next
        End Sub

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Sunday, March 24, 2013 12:25 PM
    Thursday, March 14, 2013 12:52 PM

All replies

    • Proposed as answer by Mike Feng Thursday, March 14, 2013 12:23 PM
    Wednesday, March 13, 2013 1:27 PM
  • WOW!

    Thats a whopper! No way to do just what the script does?

    And as I read it I still have to figure out how to get hold of the SIDs from the 

    "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

    in the first place..

    Is that correctly understood?

    Wednesday, March 13, 2013 3:09 PM
  • Hi Samohtrelhe,

    Please try this VB.net code:

        Private Const profiles As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
        Public Sub GetUserName()
    
            Dim sh = CreateObject("WScript.Shell")
            Dim wmi = GetObject("winmgmts://./root/cimv2")
            Dim reg = My.Computer.Registry.LocalMachine.OpenSubKey(profiles, False,
                                Security.AccessControl.RegistryRights.EnumerateSubKeys Or Security.AccessControl.RegistryRights.QueryValues)
    
            For Each sid As String In reg.GetSubKeyNames
                Dim subkey = reg.OpenSubKey(sid)
                Dim path = subkey.GetValue("ProfileImagePath")
                If Directory.Exists(path) Then
                    Dim acct = wmi.Get("Win32_SID.SID='" & sid & "'")
                    Console.WriteLine(acct.ReferencedDomainName & "\" & acct.AccountName)
                End If
            Next
        End Sub

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mike Feng Sunday, March 24, 2013 12:25 PM
    Thursday, March 14, 2013 12:52 PM