使用状況レポートを取得するOffice365グラフAPI

1、グローバル管理者ログOffice365のAzure ADは、公式サイトのリンクhttps://docs.microsoft.com/zh-cn/graph/auth-register-app-v2を特に参照して、グラフAPIアプリケーションを登録して使用して
アクセス許可の委任APIアプリケーションを追加し、2 Reports.Read.All
3、証明書とクライアントは、クライアントコード作成
トークン機能を生成し、4

function Graph_Auth
{
$clientID = "客户端ID(36位)" 
$tenantName = "tenant.onmicrosoft.com"  
$ClientSecret = "客户端密码"
$Username = "拥有应用权限的账号"
$Password = "以上账号密码"
$ReqTokenBody = @{
    Grant_Type    = "Password"
    client_Id     = $clientID
    Client_Secret = $clientSecret
    Username      = $Username
    Password      = $Password
    Scope         = "https://graph.microsoft.com/.default"
} 
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
$headerParams = @{
"Content-Type" = "application/json"
"Authorization"="$($TokenResponse.token_type) $($TokenResponse.access_token)"}
return $headerParams
}

5.呼び出し-restmethod実行時間は、サーバーエラーに接続できない場合、問題は、以下の機能が証明書を無視し実行し、HTTPS証明書もあり
使用状況レポートを取得するOffice365グラフAPI

function Ignore-SelfSignedCerts {
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}}
"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}

スクリプトの実行に現在のディレクトリ内の使用状況レポートや出力ファイルをクロールする6、

Ignore-SelfSignedCerts 
$current_path = Split-Path -Parent $MyInvocation.MyCommand.Definition #获取当前目录位置
$today = get-date -format yyyy-MM-dd
$headerParams = Graph_Auth       #使用步骤4的函数生成Token
$detailreports = "getEmailActivityUserDetail","getMailboxUsageDetail","getOffice365ActiveUserDetail"
foreach($detailreport in $detailreports){
Write-Host $detailreport -ForegroundColor Green
$filename = $current_path + "\" + $detailreport+ "_$today.csv"
$url = "https://graph.microsoft.com/v1.0/reports/$detailreport(period='D90')" 
$myReport = ""
$Error.Clear()
$myReport =Invoke-RestMethod -UseBasicParsing -Headers $headerParams -Uri $url -Method Get -Verbose
if($myReport){
$myReport | Out-File $filename -Encoding UTF8
}
}

おすすめ

転載: blog.51cto.com/6293080/2479403