I'm trying to create a special AD dump. When I output the results a leading/trailing space is being added to each attribute I export. Why is this happening? The first value has a trailing space and each one after that has a leading and trailing space.
#Get the LDAP path to a computer in the Active Directory.
[string]$AdDomain = [system.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()$searcher = new-object DirectoryServices.DirectorySearcher("ldap://" + $AdDomain)[string]$searchfilter = "(&(objectcategory=person)(objectclass=user)(extensionattribute1=c)(mail=*arch.avs.com))"$searcher.PropertiesToLoad.Add("sn") | Out-Null$searcher.PropertiesToLoad.Add("givenName") | Out-Null$Searcher.PropertiesToLoad.Add("telephonenumber") | Out-Null$searcher.PropertiesToLoad.Add("whencreated") | Out-Null$searcher.PropertiesToLoad.Add("displayname") | Out-Null$searcher.PropertiesToLoad.Add("department") | Out-Null$searcher.PropertiesToLoad.Add("streetaddress") | Out-Null$searcher.PropertiesToLoad.Add("canonicalname") | Out-Null$searcher.PropertiesToLoad.Add("mail") | Out-Null$searcher.PropertiesToLoad.Add("l") | Out-Null$searcher.PropertiesToLoad.Add("st") | Out-Null$searcher.PropertiesToLoad.Add("postalcode") | Out-Null$searcher.filter = $searchfilter
[system.DirectoryServices.SearchResultCollection]$Results = $searcher.FindAll()
Write-Host "Matching Records: " $Results.Count[string]$outfile = "c:\contractors.csv"[string]$newline[string]$delimiter="|"[directoryservices.SearchResult]$Result | out-nullforeach ($Result in $Results){
if ($Result.Properties.Contains("sn")) { $newline = $result.Properties.Item("sn") + $delimiter } else { $newline = $delimiter }if ($Result.Properties.Contains("givenname")) { $newline=$newline+$result.Properties.Item("givenname") + $delimiter } else { $newline = $newline + $delimiter }if ($Result.Properties.Contains("telephonenumber")) { $newline = $newline + $Result.Properties.Item("telephonenumber") } else { $newline = $newline + $delimiter }#$newline = $newline + [system.Environment]::NewLine[system.IO.File]::AppendAllText($outfile,$newline)Write-Host $newline}
Output: lastname | firstname | telephonenumber I want it to be: lastname|firstname|telephonenumber
lastname | firstname | telephonenumber
I want it to be:
lastname|firstname|telephonenumber
Hi,
Can you try using Trim() function to remove the spaces..
Eg. $a = $a.Trim()
Regards,Krishnahttp://smtpport25.wordpress.com
Once you get the trimmed value you can try to append to $newline and display the same..
Hope this helps
Cool Thanks