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 $allPrintQueues = @() $objPrintQueues = Get-ADObject -LDAPFilter "(objectClass=printQueue)" -SearchScope Subtree ` Foreach($printQueue in $objPrintQueues){ $PortNameCount=$printQueue.portName.Count $myObject = New-Object System.Object $allPrintQueues | Export-Csv -NoTypeInformation -Path "C:\Powershell Scripts\AD-Printer-Objects.csv" Write-Host -ForegroundColor Blue "All done."
import-module activedirectory
-ResultSetSize $null -Server "domain.com" -Properties * | Select-Object Name,Description,Location,ServerName,DistinguishedName,portName
$portNameTable = @{}
for($i=0;$i -lt $PortNameCount;$i++){
$portNameTable.add("PortName_$i",$printQueue.portName[$i])
}
$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
}
###################
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