Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

AD export script does not write all values during call to Export-CSV

$
0
0

I wrote a powershell script to extract information from AD. For some reason extensionattribute10 and extensionattribute11 do not show up in the export and I'm baffled as to why. I know other ways to export data but I need to perform additional processing of the results so this is why I'm trying this method.

 

#Export users from Active Directory.
#version 1.30

#Add the AD attributes to query in the first column and the name of the Excel header column in the second.
$ADPropertyList += ,@('distinguishedname','distinguishedname')
$ADPropertyList += ,@('sn','Last name')   
$ADPropertyList += ,@('givenName','First name')  
$ADPropertyList += ,@('displayname','Display Name')
$ADPropertyList += ,@('mail','Email')
$ADPropertyList += ,@('telephonenumber','Telephone') 
$ADPropertyList += ,@('samAccountname','Login Name')
$ADPropertyList += ,@('manager','manager')
$ADPropertyList += ,@('title','Title')
$ADPropertyList += ,@('division','Organization')  
$ADPropertyList += ,@('company','Level 1-2')
$ADPropertyList += ,@('department','Level 3')
$ADPropertyList += ,@('physicaldeliveryofficename','Level 4')
$ADPropertyList += ,@('extensionattribute10','Level 5')
$ADPropertyList += ,@('extensionattribute11','Level 6')   

[string]$outfile = "users.csv"
[string]$attribute = ""
[string]$newline = ""
$NewCSVObject = @()

#Delete the output file if it already exists.
[system.IO.File]::Delete($outfile)

#Sort the results in ascending order using the surname attribute.
[system.directoryservices.sortoption]$so | Out-Null
$so = new-object system.directoryservices.sortoption
$so.direction = [system.DirectoryServices.SortDirection]::Ascending
$so.propertyname = "sn"
$objDomain = New-Object DirectoryServices.DirectoryEntry("LDAP://OU=All SSD,dc=carver,dc=labs,dc=org")
$searcher = new-object DirectoryServices.DirectorySearcher
$searcher.searchroot = $objDomain
[string]$searchfilter = "(&(objectcategory=person)(objectclass=user))"

foreach ($property in $ADPropertylist)
 {
 $searcher.propertiestoload.add($property[0])
 }

$searcher.filter = $searchfilter
$searcher.sort = $so
$searcher.SizeLimit = 8000
$searcher.PageSize = 8000
[system.DirectoryServices.SearchResultCollection]$Results = $searcher.FindAll()

[directoryservices.SearchResult]$Result | out-null
#Process the results returned from the directory searcher.

foreach ($Result in $Results)
{
 #$newline = ""
 $obj = new-object PSObject 
 Foreach ($ADPropName in $ADPropertylist)
  {
  if ($result.properties.contains($ADPropName[0]))
  {
  $newline = $result.properties.Item($ADPropName[0]).item(0)
  $obj | add-member -membertype NoteProperty -name $ADPropName[1] -value $newline
   if ($ADPropName[1] -eq "Level 6")
   {
   write-host "HIT"
   $newline
   }
  }
 }
 $NewCSVObject += $obj
 $obj = $null
 
}
 $NewCSVObject | export-csv "ExportData.csv" -NoTypeInformation
 


Viewing all articles
Browse latest Browse all 6937

Trending Articles