旧电子邮件脚本改造

时间:2015-08-21 19:12:15

标签: vbscript active-directory ldap

我的老板要求更新我在此之前使用的旧脚本,以便将AD中的State字段添加到其中。以下是返回所有活动用户及其电子邮件地址的脚本。现在我只需要添加State

Const ADS_PROPERTY_NOT_FOUND  = &h8000500D
Const ADS_UF_ACCOUNTDISABLE = 2
Const strX400Search = "X400"

Set objRootDSE = GetObject("LDAP://rootDSE")
strDomain = objRootDSE.Get("defaultNamingContext")
strADPath = "LDAP://" & strDomain

Set objDomain = GetObject(strADPath)

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 5000

objCommand.CommandText = "<" & strADPath & ">" & _
  ";(&(|(objectClass=contact)(objectClass=group))  (mail=*))" & _
  ";distinguishedName,displayName,mail,proxyAddresses;subtree"

Set objRecordSet = objCommand.Execute

AddressCount = 0

varDisabledCounter = 0

objCommand.CommandText = "<" & strADPath & ">" & _
  ";(&(objectClass=user)(mail=*))" & _
  ";distinguishedName,displayName,mail,proxyAddresses;subtree"

Set objRecordSet = objCommand.Execute

strResult = strResult & "Name" & "," & "Email" & VbCrLf

While Not objRecordSet.EOF
  strUserDN = objRecordSet.Fields("distinguishedName")
  strUserDN=Replace(strUserDN,"/","\/")
  set objUser= GetObject("LDAP://"& strUserDN & "")

  If objUser.AccountDisabled = FALSE Then
    strResult = strResult & objUser.givenName & " " & objUser.sn & ","
    strResult = strResult & objUser.mail
    strResult = strResult &  VbCrLf
  End If

  objRecordSet.MoveNext
Wend

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile("C:\Email List.csv")
objOutputFile.Write strResult

LF=chr(10)
WScript.Echo "Done - Please Check C:\Email List.csv to see your file." & _
  LF &   LF & "If you have any questions please contact Kevin Reed"

1 个答案:

答案 0 :(得分:0)

这应该可以做到......

Const ADS_PROPERTY_NOT_FOUND  = &h8000500D
Const ADS_UF_ACCOUNTDISABLE = 2
Const strX400Search = "X400"

Set objRootDSE = GetObject("LDAP://rootDSE")
strDomain = objRootDSE.Get("defaultNamingContext")
strADPath = "LDAP://" & strDomain

Set objDomain = GetObject(strADPath)

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 5000

objCommand.CommandText = "<" & strADPath & ">" & _
  ";(&(|(objectClass=contact)(objectClass=group))  (mail=*))" & _
  ";distinguishedName,displayName,mail,proxyAddresses,st;subtree"

Set objRecordSet = objCommand.Execute

AddressCount = 0

varDisabledCounter = 0

objCommand.CommandText = "<" & strADPath & ">" & _
  ";(&(objectClass=user)(mail=*))" & _
  ";distinguishedName,displayName,mail,proxyAddresses,st;subtree"

Set objRecordSet = objCommand.Execute

strResult = strResult & "Name" & "," & "Email" & VbCrLf

While Not objRecordSet.EOF
  strUserDN = objRecordSet.Fields("distinguishedName")
  strUserDN=Replace(strUserDN,"/","\/")
  set objUser= GetObject("LDAP://"& strUserDN & "")

  If objUser.AccountDisabled = FALSE Then
    strResult = strResult & objUser.givenName & " " & objUser.sn & ","
    strResult = strResult & objUser.mail & ","
    strResult = strResult & objUser.st
    strResult = strResult &  VbCrLf
  End If

  objRecordSet.MoveNext
Wend

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFileSystem.CreateTextFile("C:\Email List.csv")
objOutputFile.Write strResult

LF=chr(10)
WScript.Echo "Done - Please Check C:\Email List.csv to see your file." & _
  LF &   LF & "If you have any questions please contact Kevin Reed"