在一个受到严格监管的上下文中,我们正在对所有Azure AD进行审核,我们正在努力应对以下用例。任何帮助表示赞赏。
上下文:
用例解决:
答案 0 :(得分:0)
根据您的描述,您可以在主要租户中注册多租户应用:
创建应用后,为其创建一个秘密并记下它的应用ID 和秘密,稍后我们将使用它来连接到您的租户:
为此应用程序授予Microsoft Graph API的“ User.ReadWrite.All”权限,因为我们将调用Microsoft Graph API删除用户:
不要忘记单击“为您的租户授予管理员同意”按钮以完成分配过程。
通过链接让其他租户的管理员同意使用此应用程序:
https://login.microsoftonline.com/<your tenant ID>/adminConsent?client_id=<your multi-tenant app ID>
并且此应用程序将作为服务原则添加到其他租户中:
这样我们就可以使用此应用对所有租户进行一些操作。
只需运行下面的Powershell即可从您的所有租户中删除一个用户,我已经在我这边进行了测试,它可以为我工作。顺便说一句,请在运行它之前删除所有目录角色,否则,如果为用户分配了一些管理员角色,则会出现403错误。
#config your multi tenant app id and secret here.
$appid = "<YOUR MULTI TENANT APP ID>"
$secret = "<YOUR MULTI TENANT APP SECRET>"
$userEmailaddress = "<EMAIL ADDRESS OF THE USER YOU WANT TO DELETE>"
#config all your tenents here
$tenants = "<TENANT 1 ID/NAME>","TENANT 2 ID/NAME",...
#check and delete user by email address in all tenants
foreach($tenant in $tenants){
#get token to call microsoft graph api to delete users
$body=@{
"grant_type"="client_credentials";
"resource"="https://graph.microsoft.com";
"client_id"=$appid;
"client_secret" = $secret
}
$result=Invoke-RestMethod -Uri "https://login.windows.net/$tenant/oauth2/token" -Method POST -Body $body
#get user object from tenant
$userguestPrefix = $userEmailaddress.Replace('@','_')
$searchURL = "https://graph.microsoft.com/v1.0/users?`$filter=startswith(userPrincipalName,'$userEmailaddress') or startswith(userPrincipalName,'$userguestPrefix')"
$searchUserResult = Invoke-RestMethod -Uri $searchURL -Method GET -Headers @{'Authorization'='Bearer '+$result.access_token}
#remove the user from tenant
if($searchUserResult.value.Length -eq 1){
echo "remove:"$searchUserResult.value[0].displayName" from tenant : $tenant"
$removeURL = "https://graph.microsoft.com/v1.0/users/" + $searchUserResult.value[0].id
Invoke-RestMethod -Uri $removeURL -Method DELETE -Headers @{'Authorization'='Bearer '+$result.access_token}
}
}
如果您还有其他疑问,请随时告诉我。