none
Error: "Method invocation failed because [System.Object[]] doesn't contain a method named 'replace'."

    Question

  • Hiho,

    I am pretty new to PS so maybe what I try I try the wrong way. I just modified a script from Technet to get some values out of AD and write it to a csv. It's working fine except I have CRLF after every value which breaks my list.

    $strFilter = "(objectClass=User)"

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objOU = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Users,OU=Users & Contacts,DC=Domain,DC=com")

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher

    $objSearcher.SearchRoot = $objOU

    $objSearcher.PageSize = 1000

    $objSearcher.Filter = $strFilter

    $objSearcher.SearchScope = "Subtree"

     

    $colProplist = "givenname","sn","title","homephone","mail","department","physicaldeliveryofficename"

    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

     

    $colResults = $objSearcher.FindAll()

     

    $tab="`t"

    $title = "First Name`tName`tPosition`tPhone`tEmail`tLab`tOffice"

    $title>"phonelist.csv"

     

    foreach ($objResult in $colResults)

        {$objItem = $objResult.Properties; $objItem.givenname; 

    $output = $objItem.givenname + $tab + $objItem.sn + $tab + $objItem.title + $tab + $objItem.homephone + $tab + $objItem.mail + $tab + $objItem.department + $tab + $objItem.physicaldeliveryofficename

    $output.replace("`n","")

    $output>>"phonelist.csv"

    }

     

     

    Error: "Method invocation failed because [System.Object[]] doesn't contain a method named 'replace'."

     

    Maybe it's not a "`n" I am searching for? How can I get rid of the CRLF that each $objItem.xyz seems to have at the end?

     

    Thx alot

    Morgenstern

    Wednesday, October 13, 2010 2:01 PM

Answers

  • You may want to give something like this a try.

    $searcher = New-Object DirectoryServices.DirectorySearcher
    $searcher.Filter = "(&(objectcategory=person)(objectclass=user))"
    $searcher.PageSize = 1000
    $results = $searcher.FindAll()
    
    $list = @()
    ForEach ($user in $results)
      {
        $list += New-Object PSObject -Property @{
          
        Name = [string]$user.properties.sn
        FirstName = [string]$user.properties.givenname
        Department = [string]$user.properties.department
        }
      }
      
    $list | Export-Csv phonelist.csv -NoTypeInformation
    

    Wednesday, October 13, 2010 3:33 PM