🧅
Windows サーバーで Entra ID ログを取得する
概要
以前は Linux サーバーから Entra ID ログの取得について調べました。
言いたいことは分かります、Entra ID のハイブリッド ID 構成において Linux ホストなんてでてこねーよ、と。
というわけで、Windows サーバーからの取得方法も検証してみました。やりたいことは...
- Windows Server から Entra ID ログの取得
今回利用した環境
- Windows Server 2025
- Microsoft Graph PowerShell 2.28.0
前準備
前回と同じように Entra ID に サービス プリンシパル を作成しておきます。
今回は クライアント シークレットではなくて、資格情報に証明書を使いますのでその証明書を作成します。(テストなので自己署名証明書を使います) 手順は Microsoft のドキュメント に記載されていますので、証明書の登録まで行います。

コマンドレベルで確認
Windows Server に Microsoft Graph PowerShell をインストールします。
PowerShell Console
Install-Module Microsoft.Graph -Scope CurrentUser -Repository PSGallery -Force
Entra ID のテナント情報をセットして
PowerShell Console
$TenantId = "********-****-****-****-***********"
$ClientId = "********-****-****-****-***********"
$CertificateThumbprint = "****************************************"
Entra ID に接続します。
PowerShell Console
Connect-MgGraph -TenantId $TenantId -ClientId $ClientId -CertificateThumbprint $CertificateThumbprint
とりあえず サインインのログを取得してみます。大丈夫そう。
PowerShell Console
Get-MgAuditLogSignIn -Top 10

簡易スクリプト
管理スクリプトを作成してテストしてみます。
- 対象はサインインログ
- ログの出力先は :
C:\ProgramData\EntraIdLogCollector\ - 直近1週間分のログ
- json形式(2次加工の柔軟性を考慮)
test-entraid-log-collect.ps1
$ApplicationName = "EntraIdLogCollector"
$LogRootPath = Join-Path $env:ProgramData $ApplicationName
$TenantId = "********-****-****-****-***********"
$ClientId = "********-****-****-****-***********"
$CertificateThumbprint = "****************************************"
# 前1週間の日付を計算
$EndDate = (Get-Date).Date # 今日の00:00:00
$StartDate = $EndDate.AddDays(-7) # 7日前の00:00:00
$DateString = "$(($StartDate).ToString('yyyy-MM-dd'))_to_$(($EndDate.AddDays(-1)).ToString('yyyy-MM-dd'))"
# 前1週間の日付範囲を設定
$StartDateTime = $StartDate.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
$EndDateTime = $EndDate.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
# 出力フォルダ作成
if (!(Test-Path $LogRootPath)) {
New-Item -ItemType Directory -Path $LogRootPath -Force | Out-Null
}
# Microsoft Graphに接続
Connect-MgGraph -TenantId $TenantId -ClientId $ClientId -CertificateThumbprint $CertificateThumbprint
# フィルター条件
$Filter = "createdDateTime ge $StartDateTime and createdDateTime lt $EndDateTime"
# サインインログを取得
$SignInLogs = Get-MgAuditLogSignIn -Filter $Filter -All
# JSONファイル名
$FileName = "SignInLogs_$DateString.json"
$FilePath = Join-Path $LogRootPath $FileName
# JSONに変換して出力
$SignInLogs | ConvertTo-Json -Depth 10 | Out-File -FilePath $FilePath -Encoding UTF8
# Microsoft Graphから切断
Disconnect-MgGraph
実行して出力されたファイルを、コマンドで加工して参照してみます。
PowerShell Console
$logs = Get-Content -Path <FileName> -Raw | ConvertFrom-Json
$logs | Select-Object @{Name="DateTime";Expression={$_.CreatedDateTime}}, @{Name="UPN";Expression={$_.UserPrincipalName}}, @{Name="IPAddress";Expression={$_.IpAddress}}
大丈夫そう。

あとは下記のような点を考慮すれば自動化できると思います。
- エラーハンドリング
- ハウスキーピング
- タスクスケジューラー実行
あと、今回私は自前のテスト環境でログの物量が少なかったので1週間分の取得でしたが、実際に運用環境で1週間分を取得するには相当な量になるので、1日に調整するのがいいかもしれません。