How generate CSV file after data retrieved

Federico Coppola 1,181 Reputation points
2023-04-17T17:41:20.0866667+00:00

Hi all,
I am working with PowerShell and Microsoft Graph to handle company MS Exchange mailbox contacts.
In this moment I need to download contacts of some company employees.
In this moment I can access with success to employees mailbox, after I created Microsoft App in my Azure tenant.

# Connecting to Azure Parameters
$tenantID = "XXXXX"
$applicationID = "XXXXX"
$clientKey = "XXXX"
#$FilePersonalContacts= "C:\OutlookSignature\Output\PersonalContacts.csv"
$PersonalID='mike.green@company.com'
$IDContact=""

# Authenticate to Microsoft Grpah
Write-Host "Authenticating to Microsoft Graph via REST method"
 
$url = "https://login.microsoftonline.com/$tenantId/oauth2/token"
$resource = "https://graph.microsoft.com/"
$restbody = @{
         grant_type    = 'client_credentials'
         client_id     = $applicationID
         client_secret = $clientKey
         resource      = $resource
}
     
# Get the return Auth Token
$token = Invoke-RestMethod -Method POST -Uri $url -Body $restbody
     
# Set the baseurl to MS Graph-API (v 1.0)
$baseUrl = 'https://graph.microsoft.com/v1.0'
         
# Pack the token into a header for future API calls
$Header = @{
          'Authorization' = "$($Token.token_type) $($Token.access_token)"
         'Content-type'  = "application/json"
}


$url = $baseUrl + '/users/'+ $PersonalID+ '/contacts'
write-host $url

$usersList = Invoke-RestMethod -Method GET -headers $header -Uri $url

$allPages = @()

$uri = "https://graph.microsoft.com/v1.0/users/" + $PersonalID + "/contacts?`$top=5"
Write-Host $uri

$aadUsers = (Invoke-RestMethod -Uri $uri -Headers $Header -Method Get -ContentType "application/json")
$allPages += $aadUsers.value

if ($aadUsers.'@odata.nextLink') {

        do {

            $aadUsers = (Invoke-RestMethod -Uri $aadUsers.'@odata.nextLink' -Headers $Header -Method Get -ContentType "application/json")
            $allPages += $aadUsers.value

        } until (
            !$aadUsers.'@odata.nextLink'
        )
        
}

$aadUsers = $allPages
$aadUsers | Select givenName,surname,businessPhones,emailAddresses

I need to create CSV file after personal mailbox contacts collection.
I wasn't able to generate CSV because I collected this kind of data:

givenName  surname      businessPhones   emailAddresses                                                            
--------- -------      --------------   --------------                                                            
Mike       Green       {003931234677}  {@{name=Mike Green; address=mike.green@company.com}}

I need to generate CSV file with givenName, surname, businessPhones, address (emailAddresses -> address) How can I extract and populate CSV with also address (emailAddresses -> address) data? Is it possible? Thanks a lot!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,254 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,666 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,582 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,076 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,811 Reputation points
    2023-04-17T19:10:23.76+00:00

    The emailAddresses may contain more than one address. A CSV isn't able to deal with structured data; it only holds strings.

    Give this a try and see if it does what you want:

    $aadUsers |
        Select-Object givenname,surname,@{n='emailAddresses';e={($_.emailAddresses|foreach-object{$_.address}) -join ';'}} |
            Export-CSV -Path <path-to-csv> -NoTypeInfo
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.