如何使用 az cli 获取 Outlook 电子邮件?

问题描述 投票:0回答:1

我正在尝试使用 az cli 获取 Outlook 电子邮件

 $userEmail = "[email protected]"
 $folderName = "Inbox" # Specify the folder name you want to retrieve emails from
 $outputFile = "emails.xlsx" # Specify the Excel file where you want to store the emails

 $outlookMessages = Get-AzOutlookMessage -MailboxEmailAddress $userEmail -FolderName $folderName

 $excel = New-Object -ComObject Excel.Application
 $workbook = $excel.Workbooks.Add()
  $sheet = $workbook.Worksheets.Item(1)

它给出错误,术语“Get-MgAuthenticationToken”未被识别为 cmdlet 的名称。

我需要安装哪个模块或者有更简单的方法吗?

powershell
1个回答
0
投票

从管理员提升的 PowerShell 运行

Install-Module Microsoft.Graph.Authentication
以安装 Microsoft Graph PowerShell 身份验证模块

这需要使用 Mail.Read

 配置 
M365 应用程序注册,您还应该听取建议并“配置 应用程序访问策略 以限制应用程序访问特定邮箱,而不是组织中的所有邮箱。

此处的 PowerShell 示例需要将 M365 租户 ID、企业应用程序的客户端 ID 和客户端密钥传递给函数以获取用于 Graph 身份验证的令牌。

$M365ClientId = "abc";
$M365ClientSecret = "xyz";
$tenantId = "mnopqrstuz";
$folderName = "Inbox";
$mailboxUserId = "[email protected]";

Function Get-M365AccessToken (){
    $clientId = $args[0];     ### Pass in client id
    $clientSecret = $args[1]; ### Pass in client secret "value"
    $TenantId = $args[2];     ### Pass in tenantid
    
    Import-Module Microsoft.Graph.Authentication
    
    $body = @{
        grant_type = "client_credentials";
        client_id = $clientId;
        client_secret = $clientSecret;
        scope = "https://graph.microsoft.com/.default";
    }
    
    $response = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token -Body $body
    $accessToken = $response.access_token
    Return $accessToken; 
    };

$AccessToken = Get-M365AccessToken $M365ClientId $M365ClientSecret $tenantId;

Import-Module Microsoft.Graph.Authentication;
Connect-MgGraph -AccessToken $accessToken;

$messages = Get-MgUserMailFolderMessage -UserId $mailboxUserId -MailFolderId $folderName -Top 100;
$messages;
© www.soinside.com 2019 - 2024. All rights reserved.