locked
Sounds simple, right? RRS feed

  • Question

  • Hello everyone,

    The company that I work for has been bought out by another company (all good things to come).  One of the big differences is they are on Lotus Notes, we use exchange.  We have one network/domain, they have another.  We are not planning on merging our networks until the new year but until then, they want to have Distribution Lists that contain the users not on our domain (ie Bring the unique users from the Lotus Notes domain over to ours).

    I have been given LDAP access to one server for this task and I have used ldp.exe to query the list of users that fit this filter.

    (&(|(objectclass=dominoPerson)(objectclass=dominoServerMailInDatabase))(companyname=banana))

    To this end I have been tasked with getting the users of the other domain that fit this LDAP filter. So I scouted the net and found some helpful hints/tips and so far this is what I have:

    set-executionpolicy unrestricted
    import-module ActiveDirectory
    import-module S.DS.P
    $attrib ="mail"
    $conn = "LDAP://123.456.789.100:389"
    $entry = New-Object System.DirectoryServices.DirectoryEntry($conn,"wpsbind","wpsbind","none")
    $LDAPfilter = "(&(|(objectclass=dominoPerson)(objectclass=dominoServerMailInDatabase))(Mail=*)(companyname=*banana*))"
    $directorySearcher = New-Object System.DirectoryServices.DirectorySearcher($entry,$LDAPfilter)
    $results = $directorySearcher.FindAll()
     
    foreach($result in $results){
            foreach($propertyName in $result.Properties.PropertyNames){
                foreach($property in $result.Properties["$propertyName"]){
                       $value = $property  
                Write-Host "$propertyName :: $value"  
                }
           }
                Write-Host "`n`n"
     }

    So far this works good but I just need to get the $propertyName that matches "mail" and "companyname"

    I would like this output in csv format so I can then use another powershell script to import these users into my AD (as contacts) so I can then create a distribution list from that.

    I did try

    return $result | foreach {$_.Properties.Item("$attrib")} and that does give me the list I want from about but I cannot figure out how to get that into a CSV format.

    I am fairly new to scripting and looking for any help you guys can offer.

    Thanks!



    • Edited by N3w2This Wednesday, July 8, 2015 10:52 PM
    Wednesday, July 8, 2015 10:51 PM

Answers

  • Start here:

    $conn = "LDAP://123.456.789.100:389"
    $entry = New-Object System.DirectoryServices.DirectoryEntry($conn,"wpsbind","wpsbind","none")
    $LDAPfilter = "(&(|(objectclass=dominoPerson)(objectclass=dominoServerMailInDatabase))(Mail=*)(companyname=*banana*))"
    $searcher = New-Object System.DirectoryServices.DirectorySearcher($entry,$LDAPfilter)
    $searcher.PropertiesToLoad.AddRange(@('mail','Company'))
    $searcher.FindAll() |
        Select @{N='Mail';E={$_.Properties['mail'][0]}},@{N='CompanyName';E={$_.Properties['company'][0]}} |
        Export=Csv myusers.csv
     
    


    \_(ツ)_/

    • Marked as answer by N3w2This Thursday, July 9, 2015 3:39 PM
    Thursday, July 9, 2015 12:21 AM

