このチュートリアルでは、プログラミング ツールを使用してテナント間分離レポートを作成する方法について説明します。 テナント間接続レポートには、テナント内の接続が到達した外部テナント、またはテナントに到達する外部テナントが表示されます。 これらのレポートを使用して、テナント間接続を制限する前に、許可リストに追加するテナントを特定します。 テナントの分離の詳細については、「 テナント間の受信と送信の制限」を参照してください。
このチュートリアルで学習する内容は次のとおりです:
- サインイン
- レポートの作成
- テナントのすべてのレポートを一覧表示する
- 単一のレポートを取り込む
- レポートに含まれる内容を理解する
- 報告された接続の環境を見つける
Prerequisites
- Power Platform 管理者など、テナントの管理者ロールを持つアカウント。 テナント間接続レポート エンドポイントは、Power Platform 管理センターのロールベースのアクセス制御 (RBAC) をサポートしていません。 詳細については、既知の制限 を参照してください。
- Power AutomateまたはAzure Logic Appsを使用するには、Power Platform for Admins V2 コネクタへの接続。
- Power Platform CLI または PowerShell を使用するには、Microsoft Power Platform CLI の最新バージョン。 PowerShell スクリプトは、それを使用してアクセス トークンを取得します。
- PowerShell、PowerShell 7、または PowerShell 5.1 Windowsを使用する場合。
- C# または Python SDK を使用するには、Power Platform API 用に構成されたアプリ登録と、言語用の SDK パッケージ:
サインイン
レポートを作成またはフェッチする前にサインインします。 Power AutomateまたはAzure Logic Appsを使用する場合は、このセクションをスキップします。 コネクタは、作成した接続を使用します。
ヒント
このチュートリアルで使用する pac governance コマンドはプレビュー段階です。
認証プロファイルを作成し、管理者アカウントでサインインします。 このチュートリアルの pac governance コマンドでは、選択した認証プロファイルを使用します。
pac auth create
既に認証プロファイルがある場合は、 pac auth list を実行して表示し、 pac auth select --index <index> 実行してテナントのプロファイルを選択します。 詳細については、「pac auth」を参照してください。
テナントの Power Platform CLI 認証プロファイルがない場合は、テナントを作成してサインインします。
pac auth create
次に、PowerShell セッションで次のスクリプトを実行します。 選択した認証プロファイルから Power Platform API のアクセス トークンを取得します。 このチュートリアルの残りの部分のスクリプトでは、このスクリプトが設定する $headers、 $apiBaseUrl、および $apiVersion 変数を使用します。
$apiBaseUrl = "https://api.powerplatform.com"
$apiVersion = "2024-10-01"
# Get an access token for the Power Platform API from the selected authentication profile.
# The pattern extracts the token from the command output.
$tokenOutput = pac auth token | Out-String
$accessToken = [regex]::Match($tokenOutput, 'eyJ[\w-]+\.[\w-]+\.[\w-]+').Value
$headers = @{ Authorization = "Bearer $accessToken" }
Tip
要求が失敗した場合は、 $Error[0].ErrorDetails.Message 実行して、API から返されたエラーを確認します。 アクセス トークンは、一定期間後に期限切れになります。 要求から 401 Unauthorized エラーが返された場合は、スクリプトをもう一度実行して新しいトークンを取得します。
アプリ登録のクライアント ID を使用してクライアントを作成します。 サインインするためのブラウザー ウィンドウが開きます。 このチュートリアルの残りの部分の C# の例では、 client 変数と apiVersion 変数を使用します。
using Microsoft.PowerPlatform.Management;
using Microsoft.PowerPlatform.Management.Models;
// Create an interactive client by using your app registration's client ID.
var factory = new ServiceClientFactory();
var client = factory.Create("<client-id>");
var apiVersion = "2024-10-01";
アプリ登録のクライアント ID を使用してクライアントを作成します。 サインインするためのブラウザー ウィンドウが開きます。 このチュートリアルの残りの部分のPython例では、この例のインポート、API_VERSION定数、およびclient変数を使用します。
Pythonの例では、awaitを使用します。 Jupyter Notebook や python -m asyncio コンソールなど、最上位のawaitをサポートする環境で実行するか、async関数内に配置します。
from azure.identity import InteractiveBrowserCredential
from kiota_abstractions.base_request_configuration import RequestConfiguration
from kiota_authentication_azure.azure_identity_authentication_provider import (
AzureIdentityAuthenticationProvider,
)
from kiota_http.httpx_request_adapter import HttpxRequestAdapter
from mspp_management.service_client_base import ServiceClientBase
from mspp_management.governance.cross_tenant_connection_reports.cross_tenant_connection_reports_request_builder import (
CrossTenantConnectionReportsRequestBuilder,
)
from mspp_management.governance.cross_tenant_connection_reports.item.with_report_item_request_builder import (
WithReportItemRequestBuilder,
)
from mspp_management.models.cross_tenant_connection_report_status import (
CrossTenantConnectionReport_status,
)
API_VERSION = "2024-10-01"
# Sign in interactively by using your app registration's client ID.
credential = InteractiveBrowserCredential(client_id="<client-id>")
auth_provider = AzureIdentityAuthenticationProvider(
credentials=credential,
scopes=["https://api.powerplatform.com/.default"],
)
adapter = HttpxRequestAdapter(authentication_provider=auth_provider)
client = ServiceClientBase(adapter)
サービス プリンシパルとしてサインインする場合、またはアプリの登録の詳細については、「 認証」を参照してください。
レポートの作成
Power Automate、Azure Logic Apps、Power Platform CLI、PowerShell、または C# または Python SDK を使用して、テナントのレポートを作成します。
ヒント
テナントごとにカレンダー日ごとに 1 つのレポートを作成できます。 テナントに現在の日のレポートが既にある場合、要求は新しいレポートを作成するのではなく、そのレポートを返します。
キャンバスに新しいアクションを追加し、Power Platform for Admins V2 コネクタでテナント間接続レポートの生成またはフェッチを検索します。 アクションにはパラメーターは必要ありません。
次の PowerShell スクリプトを使用してレポートを作成する。
# Create a cross-tenant connection report, or get today's report if it already exists.
$report = Invoke-RestMethod -Method Post -Uri "$apiBaseUrl/governance/crossTenantConnectionReports?api-version=$apiVersion" -Headers $headers -Body ""
$reportId = $report.reportId
Write-Host "Cross-tenant connection report $reportId has status $($report.status)."
Power Platform API リファレンス: テナント間接続レポートの作成
レポートを作成するには、次の C# コードを使用します。
// Create a cross-tenant connection report, or get today's report if it already exists.
var report = await client.Governance.CrossTenantConnectionReports.PostAsync(config =>
{
config.QueryParameters.ApiVersion = apiVersion;
});
var reportId = report.ReportId.ToString();
Console.WriteLine($"Cross-tenant connection report {reportId} has status {report.Status}.");
レポートを作成するには、次のPythonコードを使用します。
# Create a cross-tenant connection report, or get today's report if it already exists.
post_config = RequestConfiguration(
query_parameters=CrossTenantConnectionReportsRequestBuilder.CrossTenantConnectionReportsRequestBuilderPostQueryParameters(
api_version=API_VERSION
)
)
report = await client.governance.cross_tenant_connection_reports.post(request_configuration=post_config)
report_id = str(report.report_id)
print(f"Cross-tenant connection report {report_id} has status {report.status.value}.")
reportId の値を保存します。 これを使用して、このチュートリアルの後半でレポートをフェッチします。
テナントのすべてのレポートを一覧表示する
テナントのレポートを一覧表示してレポート ID を検索し、各レポートの状態を確認します。
キャンバスに新しいアクションを追加し、テナントの テナント間接続レポートの一覧表示を検索します。 アクションにはパラメーターは必要ありません。
次のコマンドを使用して、テナントで使用可能なすべてのレポートを一覧表示します。 テーブル出力の列を選択するには、--columns reportId,status,startDate,endDateなどの応答プロパティのコンマ区切りのリストを含む--columnsを追加します。
pac governance list-cross-tenant-connection-reports
Power Platform CLI リファレンス: pac governance list-cross-tenant-connection-reports
次の PowerShell スクリプトを使用して、テナントで使用可能なすべてのレポートを一覧表示します。
# Get all cross-tenant connection reports for the tenant, following each page of results.
$reports = @()
$uri = "$apiBaseUrl/governance/crossTenantConnectionReports?api-version=$apiVersion"
while ($uri)
{
$page = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers
$reports += $page.value
$uri = $page.'@odata.nextLink'
}
$reports | Format-Table reportId, requestDate, startDate, endDate, status
Power Platform API リファレンス: テナント間接続レポートの一覧表示
次の C# コードを使用して、テナントで使用可能なすべてのレポートを一覧表示します。
// Get all cross-tenant connection reports for the tenant, following each page of results.
var reports = new List<CrossTenantConnectionReport>();
var page = await client.Governance.CrossTenantConnectionReports.GetAsync(config =>
{
config.QueryParameters.ApiVersion = apiVersion;
});
while (page != null)
{
reports.AddRange(page.Value ?? new List<CrossTenantConnectionReport>());
page = string.IsNullOrEmpty(page.OdataNextLink)
? null
: await client.Governance.CrossTenantConnectionReports.WithUrl(page.OdataNextLink).GetAsync();
}
foreach (var item in reports)
{
Console.WriteLine($"{item.ReportId} {item.RequestDate} {item.StartDate} {item.EndDate} {item.Status}");
}
テナントで使用可能なすべてのレポートを一覧表示するには、次のPythonコードを使用します。
# Get all cross-tenant connection reports for the tenant, following each page of results.
list_config = RequestConfiguration(
query_parameters=CrossTenantConnectionReportsRequestBuilder.CrossTenantConnectionReportsRequestBuilderGetQueryParameters(
api_version=API_VERSION
)
)
reports = []
page = await client.governance.cross_tenant_connection_reports.get(request_configuration=list_config)
while page:
reports.extend(page.value or [])
page = (
await client.governance.cross_tenant_connection_reports.with_url(page.odata_next_link).get()
if page.odata_next_link
else None
)
for item in reports:
print(item.report_id, item.request_date, item.start_date, item.end_date, item.status.value)
単一のレポートを取り込む
レポートは非同期的に生成されます。 新しいレポートは Received または InProgressの状態で始まり、その状態が Completedに変わると接続できます。 状態がまだ Received または InProgress場合は、待ってから、レポートをもう一度フェッチします。 状態が Failed場合は、後でもう一度レポートを作成します。
キャンバスに新しいアクションを追加し、「テナントの レポート ID でテナント間接続レポートを取得する」を検索します。 前の手順のレポート ID を指定します。
接続の出力を使用する前に、状態の出力が [完了] になっていることを確認します。
テナントの 1 つのレポートをフェッチするには、次のコマンドを使用します。
<report-id>を作成ステップまたはリスト ステップのreportId値に置き換えます。
connections配列を含む完全な応答を確認するには、--jsonを追加します。
pac governance get-cross-tenant-connection-report --report-id "<report-id>"
Power Platform CLI リファレンス: pac governance get-cross-tenant-connection-report
テナントの 1 つのレポートをフェッチするには、次の PowerShell スクリプトを使用します。 レポートが完了すると、スクリプトはレポート内のすべての接続ページを収集します。
# Replace with the reportId value from the create or list step.
$reportId = "<report-id>"
# Get one cross-tenant connection report for the tenant.
$report = Invoke-RestMethod -Method Get -Uri "$apiBaseUrl/governance/crossTenantConnectionReports/$($reportId)?api-version=$apiVersion" -Headers $headers
Write-Host "Report $($report.reportId) covers $($report.startDate) to $($report.endDate) and has status $($report.status)."
if ($report.status -eq "Completed")
{
# Collect every page of connections in the report.
$connections = @($report.connections)
$nextLink = $report.'@odata.nextLink'
while ($nextLink)
{
$page = Invoke-RestMethod -Method Get -Uri $nextLink -Headers $headers
$connections += $page.connections
$nextLink = $page.'@odata.nextLink'
}
$connections | Format-Table tenantId, connectionType, connectionId
}
Power Platform API リファレンス: テナント間接続レポートの取得
テナントの 1 つのレポートをフェッチするには、次の C# コードを使用します。 レポートが完了すると、コードはレポート内のすべての接続ページを収集します。
// Replace with the reportId value from the create or list step.
var reportId = "<report-id>";
// Get one cross-tenant connection report for the tenant.
var report = await client.Governance.CrossTenantConnectionReports[reportId].GetAsync(config =>
{
config.QueryParameters.ApiVersion = apiVersion;
});
Console.WriteLine($"Report {report.ReportId} covers {report.StartDate} to {report.EndDate} and has status {report.Status}.");
if (report.Status == CrossTenantConnectionReport_status.Completed)
{
// Collect every page of connections in the report.
var connections = new List<CrossTenantConnection>(report.Connections ?? new List<CrossTenantConnection>());
var nextLink = report.OdataNextLink;
while (!string.IsNullOrEmpty(nextLink))
{
var page = await client.Governance.CrossTenantConnectionReports[reportId].WithUrl(nextLink).GetAsync();
connections.AddRange(page.Connections ?? new List<CrossTenantConnection>());
nextLink = page.OdataNextLink;
}
foreach (var connection in connections)
{
// The SDK returns connectionId in the AdditionalData dictionary.
connection.AdditionalData.TryGetValue("connectionId", out var connectionId);
Console.WriteLine($"{connection.TenantId} {connection.ConnectionType} {connectionId}");
}
}
テナントの 1 つのレポートをフェッチするには、次のPython コードを使用します。 レポートが完了すると、コードはレポート内のすべての接続ページを収集します。
# Replace with the reportId value from the create or list step.
report_id = "<report-id>"
# Get one cross-tenant connection report for the tenant.
get_config = RequestConfiguration(
query_parameters=WithReportItemRequestBuilder.WithReportItemRequestBuilderGetQueryParameters(
api_version=API_VERSION
)
)
report_builder = client.governance.cross_tenant_connection_reports.by_report_id(report_id)
report = await report_builder.get(request_configuration=get_config)
print(f"Report {report.report_id} covers {report.start_date} to {report.end_date} and has status {report.status.value}.")
if report.status == CrossTenantConnectionReport_status.Completed:
# Collect every page of connections in the report.
connections = list(report.connections or [])
next_link = report.odata_next_link
while next_link:
page = await report_builder.with_url(next_link).get()
connections.extend(page.connections or [])
next_link = page.odata_next_link
for connection in connections:
# The SDK returns connectionId in the additional_data dictionary.
print(connection.tenant_id, connection.connection_type.value, connection.additional_data.get("connectionId"))
レポートに含まれる内容を理解する
各レポートには、次のプロパティが含まれています。
-
reportId: レポートの識別子。
-
status: レポートの生成状態: Received、 InProgress、 Completed、または Failed。
-
requestDate: レポートが要求された日付。
-
startDate と endDate: レポートに表示されるレポート ウィンドウ。
-
connections: レポートウィンドウで観察されたテナント間接続。
-
@odata.nextLink: 存在する場合、レポート内の接続の次のページの URL。
connections配列内のすべてのエントリは、テナント間接続を記述し、次のものが含まれます。
-
tenantId: 接続が到達する外部テナント。
-
connectionType: 接続の方向 (テナントから外部テナントへの) outbound または inbound。
-
connectionId: 接続の識別子。 この値は、接続が存在する環境の接続の name (接続のリソース ID の最後のセグメント) と一致します。
ヒント
レポートでは、現在存在する接続だけでなく、レポートウィンドウで確認されたテナント間の接続 試行 がログに記録されます。 レポートには、テナントまたはターゲット テナントでのテナント分離によってブロック された試行が含まれます。 その結果、レポート内の connectionId が、現在の環境に存在する接続に対応していない可能性があります。 次のセクションの手順を使用して、ブロックされた接続試行 (および後で削除された接続) を見つけることができません。
報告された接続の環境を見つける
レポートは connectionIdによって各接続を識別しますが、接続が存在する環境は含まれません。 環境を見つけるには、テナント内の環境を列挙し、各環境の接続を一覧表示し、接続 nameで一致させます。
次の PowerShell スクリプトを使用して、報告された connectionIdを環境で検索します。 このスクリプトでは、PowerShell サインイン スクリプトの変数を使用します。
Power Platform 管理センターなど、必要に応じて環境 ID を列挙し、レポートからconnectionId値を指定します。 このスクリプトでは、値を比較するときにハイフンと文字の大文字と小文字が無視されるため、識別子の書式設定の違いによって一致が妨げることはありません。
# The connectionId values from the report that you fetched earlier.
# To search for every connection in that report, use: $reportConnectionIds = $connections.connectionId | Select-Object -Unique
$reportConnectionIds = @('<connection-id-1>', '<connection-id-2>')
# The environments in your tenant to search.
$environmentIds = @('<environment-id-1>', '<environment-id-2>')
foreach ($environmentId in $environmentIds)
{
try
{
# List the connections in the environment.
$uri = "$apiBaseUrl/connectivity/environments/$environmentId/connections?api-version=$apiVersion"
$environmentConnections = (Invoke-RestMethod -Method Get -Uri $uri -Headers $headers).value
foreach ($connectionId in $reportConnectionIds)
{
# The report's connectionId matches the end of the connection's 'name'.
$normalizedId = $connectionId -replace '-', ''
$matchingConnections = $environmentConnections | Where-Object { ($_.name -replace '-', '') -like "*$normalizedId" }
foreach ($connection in $matchingConnections)
{
Write-Host "connectionId $connectionId is in environment $environmentId (connector: $($connection.properties.apiId), display name: $($connection.properties.displayName))"
}
}
}
catch
{
Write-Host "Couldn't list connections for environment ${environmentId}: $($_.Exception.Message)"
}
}
報告された connectionId が環境で見つからない場合は、ブロックされた試行 (前のメモを参照) またはレポートの生成後に削除された接続である可能性が最も高くなります。
Power Platform API リファレンス: 環境内の接続を一覧表示する
関連コンテンツ