Unwanted trailing spaces

rated by 0 users
This post has 4 Replies | 2 Followers

Top 200 Contributor
Posts 5
coulbc Posted: 01-13-2010 6:43 AM

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-null
foreach ($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

 

 

 

 

Top 10 Contributor
Posts 211
Top Contributor

Hi,

Can you try using Trim() function to remove the spaces..

Eg. $a = $a.Trim()

 

Regards,
Krishna
http://smtpport25.wordpress.com

 

Top 10 Contributor
Posts 211
Top Contributor

Hi,

Once you get the trimmed value you can try to append to $newline and display the same..

Hope this helps

 

Regards,
Krishna
http://smtpport25.wordpress.com

Top 200 Contributor
Posts 5
I found the actual problem. Instead of $results.proprties("propertyname)(0) use: $result.properties.item("propertyname").item(0) There may be more than one item in the atrribute if it is multivalued of course.
Top 10 Contributor
Posts 211
Top Contributor

Cool Thanks

Regards,
Krishna
http://smtpport25.wordpress.com

Page 1 of 1 (5 items) | RSS
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.