当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 查询和更改团队用户的状态

作者:精品下载站 日期:2024-12-14 06:35:21 浏览:14 分类:玩电脑

如何使用 PowerShell 查询和更改团队用户的状态


在对某些集成脚本执行特定操作之前,可能需要检查用户在 Microsoft Teams 中的状态(在线/离开/忙碌)。让我们看看如何使用 Microsoft Graph API 和 PowerShell 获取和更改 Teams 中用户的状态。

通过 PowerShell 获取 Teams 用户状态

如果您的计算机上安装了 Microsoft.Graph 模块,请使用您的帐户连接到您的租户:

Connect-MgGraph -Scopes Presence.Read.All,User.Read.All

如果未安装Microsoft.Graph,可以按如下方式安装:

Install-Module Microsoft.Graph -Scope AllUsers

指定您要获取其状态的用户的 UPN:

$TeamsUser = Get-MGUser -Userid [email 
Get-MgCommunicationPresence -PresenceId $TeamsUser.Id | select Activity, Availability

[玩转系统] 如何使用 PowerShell 查询和更改团队用户的状态

可用的状态:

  • 可用性:有空、忙碌、离开、离线

  • 活动:空闲、通话中、请勿打扰、会议通话中、离开、下班、BeRightBack

您可以列出租户中所有用户的 Teams 状态:

$allUserStatus = @()
$AllUsers=Get-MGUser
foreach ($TeamUser in $AllUsers) 
{ 
$TeamsStatus=Get-MgCommunicationPresence -PresenceId $TeamUser.Id 
$CurUserStatus = New-Object PSObject -Property @{
    Activity=$TeamsStatus.Activity
    Availability=$TeamsStatus.Availability
    DisplayName=$TeamUser.DisplayName
}
$allUserStatus += $CurUserStatus
}
$allUserStatus

[玩转系统] 如何使用 PowerShell 查询和更改团队用户的状态

如果要从脚本获取 Teams 中的用户状态,请创建一个新应用程序(Azure AD -> 应用程序注册)并委派

Presence.ReadWrite.All

对其的许可(或

Presence.Read

Present.Read.All

以用户身份运行应用程序时的权限)。

连接到您的租户并获取令牌:

$ApplicationID = "1111111-1111-1111-1111-11111111111"
$TenatDomainName = "2222222-2222-2222-2222-222222222222"
$AccessSecret = "3333333333333333333333333333333333333333333"
$Body = @{
    Grant_Type = "client_credentials"
    Scope = "https://graph.microsoft.com/.default"
    client_Id = $ApplicationID
    Client_Secret = $AccessSecret
}
$ConnectGraph = Invoke-RestMethod -Uri https://login.microsoftonline.com/$TenatDomainName/oauth2/v2.0/token -Method POST -Body $Body 

详细了解如何通过 Microsoft Graph API 从 PowerShell 访问 Azure。

通过 API 访问 Azure 时,必须提供用户 ID(ObjectId、用户对象 GUID)而不是 UserPrincipalName (UPN)。

$UserId = "111111-2222-3333-4444-555555555"
$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type" = "application/json"
    }
$ApiUrl = "https://graph.microsoft.com/v1.0/users/$UserId/presence"
$Response = Invoke-RestMethod -Method GET -Uri $ApiUrl -ContentType "application\json" -Headers $headers -SkipHeaderValidation
$Response 

你还可以使用此 Azure 应用程序通过 PowerShell 读取消息或向 Teams 聊天发送消息。

如何从 PowerShell 更改团队的状态

您可以使用 PowerShell 和 Graph API 更改用户在 Teams 中的状态。使用 Graph API 连接到 Azure,如上所示。

使用以下脚本更改用户状态 1 小时 (PT1H):

$UserId = "111111-2222-3333-4444-555555555"
$uri = "https://graph.microsoft.com/beta/users/$userid/presence/setPresence"
$body = @"
{
    "sessionId": "$ApplicationID",
    "availability": "Away",
    "activity": "Away",
    "expirationDuration": "PT1H"
  }
"@
Invoke-RestMethod -Uri $uri -Method Post -Body $body -Headers $headers -ContentType "application/json"

检查 Teams 中的用户状态是否已更改:

Get-MgCommunicationPresence -PresenceId $UserId

[玩转系统] 如何使用 PowerShell 查询和更改团队用户的状态

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