在對某些集成腳本執行特定操作之前,可能有必要檢查Microsoft團隊中用戶的存在狀態(在線/外出/忙)。讓我們看一下如何使用Microsoft Graph API和PowerShell在團隊中獲得和更改用戶的狀態。
內容:
如果是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 protected]
Get-MgCommunicationPresence -PresenceId $TeamsUser.Id | select Activity, Availability
可用的存在狀態:
- 可用性:可用,忙碌,離開,離線
- 活動:可用,Inacall,Donotdisturb,InaconFerenceCall,Away,affwork,berightback
您可以列出租戶中所有用戶的團隊在場狀態:
$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
如果您想從腳本中獲得團隊中的用戶狀態,請創建一個新應用(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),而不是用戶principalname(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讀取或發送消息給團隊聊天。
如何從PowerShell改變團隊的存在狀態
您可以使用PowerShell和Graph API來更改用戶在團隊中的存在狀態。如上圖所示,使用圖形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"
檢查團隊中的用戶狀態是否發生了變化:
Get-MgCommunicationPresence -PresenceId $UserId