gpt4 book ai didi

azure - 无法使用 MS Graph Powershell SDK 获取 Azure AD 用户的 CustomSecurityAttributes

转载 作者:行者123 更新时间:2023-12-03 02:02:20 24 4
gpt4 key购买 nike

就上下文而言,我正在构建一个应用程序,它将读取和更新 Azure AD 用户的自定义安全属性。我正在使用测试版。

这是我的代码:

# Connect to the client
Function Invoke-Connect-MSGraph($ClientId, $TenantId, $ClientSecret) {
$body = @{
'grant_type' = 'client_credentials'
'client_id' = $ClientId
'client_secret' = $ClientSecret
# Scope has to be .default when using client cred flow: https://stackoverflow.com/a/51789899/12739456
'scope' = 'https://graph.microsoft.com/.default'
}

$Params = @{
'Uri' = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
'Method' = 'Post'
'ContentType' = 'application/x-www-form-urlencoded'
'Body' = $body
}

try {
$TokenResponse = Invoke-RestMethod @Params
$AccessToken = $TokenResponse.access_token

Connect-MgGraph -AccessToken $AccessToken
Select-MgProfile -Name "beta"

Write-Host "Connected to MS Graph"
}
catch [Exception] {
Write-Error "Error Connecting. Error was: $_.Exception.Message"
exit
}
}

# Get User
$EmployeeEmail = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="305a515e55545f557048494a1e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>"
$User = Get-MgUser -Filter "proxyAddresses/any(x:x eq 'smtp:$($EmployeeEmail)')"

# Get Custom Security Attributes
$Attr = Get-MgUser -UserId $User.Id -Property "customSecurityAttributes"
$AzureCustomSecurityAttributes = Convertfrom-json ($Attr).ToJson()

我收到的错误:找不到“ToJson”的重载且参数计数:“0”。似乎$Attr总是返回null。但我可以获得其他用户属性。

应用程序具有正确的权限:CustomSecAttributeAssignment.ReadWrite.AllUser.Read.All我正在使用的管理员角色还具有属性分配管理员角色。

我在这里做错了什么?预先感谢您的输入!

我尝试使用 Azure AD PowerShell 实现相同的逻辑。它可以工作,但由于 Azure AD PowerShell 已被弃用,我需要使用 MS Graph PowerShell 找到解决方案。

最佳答案

我同意@user2250152。

这是我的 api 调用方式

# replace this with yours
$tenantId = "xxx"
$appId = "xxx"
$appSecret = "xxx"

# Define API endpoint and parameters
$authEndpoint = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$body = @{
client_id = $appId
client_secret = $appSecret
scope = "https://graph.microsoft.com/.default"
grant_type = "client_credentials"
}

$tokenResponse = Invoke-RestMethod -Method Post -Uri $authEndpoint -Body $body
$accessToken = $tokenResponse.access_token


# Define the headers for the API request
$headers = @{
Authorization = "Bearer $accessToken"
ContentType = "application/json"
}

# user u need to read customsecurityattributes
$userupn = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f6a6c7a6d5f66706a6d7b70727e7671317c7072" rel="noreferrer noopener nofollow">[email protected]</a>"
# api call uri
$apiEndpoint = "https://graph.microsoft.com/beta/users/$userupn"

# API request
$apiResponse = Invoke-RestMethod -Method Get -Uri $apiEndpoint -Headers $headers

# Output
$apiResponse.customSecurityAttributes

请注意,要检索特定用户的自定义安全属性,必须已为该用户分配自定义属性

可以引用这个article ,它清楚地解释了如何创建自定义安全属性并分配给用户。它使用图形 API 调用和 azure AD GUI 来演示如何执行此操作。

使用 azure ad GUI 创建属性并分配给用户时,您需要其中一项权限。属性分配读取器、属性定义读取器、属性分配管理员、属性定义管理员默认情况下,此角色甚至不会分配给全局管理员。

引用这个Microsoftdoc

关于azure - 无法使用 MS Graph Powershell SDK 获取 Azure AD 用户的 CustomSecurityAttributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75736634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com