Value of custom atttribute depending existences.

hi there :slight_smile:

Get-ADUser -Filter * -Properties * -searchbase “ou=res migratie,ou=divisies,dc=network,dc=local” | % {
New-Object PSObject -Property @{
UserName = $.DisplayName
EmployeeID = $
.EmployeeID
UPN = $.UserPrincipalName
PrimarySMTP= $
.EmailAddress
RecipientType = $(If (get-mailbox -identity $.userprincpalname) {( (get-mailbox $.userprincpalname).RecipientType) } Else { “Empty” })
LitigationHoldEnabled = get-mailbox -identity $.userprincpalname
CustomAttribute15 = $
.extensionAttribute15
Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join “,”
}
} | Select UserName,EmployeeID,UPN,PrimarySMTP,RecipientType,CustomAttribute15,Groups | Export-Csv C:\temp\ADreport.csv -NTI

 

Hi

 

In the red sentence I try to fill attribute Receipttype with value what is dependent of the existence of the mailboxobject.

I got the error message :

Cannot process argument transformation on parameter ‘Identity’. Cannot convert the “System.Collections.ArrayList” value of type “System.Collections.ArrayList” to type “Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter”.

  • CategoryInfo : InvalidData: (:slight_smile: [Get-Mailbox], ParameterBindin…mationException
  • FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-Mailbox
  • PSComputerName : outlook.office365.com

 

I tried a simple line what you see in blue. But unfortunately that gives the same error message.

It sas something about converting, but with my knowledge I say “dumb thing what conevertion” :slight_smile:

So guys what do I here wrong?

So purpose is to put a value in a customobject if a value of object (n this case mailbox) exists.

 

 

Is the typo for userprincipalname also present in your code?

RecipientType = $(If (get-mailbox -identity $_.userprincpalname) {( (get-mailbox $_.userprincpalname).RecipientType) } Else { “Empty” })
LitigationHoldEnabled = get-mailbox -identity $_.userprincpalname

$_.userprincIpalname

There are multiple things that could use improvement

  • Only return properties you need - Using Properties * returns ALL properties, so you're returning like 90 properties and using 6. This takes longer and takes up much more memory.
  • Avoid unnecessary GETs - If Get-Mailbox lookup fails, it would be NULL, which is empty. When you find an object by Id, it will cause a failure. The error you posted basically is saying the value you are passing is an array and not an identifier for a mailbox. if you are doing it in a loop, it will cause an error. Even in the code below, it's doing 2 GETs on a mailbox, which could taking more time, but you can give it a try as the calculated expression will not report an error. Test with a couple of accounts first then do the full query
  • Parse over another GET - The group code you posted is doing a GET on the group. All you need is the name, which is contained in the DistinguishedName, so you can just parse it rather than another lookup
$results = Get-ADUser -Filter * -Properties DisplayName,EmployeeId,UserPrincipalName,EmailAddress,extensionAttribute15 -searchbase “ou=res migratie,ou=divisies,dc=network,dc=local” | 
           Select-Object @{Name='UserName';Expression={$_.DisplayName}},
                         EmployeeId,
                         @{Name='UPN';Expression={$_.UserPrincipalName}},
                         @{Name='PrimarySMTP';Expression={$_.EmailAddress}},
                         @{Name='RecipientType';Expression={Get-Mailbox -Identity $_.userprincpalname | Select -ExpandProperty RecipientType}},
                         @{Name='LitigationHoldEnabled';Expression={Get-Mailbox -Identity $_.userprincpalname | Select -ExpandProperty LitigationHoldEnabled}},
                         @{Name='CustomAttribute15';Expression={$_.extensionAttribute15}},
                         @{Name='Groups';Expression={$_.memberof | foreach{($_ -split ',')[0].Replace('CN=','')}}}

Doug!!!,

Im a shamed brother, very deeeeep

I’ve made a appointment with the opticien…

Hey site master give this champ 100000000 points and me the same but starting with a “-”

Thanks and take care of your self’s

 

 

 

Hey Rob bro,

The same what I said to Doug.

I was watching and watching as cow looking at passing train but didnt sas the typo where Doug referred to.

Thanks friend for you help and will follow your advices.

Wishing you the best.

 

Don’t be ashamed! It happens to us all. Usually just me explaining it to another person leads to me discovering something I overlooked for days or weeks. It’s helpful to have someone just to be a second pair of eyes… or an entire forum. Cheers!