0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure Entra IDアカウント毎に特定の認証情報が設定済みの場合に自動的にTAPを削除する

0
Last updated at Posted at 2026-05-11

Azure Automation

スクリプト本体

# =============================
# Graph 接続
# =============================
Connect-MgGraph -Identity -NoWelcome

# =============================
# TAP を持つ全ユーザー取得
# =============================
Write-Output "組織内の全ユーザーをフェッチします。"
$users = Get-MgUser -All -Property Id,UserPrincipalName

foreach ($user in $users) {
    Write-Output "$($user.UserPrincipalName)の認証情報をフェッチ中…"

    # --- TAP 取得 ---
    $tapMethods = Get-MgUserAuthenticationTemporaryAccessPassMethod `
        -UserId $user.Id `
        -ErrorAction SilentlyContinue

    if (-not $tapMethods) {
        Write-Output "$($user.UserPrincipalName)はTAPがありませんでした。スキップします…"
        continue  # TAP なし
    }

    # =============================
    # パスワードレス判定
    # =============================

    $authMethods = Get-MgUserAuthenticationMethod -UserId $user.Id

    $passwordlessMethods = $authMethods | Where-Object {
        $_.AdditionalProperties['@odata.type'] -in @(
            '#microsoft.graph.microsoftAuthenticatorAuthenticationMethod',
            '#microsoft.graph.fido2AuthenticationMethod',
            '#microsoft.graph.windowsHelloForBusinessAuthenticationMethod'
        )
    }

    $hasPasswordless = ($passwordlessMethods.Count -gt 0)

    if ($hasPasswordless) {

        Write-Output "パスワードレス設定済みを検知しました。TAPを削除します..."

        foreach ($tap in $tapMethods) {
            Remove-MgUserAuthenticationTemporaryAccessPassMethod `
                -UserId $user.Id `
                -TemporaryAccessPassAuthenticationMethodId $tap.Id `
                -Confirm:$false
        }

    } else {
        Write-Output "パスワードレス設定が完了していないため、TAPを保持します。"
    }
}

ランタイム環境

言語とバージョン

PowerShell 7.2

アタッチされたパッケージ

  • Az 11.2.0
  • Azure CLI 2.56.0
  • Microsoft.Graph.Authentication 2.36.1
  • Microsoft.Graph.Identity.SignIns 2.36.1
  • Microsoft.Graph.Users 2.36.1

Logic Apps

コード

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "contentVersion": "1.0.0.0",
        "triggers": {
            "定期実行スケジュール": {
                "recurrence": {
                    "interval": 1,
                    "frequency": "Day",
                    "timeZone": "Tokyo Standard Time",
                    "schedule": {
                        "hours": [
                            0
                        ],
                        "minutes": [
                            0
                        ]
                    }
                },
                "evaluatedRecurrence": {
                    "interval": 1,
                    "frequency": "Day",
                    "timeZone": "Tokyo Standard Time",
                    "schedule": {
                        "hours": [
                            0
                        ],
                        "minutes": [
                            0
                        ]
                    }
                },
                "type": "Recurrence"
            }
        },
        "actions": {
            "ユーザーリストを取得": {
                "runAfter": {},
                "type": "Http",
                "inputs": {
                    "uri": "https://graph.microsoft.com/v1.0/users?$select=id,userPrincipalName",
                    "method": "GET",
                    "authentication": {
                        "type": "ManagedServiceIdentity",
                        "audience": "https://graph.microsoft.com/"
                    }
                },
                "runtimeConfiguration": {
                    "contentTransfer": {
                        "transferMode": "Chunked"
                    }
                }
            },
            "ユーザーリストをループ": {
                "foreach": "@body('ユーザーリストを取得')?['value']",
                "actions": {
                    "アクセス権のあるユーザーのみ": {
                        "actions": {
                            "Condition": {
                                "actions": {
                                    "PATループ": {
                                        "foreach": "@body('PATチェック')",
                                        "actions": {
                                            "ユーザーのTAPを削除": {
                                                "type": "Http",
                                                "inputs": {
                                                    "uri": "https://graph.microsoft.com/v1.0/users/@{items('ユーザーリストをループ')?['id']}/authentication/temporaryAccessPassMethods/@{items('PATループ')?['id']}",
                                                    "method": "DELETE",
                                                    "authentication": {
                                                        "type": "ManagedServiceIdentity",
                                                        "audience": "https://graph.microsoft.com/"
                                                    }
                                                },
                                                "runtimeConfiguration": {
                                                    "contentTransfer": {
                                                        "transferMode": "Chunked"
                                                    }
                                                }
                                            }
                                        },
                                        "type": "Foreach"
                                    }
                                },
                                "runAfter": {
                                    "認証情報チェック": [
                                        "Succeeded"
                                    ],
                                    "PATチェック": [
                                        "Succeeded"
                                    ]
                                },
                                "else": {
                                    "actions": {}
                                },
                                "expression": {
                                    "and": [
                                        {
                                            "greater": [
                                                "@length(body('認証情報チェック'))",
                                                0
                                            ]
                                        },
                                        {
                                            "greater": [
                                                "@length(body('PATチェック'))",
                                                0
                                            ]
                                        }
                                    ]
                                },
                                "type": "If"
                            },
                            "認証情報チェック": {
                                "type": "Query",
                                "inputs": {
                                    "from": "@body('ユーザーの認証情報を取得')?['value']",
                                    "where": "@or(equals(item()?['@odata.type'], '#microsoft.graph.microsoftAuthenticatorAuthenticationMethod'),equals(item()?['@odata.type'], '#microsoft.graph.fido2AuthenticationMethod'),equals(item()?['@odata.type'], '#microsoft.graph.windowsHelloForBusinessAuthenticationMethod'))\n"
                                }
                            },
                            "PATチェック": {
                                "type": "Query",
                                "inputs": {
                                    "from": "@body('ユーザーの認証情報を取得')?['value']",
                                    "where": "@equals(item()?['@odata.type'], '#microsoft.graph.temporaryAccessPassAuthenticationMethod')"
                                }
                            }
                        },
                        "runAfter": {
                            "ユーザーの認証情報を取得": [
                                "Succeeded",
                                "Failed"
                            ]
                        },
                        "else": {
                            "actions": {}
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@outputs('ユーザーの認証情報を取得')?['statusCode']",
                                        200
                                    ]
                                }
                            ]
                        },
                        "type": "If"
                    },
                    "ユーザーの認証情報を取得": {
                        "type": "Http",
                        "inputs": {
                            "uri": "https://graph.microsoft.com/v1.0/users/@{items('ユーザーリストをループ')?['id']}/authentication/methods",
                            "method": "GET",
                            "authentication": {
                                "type": "ManagedServiceIdentity",
                                "audience": "https://graph.microsoft.com/"
                            }
                        },
                        "runtimeConfiguration": {
                            "contentTransfer": {
                                "transferMode": "Chunked"
                            }
                        }
                    }
                },
                "runAfter": {
                    "ユーザーリストを取得": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            }
        },
        "outputs": {},
        "parameters": {
            "$connections": {
                "type": "Object",
                "defaultValue": {}
            }
        }
    },
    "parameters": {
        "$connections": {
            "type": "Object",
            "value": {}
        }
    }
}

説明

テナント内のすべてのユーザーをフェッチし、特定の認証情報(コード例の場合はMicrosoft Authenticator/FIDO2/WHfB)のいずれかを登録済みの場合、TAPを削除する。
毎日定期実行することでパスワードレス運用におけるTAP発行後の失効・削除忘れを防ぐことが出来る。
また、特定の認証情報が登録されていない場合はTAPは削除されない。

Automation/Logic AppsのいずれもマネージドID(システム)を割り当て、Entra ID ロールと管理者から認証管理者ロールを割り当てることで利用可能。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?