question

ansred avatar image
0 Votes"
ansred asked ·

PowerShell Script "msExchMailboxGUID" converting "System.Byte[]"

Greetings,


Request:
Trying to run a PowerShell script in Exchange in order to generate CSV report to of users.

I have the following script and it's working fine, except the last attribute "msExchMailboxGUID" returned the output as "System.Byte[]"



 Import-Module ActiveDirectory 
     
 $FormatEnumerationLimit =-1
     
 $Users=get-aduser -SearchBase "DC=ans,DC=red" -filter * -Properties DisplayName, mail, DistinguishedName, objectGUID, msExchMailboxGUID
     
     
 # Setup the Table
 $table = New-Object System.Data.DataTable
 $table.Columns.Add("DisplayName","string") | Out-Null
 $table.Columns.Add("Mail","string") | Out-Null
 $table.Columns.Add("DistinguishedName", "string") | Out-Null
 $table.Columns.Add("objectGUID","string") | Out-Null
 $table.Columns.Add("msExchMailboxGUID","string") | Out-Null
 $userNumber = 1
     
     
 ForEach($user in $users) 
 {  
      # Define a new row object
      $r = $table.NewRow()
     
     
      #Add field to the row
      $r.DisplayName = $user.DisplayName
    
      # Add field to the row
      $r.Mail = $user.Mail
     
      # Add field to the row
      $r.DistinguishedName = $user.DistinguishedName
     
      # Add field to the row
      $r.objectGUID = $user.objectguid
          
      # Add field to the row
      $r.msExchMailboxGUID = $user.msExchMailboxGUID
     
      
        
     
      # Add the row to the table
      $table.Rows.Add($r)
      Write-Host "Processing user: " $userNumber
      $userNumber = $userNumber + 1
 }
     
     
 #$table | Format-table
 $table | Export-Csv -Path C:\AD-users-dump-exchange.csv -NoTypeInformation





Example of the output


79494-image.png





Help:
Can someone help please from the PowerShell community to tweak the script in order to convert correctly the HEXA msExchMailboxGUID into actual attribute value?


Thank you all!






windows-server-powershellwindows-active-directoryoffice-exchange-server-dev
image.png (6.9 KiB)
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ansred avatar image
0 Votes"
ansred answered ·

I thought about another way to extract the data, and I got it right this time.

Here is what I used

Using PowerShell


 $UserCredential = Get-Credential
    
 $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange-2019/PowerShell/ -Authentication Kerberos -Credential $UserCredential
    
 Import-PSSession $Session
    
 Get-Mailbox  | export-csv -NoTypeInformation C:\Mailboxes-all-2021.csv -Delimiter ";" -Encoding unicode




and I got a really good detailed report for all the mailbox and their AD attributes

79863-image.png






image.png (57.3 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

Your msExchMailboxGUID is an array and the CSV file doesn't support the array data type. You may write to a JSON file instead or convert the array to a string first

 $r.msExchMailboxGUID = $user.msExchMailboxGUID -join ','

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Ian,

I see. Any idea how/what to change in the script to get the correct output please?

Thanks

0 Votes 0 ·

You can append -join ',' to line 37 like above. It concatenates the elements of the array msExchMailboxGUID into a single string.

0 Votes 0 ·

I got the following output, not the actual mailbox guid


79834-image.png




 "DisplayName","Mail","DistinguishedName","objectGUID","msExchMailboxGUID"
 "Administrator","Administrator@ans.red","CN=Administrator,CN=Users,DC=ans,DC=red","bcc42c9b-6079-476c-b3b4-e4d649f36c59","148,154,214,40,52,40,235,75,182,163,176,230,58,237,115,218"



I am looking for seeing the actual mailbox guid.

Regards,

0 Votes 0 ·
image.png (7.3 KiB)
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

Have you tried something simple?

 $r.msExchMailboxGUID = $user | Select ExchangeGUID

Or, if you prefer something more complicated:

 $string = $user.msExchMailboxGuid
 $bytes = [System.Text.Encoding]::Unicode.GetBytes($string)
 $r.msExchMailboxGUID = [System.Text.Encoding]::ASCII.GetString($bytes)

Using the simplest, more readable, method is usually best. It makes your intention explicit and understandable. :-)

· 6 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Firs one:
I got the following output


79833-image.png



 "DisplayName","Mail","DistinguishedName","objectGUID","msExchMailboxGUID"
 "Administrator","Administrator@ans.red","CN=Administrator,CN=Users,DC=ans,DC=red","bcc42c9b-6079-476c-b3b4-e4d649f36c59","@{ExchangeGUID=Microsoft.ActiveDirectory.Management.ADPropertyValueCollection}"



Second one:
I got the some errors about the script

Exception calling "GetBytes" with "1" argument(s): "Array cannot be null.
Parameter name: chars"
At line:21 char:1
+ $bytes = [System.Text.Encoding]::Unicode.GetBytes($string)


I would appreciate the simple way to edit the script if you kindly can help.

Thanks

0 Votes 0 ·
image.png (8.8 KiB)

Try changing the "Select ExchangeGUID" to "Select -Expand ExchangeGUID".

But I have a question: Why are you using Get-ADUser for this work instead of the Exchange Get-Mailbox cmdlet? Use the Exchange Management Shell.


0 Votes 0 ·

Ok, sometimes "simple" can be complex! Try this (simpler) version if you want to stick to .Net:

 $y = new-object System.GUID $user.msExchMailboxGuid


The problem you may run into using that is that PowerShell loves to "flatten" arrays. If you get an error that says that "new-object : Cannot find an overload for "Guid" and the argument count: "16"". If that happens try this (a.k.a. using the "magic comma"!):

 $y = new-object System.GUID @(,$user.msExchMailboxGuid)


0 Votes 0 ·
ansred avatar image ansred RichMatheisen-8856 ·

Not sure, but both didn't work.

FYI, just replaced line 37 with the lines you provided.

Do you have a script to run in the Exchange Shell that can give the same report structure for AD attributes + msExchMailboxGuid?

0 Votes 0 ·
Show more comments