【问题标题】:Microsoft GraphAPI PowerShell download attachmentMicrosoft Graph API PowerShell 下载附件
【发布时间】:2020-05-14 01:26:03
【问题描述】:

如何从 Microsoft Graph API for Mail 消息下载附件?

'$attachment=Get-MgUserMessageAttachment -MessageId AAMkAGIzZWEyN2MxLWJmNTktNDBkMi05ZWY2LWE3YjFhYWJjNmEwOABGAAAAAAA2OVriHzx8TL2ku-KbIgYgBwChRsRs-pG2QpRCaG5OA-T0FcAAA= -UserId abcd@gmail.com

$attachment
ContentType          : application/octet-stream
Id                   : AAMkAGIzZWEyN2MxLWJmNTktNDBkMi05ZWY2LWE3YjFhYWJjNmEwOABGAAAAAAA2OVriHzx8TL2ku-KbIgYgBwChRsRs-pG2QpRCaG5OgRLxAAAAAAEMAAChRsRs-pG2QpRCaG5OgRLxAAA-T0FcAAABEg
                       AQANUHCnnWvO5Dr-D2VilhKP8=
IsInline             : False
LastModifiedDateTime : 4/14/2020 7:21:32 PM
Name                 : report.csv
Size                 : 707'

【问题讨论】:

  • 请写下你的探索和你运行的命令看看结果?
  • 你能告诉我如何在 Microsoft Graph SDK PowerShell 模块中使用文件附件
  • 目前还不能
  • 明白了,谢谢,将在powershell中使用普通的图形api而不是模块

标签: powershell microsoft-graph-api


【解决方案1】:

Microsoft 图形 API 基于 Web API。因此,如果您想下载附件,您可以直接使用 HTTP GET 命令和 Microsoft Graph API Get Attachment API。

在 PowerShell 中,如果你想在本地使用 HTTP 调用,你可以使用Invoke-WebRequest(更多细节你可以查看How can I use HTTP GET in PowerShell?)。

但 Microsoft 提供了一些工具(模块)以使 Microsoft Graph API 在 PowerShell 中易于使用。也就是说Microsoft.Graph 模块。该模块处于预览阶段,但非常有用。

如果你想使用这个模块并下载一个附件,你可以使用下面的命令:

Get-MgUserMessageAttachment -MessageId <String> -UserId <String> [-Count] [-Expand <String[]>]
 [-Filter <String>] [-Orderby <String[]>] [-Search <String>] [-Select <String[]>] [-Skip <Int32>]
 [-Top <Int32>] [<CommonParameters>]

有关此命令的更多信息,您可以查看Microsoft Graph SDK PowerShell 文档。

Get-MgUserMessageAttachment 返回IMicrosoftGraphAttachment,它具有如下一些属性(JSON 表示):

{
  "contentType": "string",
  "id": "string (identifier)",
  "isInline": true,
  "lastModifiedDateTime": "String (timestamp)",
  "name": "string",
  "size": 1024
}

但这种类型是基于fileAttachment 的类型,具有如下一些属性(JSON 表示):

{
  "contentBytes": "string (binary)",
  "contentId": "string",
  "contentLocation": "string",
  "contentType": "string",
  "id": "string (identifier)",
  "isInline": true,
  "lastModifiedDateTime": "String (timestamp)",
  "name": "string",
  "size": 1024
}

对于文件下载,您可以使用ContentBytes 属性。此属性是Base64 编码的,因此您必须对其进行解码,然后使用Out-File 等一些命令将其保存到文件中。

正如您在 PowerShell 模块的the GitHub repository 中看到的,在此 PowerShell 的当前状态下,您无法直接使用它与此 SDK 一起下载附件,您必须直接在 PowerShell 中使用 Web API 来下载附件。

【讨论】:

  • 是的,我正在使用图形 SDK powershell 模块,不知道如何从图形模块下载附件这里没有用于 saveasfile 的内容属性或从 Get-MgUserMessageAttachment 导出
  • 我更新了我的答案,正如你在github.com/microsoftgraph/msgraph-sdk-powershell/issues/… 中看到的那样,你做不到
【解决方案2】:

这行得通(“日志”基本上只是调用 write-verbose):

# Install Microsoft Graph API PowerShell module if not already installed
if (Get-Module 'Microsoft.Graph' -ListAvailable) {
    Log 'PowerShell Module Microsoft.Graph is already installed.'
} else {
    Log 'PowerShell Module Microsoft.Graph not installed. Installing...'
    try {
        Install-Module 'Microsoft.Graph' -Force
        Log 'Installed successfully.'
    }
    catch {
        throw 'Error: unable to install PowerShell Module Microsoft.Graph Module.'
    }
}

$UserId     = ''
$FolderId   = ''
$Filename   = "$( $env:USERPROFILE )\Downloads\test.pdf"
$TId        = ''
$AppId      = ''
$CertThumb  = ''

# Connect to Microsoft Graph API
$null = Connect-MgGraph -TenantId $TId -AppId $AppId -CertificateThumbprint $CertThumb
# Using null suppresses the output

# Get Message from Folder in User
$Msgs = Get-MgUserMailFolderMessage -UserId $UserId -MailFolderId $FolderId
$MsgId = $Msgs[0].Id

# Get Attachment
$Attachm = Get-MgUserMailFolderMessageAttachment -UserId $UserId -MailFolderId $FolderId -MessageId $MsgId

# Get Attachment as Base64
$Base64B = ($Attachm).AdditionalProperties.contentBytes
# Works also: Get-MgUserMessageAttachment

# Save Base64 to file
$Bytes = [Convert]::FromBase64String($Base64B)
[IO.File]::WriteAllBytes($Filename, $Bytes)

Disconnect-MgGraph

【讨论】:

    猜你喜欢
    • 2017-07-01
    • 2019-02-27
    • 2022-01-06
    • 2019-07-16
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多