VBS脚本没有抓取所有AD组

时间:2016-02-10 20:14:19

标签: vbscript active-directory

最初编写以下脚本是为了抓取用户的所有AD组(来自提示)并将它们导出到txt文件。最近做了一些比较,发现脚本没有抓住所有的AD组。任何见解都将不胜感激。

strInput = UserInput( "Enter the source user's AD username:" )
strInput2 = UserInput( "Enter folder path to save the output usergroups.txt file, ex. c:\temp:" )
strFile = strinput & ".txt"
Set ADSysInfo = CreateObject("ADSystemInfo")
Set WshNetwork = CreateObject("WScript.Network")
Set objShell = WScript.CreateObject("WScript.Shell")
DomainString = WSHNetwork.UserDomain
Set UserObj = GetObject("WinNT://" & DomainString & "/" & strInput)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForAppending = 8

If objFSO.FileExists(strInput2 & strFile) Then
   Set objFolder = objFSO.GetFolder(strInput2)
Else
   Set objFile = objFSO.CreateTextFile(strInput2 & strFile)
   'Wscript.Echo "Just created " & strDirectory & strFile
End If 

set objFile = nothing
set objFolder = nothing

Set objTextFile = objFSO.OpenTextFile _
(strInput2 & strFile, ForAppending, True)

For Each GroupObj In UserObj.Groups
    'wscript.echo GroupObj.Name
    objTextFile.WriteLine(GroupObj.Name)

Next

objTextFile.Close
wscript.echo "Completed.  The usergroups.txt file is saved to " & strInput2 & "."

Function UserInput( myPrompt )
' This function prompts the user for some input.
' When the script runs in CSCRIPT.EXE, StdIn is used,
' otherwise the VBScript InputBox( ) function is used.
' myPrompt is the the text used to prompt the user for input.
' The function returns the input typed either on StdIn or in InputBox( ).
' Written by Rob van der Woude
' http://www.robvanderwoude.com
    ' Check if the script runs in CSCRIPT.EXE
    If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
        ' If so, use StdIn and StdOut
        WScript.StdOut.Write myPrompt & " "
        UserInput = WScript.StdIn.ReadLine
    Else
        ' If not, use InputBox( )
        UserInput = InputBox( myPrompt )
    End If
End Function

1 个答案:

答案 0 :(得分:0)

这可能与它有关:https://support.microsoft.com/en-us/kb/321538

为什么你还在使用WinNT提供商?这是一个老式的NT域名。对于Active Directory,最好使用LDAP。请注意,您不能直接使用域\用户名查找帐户。您必须在域中搜索sAMAccountName = username的帐户,然后拉出memberOf属性以查看该帐户所属的组。

或者像Ashigore建议的那样使用PowerShell。