Get User SID (for login scripts)

Author Message
ginolard

  • Total Posts : 1347
  • Scores: 23
  • Reward points : 0
  • Joined: 8/11/2005
  • Status: offline
Get User SID (for login scripts) Friday, April 06, 2007 3:02 AM (permalink)
0
I ran into a thorny problem today.  How to get the user's SID during logon?  I couldn't query the owner of the explorer.exe process because it hadn't started yet.  I didn't want to use any external tools like PsGetSID.  I also didn't want to query AD as I didn't want to take the risk that the user didn't have access to that.  So, I came up with this little "hack".

It creates an environement variable of the user's Fullname.  It then walks through HKEY_USERS trying to find in which key the associated registry key for that environment variable has been created.  Once it finds that, it knows what the SID is because it'll be the name of the parent key. 

 Const HKEY_USERS = &H80000003
 Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
 Set objSysInfo = CreateObject("ADSystemInfo")
 Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
 Set wshShell = CreateObject("WScript.Shell")
 Set objSysEnv = wshShell.Environment("User")
     
 objSysEnv("FULLNAME") = objUser.FullName
 
 objReg.EnumKey HKEY_USERS, "", arrKeys
  
 For Each subkey In arrKeys
     
     objReg.GetExpandedStringValue HKEY_USERS,subkey & "\Environment","FULLNAME",strValue
     If strValue = objUser.FullName Then
         wscript.echo "SID = " & subkey
     End If
 
 Next 
 
 

 
Author of ManagePC - http://managepc.net

 
#1
    DiGiTAL.SkReAM

    • Total Posts : 1259
    • Scores: 7
    • Reward points : 0
    • Joined: 9/7/2005
    • Location: Clearwater, FL, USA
    • Status: offline
    RE: Get User SID (for login scripts) Friday, April 06, 2007 4:21 AM (permalink)
    0
    Under the key "HKEY_CURRENT_USER\Software\Microsoft\Protected Storage System Provider" there is a subkey that matches the HKEY_USER\subkey key for the current user.

     const HKEY_CURRENT_USER = &H80000001
     Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
     oReg.EnumKey HKEY_CURRENT_USER, "Software\Microsoft\Protected Storage System Provider", arrSubKeys
      For Each subkey In arrSubKeys
          WScript.Echo "Your SID is : " & subkey
      Next
     

     
    <message edited by DiGiTAL.SkReAM on Friday, April 06, 2007 4:30 AM>
    "Would you like to touch my monkey?" - Dieter (Mike Meyers)

    "It is better to die like a tiger, than to live like a pussy."
    -Master Wong, from Balls of Fury
     
    #2
      ginolard

      • Total Posts : 1347
      • Scores: 23
      • Reward points : 0
      • Joined: 8/11/2005
      • Status: offline
      RE: Get User SID (for login scripts) Friday, April 06, 2007 5:27 AM (permalink)
      0
      *grumble*

      Typical
      Author of ManagePC - http://managepc.net

       
      #3
        DiGiTAL.SkReAM

        • Total Posts : 1259
        • Scores: 7
        • Reward points : 0
        • Joined: 9/7/2005
        • Location: Clearwater, FL, USA
        • Status: offline
        RE: Get User SID (for login scripts) Friday, April 06, 2007 5:38 AM (permalink)
        0
        Sorry man, I just ran into this same problem myself 2 weeks ago, so had the solution on-hand.
        "Would you like to touch my monkey?" - Dieter (Mike Meyers)

        "It is better to die like a tiger, than to live like a pussy."
        -Master Wong, from Balls of Fury
         
        #4
          dm_4ever

          • Total Posts : 3687
          • Scores: 82
          • Reward points : 0
          • Joined: 6/29/2006
          • Location: Orange County, California
          • Status: offline
          RE: Get User SID (for login scripts) Friday, April 06, 2007 8:42 AM (permalink)
          0
          Another possible way...

           Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")
           Dim wmiQuery : wmiQuery = "Select * From Win32_UserAccount Where Domain='" & objNetwork.UserDomain & _
                                     "' And Name='" & objNetwork.UserName & "'"
           Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
           Dim colItems : Set colItems = objWMIService.ExecQuery(wmiQuery)
           Dim objItem
           For Each objItem in colItems
               WScript.Echo "Your SID is : " & objItem.SID
           Next
           

          dm_4ever

          My philosophy: K.I.S.S - Keep It Simple Stupid
          Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
          Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
           
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.9