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

AnsRed 96 Reputation points
2021-03-19T07:28:38.067+00:00

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!

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,932 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
516 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,389 questions
0 comments No comments
{count} votes

Accepted answer
  1. AnsRed 96 Reputation points
    2021-03-21T02:50:36.323+00:00

    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

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,376 Reputation points Microsoft Vendor
    2021-03-19T08:48:42.947+00:00

    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.


  2. Rich Matheisen 45,096 Reputation points
    2021-03-19T14:18:08.227+00:00

    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. :-)


  3. David Blackmore 1 Reputation point
    2021-08-17T19:48:50.533+00:00

    If you needed to stick to the ADSI format and didn't want to pull from another source, you could do the following:

    $msExchMailboxGUID = (New-Object -TypeName System.Guid -ArgumentList @(,$user.msExchMailboxGUID)).ToString()

    I found this at:
    https://stackoverflow.com/a/30057480

    0 comments No comments