All replies

  • Start here:

    $conn = "LDAP://123.456.789.100:389"
    $entry = New-Object System.DirectoryServices.DirectoryEntry($conn,"wpsbind","wpsbind","none")
    $LDAPfilter = "(&(|(objectclass=dominoPerson)(objectclass=dominoServerMailInDatabase))(Mail=*)(companyname=*banana*))"
    $searcher = New-Object System.DirectoryServices.DirectorySearcher($entry,$LDAPfilter)
    $searcher.PropertiesToLoad.AddRange(@('mail','Company'))
    $searcher.FindAll() |
        Select @{N='Mail';E={$_.Properties['mail'][0]}},@{N='CompanyName';E={$_.Properties['company'][0]}} |
        Export=Csv myusers.csv
     
    


    \_(ツ)_/

    • Marked as answer by N3w2This Thursday, July 9, 2015 3:39 PM
    Thursday, July 9, 2015 12:21 AM
  • Using AD:

    Import-Module ActiveDirectory
    get-aduser -LDAPfilter '(&(|(objectclass=dominoPerson)(objectclass=dominoServerMailInDatabase))(companyname=banana))'  -Properties mail,company|select mail,company


    \_(ツ)_/

    Thursday, July 9, 2015 12:23 AM
  • $conn = "LDAP://123.456.789.100:389"
    $entry = New-Object System.DirectoryServices.DirectoryEntry($conn, "wpsbind", "wpsbind", "none")
    $filter = "(&(|(objectclass=dominoPerson)(objectclass=dominoServerMailInDatabase))(Mail=*)(companyname=*banana*))"
    $searcher = New-Object System.DirectoryServices.DirectorySearcher($entry, $filter)
    $searcher.PropertiesToLoad.AddRange(@('name', 'mail', 'company'))
    $users = @()
    foreach ($result in $searcher.FindAll()) {
        $user = New-Object -TypeName PSObject
        foreach ($property in $result.Properties.GetEnumerator()) {
            $user | Add-Member -NotePropertyName $($property.Name) -NotePropertyValue $($property.Value)
        }
        $users += $user
    }
    $users | Export-Csv banana.csv -NoTypeInformation

    Thursday, July 9, 2015 7:48 AM
  • That was spot on! Thanks for the help.
    Thursday, July 9, 2015 3:39 PM
  • I have tried this solution and it seems to have an issue with the NotePropertyName value.

    Add-Member : A parameter cannot be found that matches parameter name 'NotePropertyName'.
    +         $user | Add-Member -NotePropertyName <<<<  $($property.Name) -NotePropertyValue $($property.Value)
        + CategoryInfo          : InvalidArgument: (:) [Add-Member], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.AddMemberCommand

    Thursday, July 9, 2015 3:41 PM
  • NotePropertyName was introduced in PowerShell 3.0, which means you are using PowerShell 1.0 or 2.0.

    It works if you upgrade to PowerShell 3.0 (or later) or change this:

    $user | Add-Member -NotePropertyName $($property.Name) -NotePropertyValue $($property.Value)

    to this:

    $user | Add-Member -MemberType NoteProperty -Name $($property.Name) -Value $($property.Value)

    Thursday, July 9, 2015 3:45 PM
  • That is the hard way to do a simple thing.  If you really want scatter-gather then try it this way:

    $users=foreach ($result in $searcher.FindAll()) {
        $user=@{}
        foreach ($property in $result.Properties.GetEnumerator()) {
            $user.Add($property.Name,$property.Value)
        }
        New-Object PsObject -Property $user 
    }


    \_(ツ)_/


    • Edited by jrv Thursday, July 9, 2015 4:04 PM
    Thursday, July 9, 2015 4:00 PM
  • This is my method for doing this:

    $searcher.FindAll() |
        ForEach-Object(
            foreach ($property in $_.Properties.GetEnumerator()) {
                $user.Add($property.Name,$property.Value)
            }
            New-Object PsObject -Proeprty $user
        } |
        Export-Csv file.csv
    
    


    \_(ツ)_/

    Thursday, July 9, 2015 4:03 PM
  • Got this error:

    Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'Add'.

    Nevermind, just noticed you changed it to hashtable. And yes, I agree. Creating hashtable and adding to psobject looks better.

    I went for the scatter-gather approach, as you called it, to avoid maintaining lists of attributes two places in the code. If OP wants department and title for example, I would add it to PropertiesToLoad. Less code to change.

    Thursday, July 9, 2015 4:08 PM
  • $conn = "LDAP://123.456.789.100:389"
    $entry = New-Object System.DirectoryServices.DirectoryEntry($conn, "wpsbind", "wpsbind", "none")
    $filter = "(&(|(objectclass=dominoPerson)(objectclass=dominoServerMailInDatabase))(Mail=*)(companyname=*banana*))"
    $searcher = New-Object System.DirectoryServices.DirectorySearcher($entry, $filter)
    $searcher.PropertiesToLoad.AddRange(@('name', 'mail', 'company'))
    $searcher.FindAll() | ForEach-Object {
        $user = @{}
        $_.Properties.GetEnumerator() | ForEach-Object {
            $user.Add($_.Name, $_.Value[0])
        }
        New-Object PSObject -Property $user
    } | Export-Csv banana.csv -NoTypeInformation
    This works quite well.
    Thursday, July 9, 2015 9:19 PM