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!