#130340 - 2004-11-30 03:04 PM
Re: WinNT:// - Groupname from PrimaryGroupID ?
|
Jochen
KiX Supporter
Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
|
|
Top
|
|
|
|
#130341 - 2004-11-30 04:17 PM
Re: WinNT:// - Groupname from PrimaryGroupID ?
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Funny enough I wanted to do the complete opposite, and identify certain groups (e.g. Domain Admins) where the name had been changed or the language was not english.
I created a set of routines to handle the binary data.
Using ADO to convert to / from BYTE arrays.
|
Top
|
|
|
|
#130342 - 2004-11-30 04:50 PM
Re: WinNT:// - Groupname from PrimaryGroupID ?
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
I have code on my site that does this. It is inside Win32Admin3.zip.
http://home.comcast.net/~habullock/kix_solutions.htm
|
Top
|
|
|
|
#130343 - 2004-11-30 10:43 PM
Re: WinNT:// - Groupname from PrimaryGroupID ?
|
kholm
Korg Regular
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
A small KiX-version could be
Code:
Break On
$Dom = @LDomain $usr = 'Guest'
$oWMIService = GetObject("winmgmts:root\cimv2") $cItems = $oWMIService.ExecQuery("Select * from Win32_Group where LocalAccount = 0 And Name = 'domain users'") For Each $oItem In $cItems $SID = $oItem.SID Next
$SID = Left($SID,InStrRev($SID,'-')) ; Get Domain RID $oUser = GetObject("WinNT://" + $Dom + "/"+ $usr + ",User") $SID = $SID + $oUser.Get("primaryGroupID")
? 'Primary group name = ' + SidToName($SID)
Get $x
-Erik
|
Top
|
|
|
|
#130345 - 2004-12-01 12:05 PM
Re: WinNT:// - Groupname from PrimaryGroupID ?
|
Jochen
KiX Supporter
Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
|
|
Top
|
|
|
|
#130346 - 2004-12-01 01:28 PM
Re: WinNT:// - Groupname from PrimaryGroupID ?
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Yes, the code does use the newer Win32Admin for Sid lookup etc. You have not really specified what you intend to do. All manipulation of "primary group" I have seen to date would be done via admin script. Would using the DLL be undesirable?
|
Top
|
|
|
|
#130348 - 2004-12-02 12:57 AM
Re: WinNT:// - Groupname from PrimaryGroupID ?
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
|
Here is Microsoft's code for this task.
Code:
On Error Resume Next
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D Set objUser = GetObject _ ("LDAP://cn=MyerKen,ou=Management,dc=NA,dc=fabrikam,dc=com") intPrimaryGroupID = objUser.Get("primaryGroupID") Set objConnection = CreateObject("ADODB.Connection") objConnection.Open "Provider=ADsDSOObject;" Set objCommand = CreateObject("ADODB.Command") objCommand.ActiveConnection = objConnection objCommand.CommandText = _ "<LDAP://dc=NA,dc=fabrikam,dc=com>;(objectCategory=Group);" & _ "distinguishedName,primaryGroupToken;subtree" Set objRecordSet = objCommand.Execute Do Until objRecordset.EOF If objRecordset.Fields("primaryGroupToken") = intPrimaryGroupID Then WScript.Echo "Primary group:" WScript.Echo objRecordset.Fields("distinguishedName") & _ " (primaryGroupID: " & intPrimaryGroupID & ")" End If objRecordset.MoveNext Loop objConnection.Close
|
Top
|
|
|
|
#130349 - 2004-12-02 09:48 PM
Re: WinNT:// - Groupname from PrimaryGroupID ?
|
kholm
Korg Regular
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
I have a KiX vesion of this: Code:
$Dom = @LDomain $usr = 'guest' $oUser = GetObject("WinNT://" + $Dom + "/"+ $usr + ",User" ) $RID = $oUser.Get("primaryGroupID") $PriName = GetGroupNameFromRID($RID) If Not @Error 'Primary group name: ' + $PriName EndIf Function GetGroupNameFromRID($RID) Dim $oConnection,$oCommand,$oRootDSE $oConnection = CreateObject("ADODB.Connection") $oConnection.Open("Provider=ADsDSOObject;") $oCommand = CreateObject("ADODB.Command") $oCommand.ActiveConnection = $oConnection $oRootDSE = GetObject("LDAP://RootDSE") $oCommand.CommandText = "<LDAP://" + $oRootDSE.Get("defaultNamingContext") + ">;(objectCategory=Group);Name,primaryGroupToken;subtree" $oRecSet = $oCommand.Execute While Not ($oRecSet.EOF Or $GetGroupNameFromRID) If $oRecSet.Fields("primaryGroupToken") = $RID $GetGroupNameFromRID = CStr($oRecSet.Fields("Name")) EndIf $oRecSet.MoveNext Loop $oConnection.Close Exit VarType($GetGroupNameFromRID) = 0 EndFunction
-Erik
|
Top
|
|
|
|
#130352 - 2004-12-21 04:02 PM
Re: WinNT:// - Groupname from PrimaryGroupID ?
|
Jochen
KiX Supporter
Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
|
Just for the records, here is what I came up with.
Using Richards udfBytes2Array (for being not too dependant) and a rip of one of the scripts Lonk links to, I created this little proof of concept scriptlet:
Code:
break on
$CurDomain = @ldomain
while $x <> "q"
cls
at(10, 2) "Enter Username..."
at(12, 3) gets $x
at(13, 3) "Primary Group = " + ADS_GetPrimaryGroup($x)
at(15, 5) "Q = Quit, any key = next" get $x
loop
exit 0
function ADS_GetPrimaryGroup($usr)
dim $root, $group, $PrimaryRID, $SID, $tmp, $i
$root = getobject("WinNT://" + $CurDomain + "/" + $usr + ",user")
if not @error
$PrimaryRID = $root.PrimaryGroupID
for each $group in $root.Groups
$SID = udfBytes2Array($group.Get("objectSID"))
for $i = ubound($sid) to ubound($sid)-3 step -1
$tmp = $tmp + dectohex(val($sid[$i]) / 16) + dectohex(val($sid[$i]) & 15)
next
if $PrimaryRID = HexToDec($tmp)
$ADS_GetPrimaryGroup = $group.Name
exit 0
endif
next
endif
endfunction
function HexToDec($Hex)
dim $HexArr, $Fac, $L, $Ch, $nb
$HexArr = "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"," "
If $Hex > "7FFFFFFF"
exit 87
EndIf
$HexToDec = 0
$Fac = 1
for $L = len($Hex) to 1 step -1
$Ch = substr($Hex, $L, 1)
$nb = 0
while $Ch <> $HexArr[$nb] and $nb < 16
$nb = $nb + 1
loop
if $nb = 16 ; Error in hex-string, contains non-valid characters
$HexToDec = 0
exit 87
endif
$HexToDec = $HexToDec + $nb * $Fac
$Fac = $Fac * 16
next
endfunction
function udfBytes2Array($abBytes)
dim $oStream,$adTypeBinary,$adTypeText,$i
$adTypeBinary = 1
$oStream = createobject("ADODB.Stream")
if @error exit @error endif
$oStream.Type=$adTypeBinary
$oStream.open
$oStream.Write($abBytes)
$oStream.Position = 0
redim preserve $udfBytes2Array[$oStream.Size-1]
for $i = 0 to ubound($udfBytes2Array)
$udfBytes2Array[$i] = asc($oStream.Read(1))
next
$oStream = 0
exit 0
endfunction
Special thanks to Erik Kaerholm for writing HexToDec udf back in 2001, which I took the freedom to adapt and use.
Suggestions and Critix welcome
_________________________
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 369 anonymous users online.
|
|
|