'ListNoManager.vbs 'June 10 Alan Kaplan for VISN 6 'alan dot kaplan at va dot gov Option Explicit Const ADS_NAME_INITTYPE_DOMAIN = 1 Const ADS_NAME_TYPE_UNKNOWN = 8 Const ADS_NAME_TYPE_CANONICAL = 2 Const ADS_NAME_TYPE_1779 = 1 Const ADS_SCOPE_SUBTREE = 2 Dim wshShell Dim fso,logfile, appendout Dim root, sDomain Dim retval Dim strManager, strDescription Dim strQuery, oRS Dim message Dim aTemp Dim oConn, oCommand Set wshShell = WScript.CreateObject("WScript.Shell") If (Not IsCScript()) Then 'If not CScript, re-run with cscript... dim quote, strArgs, i quote=chr(34) For i = WScript.Arguments.Count -1 to 0 Step -1 strArgs = WScript.Arguments(i) & Space(1) & straArgs Next WshShell.Run "CScript.exe " & quote & WScript.ScriptFullName & quote & space(1) & strArgs, 1, true WScript.Quit '...and stop running as WScript End If logfile = wshShell.ExpandEnvironmentStrings("%userprofile%") & "\desktop\NoManager_DistribGroups.xls" 'setup Log Const ForAppend = 8 Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(logfile) Then fso.DeleteFile logfile, True Set appendout = fso.OpenTextFile(logfile, ForAppend, True) appendout.writeline "Group Description AdsPath" 'Get the ADsPath for the domain to search. Set root = GetObject("LDAP://rootDSE") sDomain = root.Get("defaultNamingContext") sDomain = InputBox("This will get list of distribution lists with no manager. Query what domain:","Domain",sDomain) If sDomain = "" Then WScript.Quit Main '======= Functions and Subs Sub Main() wscript.echo "Searching " & sDomain Set oConn = CreateObject("ADODB.Connection") Set oCommand = CreateObject("ADODB.Command") oConn.Provider = "ADsDSOObject" oConn.Open "Active Directory Provider" Set oCommand.ActiveConnection = oConn oCommand.Properties("Page Size") = 1000 oCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE oCommand.Properties("Sort on") = "CN" ' pretty awful to figure out strQuery = ";" & _ "(&(objectCategory=group)(!managedBy=*)" & _ "((&(groupType:1.2.840.113556.1.4.803:=8)(!(groupType:1.2.840.113556.1.4.803:=-2147483648)))));cn,description, canonicalname,adspath;Subtree" oCommand.CommandText = strQuery Set oRS = oCommand.Execute oRS.MoveFirst Do Until oRS.EOF If IsArray(oRS.Fields("Description").value) Then aTemp = (oRS.Fields("Description").value) strDescription = aTemp(0) Else strDescription = oRS.Fields("Description").value End If EchoAndLog oRS.Fields("cn").Value & vbTab & strDescription & vbTab & oRS.Fields("adsPath") oRS.MoveNext Loop End Sub Sub EchoAndLog (message) 'Echo output and write to log Wscript.Echo message AppendOut.WriteLine message End Sub Function IsCScript() If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then IsCScript = True Else IsCScript = False End If End Function