6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Microsoft Graph PowerShell SDK] Teamsのチャネルの投稿をcsvで一覧取得する

Last updated at Posted at 2022-08-01

概要

目標は前回の記事と同様。
前回は Invoke-RestMethod を用いてPowerShellからGraph APIを叩いた。
しかし、Microsoft GraphのPowerShell SDKを使うことで、よりPowerShellライクに実現できることが分かった。

手順

事前準備

取得対象のチャネルのIDと、そのチャネルが含まれるチームのIDが必要。
取得方法は様々だが、以下の記事にまとまっているので、いずれかの方法で取得する。

また、PowerShellで以下のコマンドを実行し、Microsoft Graph PowerShell SDKをインストールする。

PowerShell
Install-Module Microsoft.Graph

最終的なスクリプト

まず最終的なスクリプトを貼り、その次に前回との差分について解説する。

PowerShell
Import-Module Microsoft.Graph.Teams
Connect-MgGraph -Scopes @("ChannelMessage.Read.All")

$teamId = "取得したチームID"
$channelId = "取得したチャネルID"

$csv = @()
$result = Get-MgTeamChannelMessage -TeamId $teamId -ChannelId $channelId -All -PageSize 50

foreach ($message in $result)
{   
    if ($message.messageType -ne "message") {
        continue
    }
    $chatMessageID = $message.id 
    if ($message.body.contentType -eq "html") {
        $html = New-Object -ComObject "HTMLFile"
        $html.IHTMLDocument2_write($message.body.content)
        $message.body.content = $html.IHTMLDocument2_body.innerText
    }
    $content = $null
    if ($message.subject -ne $null) {
        $content = $message.subject + ": " + $message.body.content
    }
    else {
        $content = $message.body.content
    }
    $data = New-Object PSObject | Select-Object DateTime, From, Type, Content, Attachments
    $data.DateTime = $message.createdDateTime
    $data.From = $message.from.user.displayName
    $data.Type = "Post"
    $data.Content = $content
    $temp = $message.attachments | ForEach-Object { $_.ContentUrl}
    $data.Attachments = $temp -join "; "
    $csv += $data

    $replies = Get-MgTeamChannelMessageReply -TeamId $teamId -ChannelId $channelId -ChatMessageId $chatMessageId -All -PageSize 50
    foreach ($reply in $replies) {
        if ($reply.messageType -ne "message") {
            continue
        }
        $replyData = New-Object PSObject | Select-Object DateTime, From, Type, Content, Attachments
        $replyData.DateTime = $reply.createdDateTime
        $replyData.From = $reply.from.user.displayName
        $replyData.Type = "Reply"
        $replyData.Content = $reply.body.content
        $temp = $reply.attachments | ForEach-Object { $_.ContentUrl}
        $replyData.Attachments = $temp -join "; "
        $csv += $replyData
    }
}

$csv | Export-Csv -Path "ChannelMessages.csv" -NoTypeInformation -Encoding UTF8

すると、ChannelMessages.csv というファイルにチャネルの投稿内容が出力される。

前回との差分

PowerShell
Import-Module Microsoft.Graph.Teams
Connect-MgGraph -Scopes @("ChannelMessage.Read.All")

モジュールをインポートし、必要な権限を要求する形でサインインしている。
チャネルの投稿の取得には ChannelMessage.Read.All の権限が必要とのことだったので、今回はそれを要求している。
(参考: https://docs.microsoft.com/ja-jp/graph/api/channel-list-messages)

PowerShell
$result = Get-MgTeamChannelMessage -TeamId $teamId -ChannelId $channelId

チャネル内の投稿について一覧で取得するところである。
前回の記事では Invoke-RestMethod を使用していたが、今回は Get-MgTeamChannelMessage コマンドで取得した。
このコマンドの使い方については以下。

PowerShell
$replies = Get-MgTeamChannelMessageReply -TeamId $teamId -ChannelId $channelId -ChatMessageId $chatMessageId

ある投稿への返信について、一覧で取得しているところ。
$chatMessageId については自動で取得しているため、特に気にする必要はない。
ここについても、前回の記事では Invoke-RestMethod を使用していたが、今回は Get-MgTeamChannelMessageReply コマンドで取得した。
このコマンドの使い方は以下を参照するとよい。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?