Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Reading multi valued attributes from AD and writing into CSV.

$
0
0

Hello, I'm relatively new to powershell. I've been tasked with getting some data from Active Directory. I'm searching for objects of class: PrintQueue in Active Directory. One of the attributes of this object is "portName", which is a multi valued attribute.

The trouble is some objects only have one value in portName, while others can have more, as many as 5. I'm trying to write the portName attribute(s) in to a csv file and finding that my psobject doesnt like additional ones. Here is the script that I'm using.

###################

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

cls
import-module activedirectory

$allPrintQueues = @()

$objPrintQueues = Get-ADObject -LDAPFilter "(objectClass=printQueue)" -SearchScope Subtree `
-ResultSetSize $null -Server "domain.com" -Properties * | Select-Object Name,Description,Location,ServerName,DistinguishedName,portName

Foreach($printQueue in $objPrintQueues){

 $PortNameCount=$printQueue.portName.Count
 $portNameTable = @{}
 for($i=0;$i -lt $PortNameCount;$i++){
  $portNameTable.add("PortName_$i",$printQueue.portName[$i])
 }

 $myObject = New-Object System.Object
 $myObject | Add-Member -MemberType NoteProperty -Name Name -Value $printQueue.Name
 
 for($j=0;$j -lt $PortNameCount;$j++){
  $myObject | Add-Member -MemberType NoteProperty -Name "portName_$j" -Value $($portNameTable.get_Item("PortName_$j"))
 } 
 
 $allPrintQueues+=$myObject
 
}

$allPrintQueues | Export-Csv -NoTypeInformation -Path "C:\Powershell Scripts\AD-Printer-Objects.csv"

Write-Host -ForegroundColor Blue "All done."

 

 

 

###################

However, whats ends up happening is when I open the csv file, I see only two headers: Name and portName_0.

I would like to see portName_1, portName_2, portName_3... and so on for objects which have that many values in PortName attribute.

What am I doing wrong and how can this be fixed? Thanks a lot in advance for your help.

-G


Viewing all articles
Browse latest Browse all 10624

Trending Articles