views:

116

answers:

2

Ok, so it's not, but...

so this is a quick script I found on the internet which runs on my Exchange server and dumps a list of email addresses that I can use for recipient validation on a spam filter:

' Export all valid recipients (= proxyAddresses) into a
' file virtual.txt
'
' Ferdinand Hoffmann & Patrick Koetter
' 20021100901
' Shamelessly stolen from 
' http://www.microsoft.com/windows2000/techinfo/ \
' planning/activedirectory/bulksteps.asp


'Global variables
Dim Container
Dim OutPutFile
Dim FileSystem

'Initialize global variables
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set OutPutFile = FileSystem.CreateTextFile("virtual.txt", True)
Set Container=GetObject("LDAP://CN=Users,DC=office,DC=example,DC=com")

'Enumerate Container
EnumerateUsers Container

'Clean up
OutPutFile.Close
Set FileSystem = Nothing
Set Container = Nothing

'Say Finished when your done
WScript.Echo "Finished"
WScript.Quit(0)

'List all Users
Sub EnumerateUsers(Cont)
Dim User

'Go through all Users and select them
For Each User In Cont
Select Case LCase(User.Class)

'If you find Users
Case "user"
  'Select all proxyAddresses
  Dim Alias
  If Not IsEmpty(User.proxyAddresses) Then
    For Each Alias in User.proxyAddresses
    OutPutFile.WriteLine "alias: " & Alias
    'WScript.Echo Alias
  Next
  End If

Case "organizationalunit" , "container"
  EnumerateUsers User

End Select
Next
End Sub

The catch is that the list of recipients comes back like this:

smtp:[email protected]
SMTP:[email protected]
x400:c=US;a= ;p=local;o=Exchange;s=lastname;g=firstname;
smtp:[email protected]
smtp:[email protected]

The spam filter has an import scrip that only imports lines with "smtp" or "SMTP" prefixed so the x400 isn't an issue. What is an issue is that I don't want the VBscript exporting the "[email protected]" address. I've tried this:

'List all Users
Sub EnumerateUsers(Cont)
Dim User

'Go through all Users and select them
For Each User In Cont
Select Case LCase(User.Class)

'If you find Users
Case "user"
  'Select all proxyAddresses
  Dim Alias
  If Not IsEmpty(User.proxyAddresses) Then
    For Each Alias in User.proxyAddresses
    If Not Alias = "*.lan" Then
        OutPutFile.WriteLine "alias: " & Alias
        WScript.Echo Alias
    End If
  Next
  End If

Case "organizationalunit" , "container"
  EnumerateUsers User

End Select
Next
End Sub

But, that doesn't do anything. I've tried matching for the public domain (If Alias = "publicdomain" Then) but that didn't produce any results.

So, how do I filter the output so I only get addresses ont he public domain?

A: 

You could use a regular expression to filter out lines that don't match your criteria. Something like the following.

smtp:.*@publicdomain\.com

Alternatively you could also tweak your LDAP query to only return users of a certain OU. Is there an AD group that only users with exchange accounts belong in?

Here's the VBS for RegEx matching...

Dim s : s = "smtp:[email protected]" & VBCRLF & _
 "SMTP:[email protected]" & VBCRLF & _
 "x400:c=US;a= ;p=local;o=Exchange;s=lastname;g=firstname;" & VBCRLF & _
 "smtp:[email protected]" & VBCRLF & _
 "smtp:[email protected]"

Dim ex : ex = "smtp:.*@publicdomain\.com"

Dim oRE: Set    oRE = New RegExp
  oRE.IgnoreCase = True
  oRE.Global = True
  oRE.Pattern = ex

Dim matches : Set matches = oRE.Execute(s)

For Each match In matches
    WScript.Echo match.Value
Next
CptSkippy
+1  A: 

Replace

If Not Alias = "*.lan"

with

If Right(Alias, 4) <> ".lan"

(It can be done with regular expressions but it's Friday and I'm tired!)

Ken Keenan