英文:
Need a sample of Update/Add User in Powershell with GraphAPI
问题
这是您要求的代码部分的翻译:
# 获取 GraphAPI 连接 @开始
$uri = "https://login.microsoftonline.com/<租户ID>/oauth2/v2.0/token"
$client_id = "<客户端ID>"
$client_secret = "<客户端密钥>"
$queryURI = "https://graph.microsoft.com/v1.0/users/<用户ID>"
$scope = "https://graph.microsoft.com/.default"
# 生成请求访问令牌时的标头值
$tokenRequestQueryHeaderss = @{
ContentType = "application/x-www-form-urlencoded"
}
# 创建在请求访问令牌时传递的主体
$body = @{
client_id = $($clinet_id)
scope = $($scope)
client_secret = $($client_secret)
grant_type = "client_credentials"
}
echo "#### 主体: " $body
# 发送请求以获取访问令牌
$result = Invoke-RestMethod -Uri $uri -Body $body -Method Post -Headers $tokenRequestQueryHeaderss
Write-Host "#### tokenResult: " $result
# 获取 GraphAPI 连接 @结束
# 收集 GraphAPI 数据
# 通过添加先前获取的访问令牌生成查询的标头
$queryHeaders = @{
Authorization = "Bearer $($result.access_token)"
}
echo "#### queryURI: " $queryURI
pause
# 发送请求
$queryResult = Invoke-RestMethod -Method Get -Headers $queryHeaders -Uri $queryURI -ContentType "application/json"
$queryResult
注意:这部分代码是用 PowerShell 编写的,用于连接到 Microsoft Graph API 并获取用户数据。在实际使用时,您需要替换 <租户ID>
、<客户端ID>
和 <客户端密钥>
以及 <用户ID>
等占位符为实际的值。这个代码片段执行两个主要操作:首先获取访问令牌,然后使用该访问令牌发送请求以获取用户数据。
英文:
Could I have a sampl of Update/Add User method in powershell with GraphAPI?
I have already tested below Get User script in my Tenant(B2C) ,
need to know the correct paramater usage for Update or ADD a user.
from Mircosoft documents, it seems doesn't have a Powershell with REST API sample of Update/Add user, only with SDK.
###############################GraphAPI Connect @Start
$uri = https://login.microsoftonline.com/<TenantID>/oauth2/v2.0/token
$clinet_id = <>
$client_secret = <>
$queryURI = https://graph.microsoft.com/v1.0/users/<id>
#$scope = [System.Web.HTTPUtility]::UrlEncode(https://graph.microsoft.com/.default)
$scope = https://graph.microsoft.com/.default
#Generating header values
$tokenRequestQueryHeaderss =@{
ContentType = "application/x-www-form-urlencoded"
}
#Create body to pass when requesting access token
$body = @{
client_id=$($clinet_id)
scope=$($scope)
client_secret=$($client_secret)
grant_type="client_credentials"
}
echo "#### body: " $body
#Send a post request to obtain bearer token
$result = Invoke-RestMethod -Uri $uri -Body $body -Method Post -Headers $tokenRequestQueryHeaderss
Write-Host "#### tokenResult: "$result
###############################GraphAPI Connect @End
###############################GraphAPI Data Collect
#Generate Header for query by adding Access Token obtained earlier
$queryHeaders = @{
Authorization = "Bearer $($result.access_token)"
}
echo "#### queryURI: " $queryURI
pause
#Send request
$queryResult = Invoke-RestMethod -Method Get -Headers $queryHeaders -Uri $queryURI -ContentType "application/json"
$queryResult
</details>
# 答案1
**得分**: 1
更新MS图中的用户使用PowerShell:
```powershell
Import-Module Microsoft.Graph.Users
$params = @{
BusinessPhones = @(
"+1 425 555 0109"
)
OfficeLocation = "18/2111"
}
# 可以使用UPN作为 -UserId。
Update-MgUser -UserId $userId -BodyParameter $params
```
更多信息请参考:[https://learn.microsoft.com/en-us/graph/api/user-update?view=graph-rest-1.0&tabs=powershell#example-1-update-properties-of-the-signed-in-user](https://learn.microsoft.com/en-us/graph/api/user-update?view=graph-rest-1.0&tabs=powershell#example-1-update-properties-of-the-signed-in-user)
要在MS图中添加或创建用户使用PowerShell:
```powershell
Import-Module Microsoft.Graph.Users
$params = @{
AccountEnabled = $true
DisplayName = "Adele Vance"
MailNickname = "AdeleV"
UserPrincipalName = "AdeleV@contoso.onmicrosoft.com"
PasswordProfile = @{
ForceChangePasswordNextSignIn = $true
Password = "xWwvJ]6NMw+bWH-d"
}
}
New-MgUser -BodyParameter $params
```
更多信息请参考:[https://learn.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=powershell#example-1-create-a-user](https://learn.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=powershell#example-1-create-a-user)
希望这有所帮助。
<details>
<summary>英文:</summary>
For updating the user in MS graph using the powershell:
Import-Module Microsoft.Graph.Users
$params = @{
BusinessPhones = @(
"+1 425 555 0109"
)
OfficeLocation = "18/2111"
}
# A UPN can also be used as -UserId.
Update-MgUser -UserId $userId -BodyParameter $params
For more information: https://learn.microsoft.com/en-us/graph/api/user-update?view=graph-rest-1.0&tabs=powershell#example-1-update-properties-of-the-signed-in-user
To Add or create a user in MS Graph using the powershell:
Import-Module Microsoft.Graph.Users
$params = @{
AccountEnabled = $true
DisplayName = "Adele Vance"
MailNickname = "AdeleV"
UserPrincipalName = "AdeleV@contoso.onmicrosoft.com"
PasswordProfile = @{
ForceChangePasswordNextSignIn = $true
Password = "xWwvJ]6NMw+bWH-d"
}
}
New-MgUser -BodyParameter $params
For more information: https://learn.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=powershell#example-1-create-a-user
Hope this helps. 
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论