azure - 如何删除 MS Graph PowerShell 模块中的目录角色成员?

标签 azure powershell microsoft-graph-api microsoft-graph-sdks

我一直在寻找一种使用 MS Graph PowerShell module 从 Azure 中删除目录角色成员的方法。但是,我似乎找不到任何 cmdlet 来执行此操作。

我目前正在使用 New-MgDirectoryRoleMemberByRef cmdlet 以便将用户添加到我们所照顾的租户的目录角色(例如全局管理员)中,但是我似乎找不到随后删除它们的方法。

我可以从this article here看到MS Graph API 公开了删除功能,但没有详细说明能够执行此操作的 PowerShell cmdlet。

任何人都可以为我指明如何使用 MS Graph PowerShell SDK 从角色中删除用户的正确方向吗?

最佳答案

目前似乎没有适用于此目的的 PowerShell cmdlet。 migration documentation列出与 Remove-AzureADDirectoryRoleMember 等效的内容是 Remove-MgDirectoryRoleScopedMember,但这似乎不正确,因为此 cmdlet 适用于作用域为管理单位的角色。

与此同时,您仍然可以使用 Azure AD PowerShell cmdlet 或 Graph API。

通过 PowerShell 使用 Graph API

您需要一个访问 token ,该 token 通常是使用客户端应用程序(应用程序注册)获取的。请求 header 中需要此访问 token 。

可用于测试目的的更手动方法是打开 Graph Explorer ,登录,然后单击访问 token 按钮。

enter image description here

客户端应用程序(或 Graph Explorer 用户)还需要权限 RoleManagement.ReadWrite.Directory(如 documentation 中所述)

# Example request

$roleId = ""
$roleTemplateId = ""
$userId = ""    
$accessToken = ""

# Use this endpoint when using the role Id
$uri = "https://graph.microsoft.com/v1.0/directoryRoles/$roleId/members/$userId/`$ref"

# Use this endpoint when using the role template ID
# $uri = "https://graph.microsoft.com/v1.0/directoryRoles/roleTemplateId=$roleTemplateId/members/$userId/`$ref"

# Splatted parameters for the HTTP request    
$params = @{
    Headers = @{ Authorization = "Bearer $accessToken" }
    Method  = "Delete"
    Uri     = $uri
}

Invoke-RestMethod @params

使用图形浏览器

使用 DELETE 方法向任意端点发出请求

DELETE /directoryRoles/{role-id}/members/{id}/$ref
DELETE /directoryRoles/roleTemplateId={roleTemplateId}/members/{id}/$ref

enter image description here

关于azure - 如何删除 MS Graph PowerShell 模块中的目录角色成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72637942/

相关文章:

powershell - 如何让 7za.exe 通过 Powershell Remoting 运行?

azure - 在 .NET Core 控制台应用程序中以编程方式创建 New-AzureRmRoleAssignment

azure - 使用 Microsoft Graph API 查询 OneDrive 中的文件 : Unable to retrieve user's mysite URL

c# - 如何确保MessageHandler委托(delegate)在MessageReciever关闭期间完成?

azure - 如何使用 MS graph API 获取 azure ad b2c 用户的身份验证方法或 MFA 电话号码

azure - 如何通过Azure数据工厂调用exe文件?

azure - AD B2C - 如何在密码重置流程中设置自定义电子邮件验证

相当于 "tail"命令的 Powershell 几乎可以远程工作

powershell - 术语 '<ScriptPath>\Move' 未被识别为 cmdlet 的名称

asp.net - Microsoft Graph API .NET - 能够拉取所有用户(包括我自己),但不仅仅是我