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)
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
|
|
|
|
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)
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
|
|
|
|
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)
|
|
|
|
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)
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
|
|
|
|
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)
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
|
|
|
|