需要Powershell中使用GraphAPI更新/添加用户的示例。

huangapple go评论69阅读模式
英文:

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/&lt;TenantID&gt;/oauth2/v2.0/token 
 
$clinet_id = &lt;&gt;
$client_secret = &lt;&gt;
 
$queryURI = https://graph.microsoft.com/v1.0/users/&lt;id&gt;
 
#$scope = [System.Web.HTTPUtility]::UrlEncode(https://graph.microsoft.com/.default) 
$scope = https://graph.microsoft.com/.default
 
#Generating header values 
 
$tokenRequestQueryHeaderss =@{ 
    ContentType = &quot;application/x-www-form-urlencoded&quot; 
} 
 
 
#Create body to pass when requesting access token 
 
$body = @{
    client_id=$($clinet_id)
    scope=$($scope)
    client_secret=$($client_secret)
    grant_type=&quot;client_credentials&quot;
}
echo &quot;#### body: &quot; $body
 
#Send a post request to obtain bearer token 
$result = Invoke-RestMethod -Uri $uri -Body $body -Method Post -Headers $tokenRequestQueryHeaderss 
 
Write-Host &quot;#### tokenResult: &quot;$result
###############################GraphAPI Connect @End
 
###############################GraphAPI Data Collect
#Generate Header for query by adding Access Token obtained earlier 
$queryHeaders = @{ 
    Authorization = &quot;Bearer $($result.access_token)&quot; 
} 
 
echo &quot;#### queryURI: &quot; $queryURI
pause
 
#Send request  
 
$queryResult = Invoke-RestMethod  -Method Get -Headers $queryHeaders -Uri $queryURI -ContentType &quot;application/json&quot;
 
$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 = @(
    		&quot;+1 425 555 0109&quot;
    	)
    	OfficeLocation = &quot;18/2111&quot;
    }
    
    # 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&amp;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 = &quot;Adele Vance&quot;
    	MailNickname = &quot;AdeleV&quot;
    	UserPrincipalName = &quot;AdeleV@contoso.onmicrosoft.com&quot;
    	PasswordProfile = @{
    		ForceChangePasswordNextSignIn = $true
    		Password = &quot;xWwvJ]6NMw+bWH-d&quot;
    	}
    }
    
    New-MgUser -BodyParameter $params

For more information: https://learn.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&amp;tabs=powershell#example-1-create-a-user

Hope this helps.&#160;

</details>



huangapple
  • 本文由 发表于 2023年3月8日 17:19:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定