• 締切済み

所属グループ取得方法

こんにちは windows2003サーバーにて addusers_x86.exeを使い、各ユーザー、各グループは 取得できるのですが、 どのユーザーがどのグループに所属しているのか、 を知りたい場合、何か手段はありますでしょうか。

みんなの回答

  • junkUser
  • ベストアンサー率56% (218/384)
回答No.1

サンプルスクリプトを作ってみました。 cscript ファイル名.vbs で実行してください。 --- Option Explicit ' CScriptで実行してください Dim WshShell Dim strEnvDnsDomain, arrItem, baseDN, i Dim objConnection, objCommand, objRecordSet, strCommandText Dim strUserName, strUserDN, objUser, strEntry, strInfo Dim gidCache(5,2), intPGID, frag, cachePoint ' DNSドメインを取得しbaseDNを設定 Set WshShell = CreateObject("WScript.Shell") strEnvDnsDomain = WshShell.ExpandEnvironmentStrings("%USERDNSDOMAIN%") If Left(strEnvDnsDomain, 1) = "%" Then WScript.Echo "操作しているPCはActive Directoryドメインに参加していないようです。" & vbNewLine & "終了します。" WScript.Quit End If arrItem = Split(strEnvDnsDomain, ".") baseDN = "" For i = 0 to UBound(arrItem) If i = 0 Then baseDN = "DC=" & arrItem(i) Else baseDN = baseDN & ",DC=" & arrItem(i) End If Next ' DCに接続 Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" objCommand.ActiveConnection = objConnection strCommandText = "<LDAP://" & baseDN & ">;" & _ "(&(objectClass=user)(!objectClass=computer));" & _ "distinguishedName,sAMAccountName;" & _ "subtree" objCommand.CommandText = strCommandText Set objRecordSet = objCommand.Execute ' gidCache 構築 For i = 0 To 4 gidCache(i, 0) = -1 gidCache(i, 1) = vbNullString Next cachePoint = 0 WScript.Echo "--- BEGIN ---" Do Until objRecordset.EOF strUserName = objRecordset.Fields("sAMAccountName") strUserDN = objRecordset.Fields("distinguishedName") strInfo = strUserName Set objUser = GetObject("LDAP://" & strUserDN ) objUser.GetInfo intPGID = objUser.Get("primaryGroupID") frag = 0 For i = 0 To cachePoint If gidCache(i, 0) = intPGID Then frag = 1 strInfo = strInfo & vbNewLine & vbTab & gidCache(i, 1) End If Next If frag = 0 Then gidCache(cachePoint, 0) = intPGID gidCache(cachePoint, 1) = GetPrimaryGroup(intPGID) strInfo = strInfo & vbNewLine & vbTab & gidCache(cachePoint, 1) cachePoint = cachePoint + 1 If cachePoint >= 5 Then cachePoint = 0 End If On Error Resume Next arrItem = objUser.GetEx("memberOf") If Err.Number = 0 Then For Each strEntry In arrItem strInfo = strInfo & vbNewLine & vbTab & strEntry Next End If On Error Goto 0 Set objUser = Nothing objRecordset.MoveNext WScript.Echo strInfo Loop objConnection.Close Set objCommand = Nothing WScript.Echo "--- END ---" WScript.Quit ' プライマリグループのDNを取得する Function GetPrimaryGroup(PGID) Dim strPrimaryPath, objRS, count Dim ADCon, ADCom, strComText Set ADCon = CreateObject("ADODB.Connection") Set ADCom = CreateObject("ADODB.Command") ADCon.Provider = "ADsDSOObject" ADCon.Open "Active Directory Provider" ADCom.ActiveConnection = objConnection strComText = "<LDAP://" & baseDN & ">;" & _ "(&(objectClass=group));" & _ "distinguishedName,primaryGroupToken;" & _ "subtree" ADCom.CommandText = strComText Set objRS = ADCom.Execute strPrimaryPath = "" count = 0 Do Until objRS.EOF strPrimaryPath = objRS.Fields("distinguishedName") If objRS.Fields("primaryGroupToken") = PGID Then count = count + 1 Exit Do End If objRS.MoveNext Loop If count < 1 Then strPrimaryPath = "Error: Primary Group Not Found" GetPrimaryGroup = strPrimaryPath End Function ---

関連するQ&A

専門家に質問してみよう