Update-MgUserPresence : Error authenticating with resource

Roberto G 20 Reputation points
2024-07-23T21:00:02.87+00:00

Hello everybody,

I wrote this code to automatically set the status on Microsoft Teams at a given time:

But when I run it I get this error:

Update-MgUserPresence : Error authenticating with resource

Status: 400 (BadRequest)

ErrorCode: AuthenticationError

Date: 2024-07-23T20:28:52

Headers:

Transfer-Encoding : chunked

Vary : Accept-Encoding

Strict-Transport-Security : max-age=31536000

request-id : 87db3c9f-7a31-4959-bd91-5b0f4609be5a

client-request-id : 50a7370d-4654-4614-b643-df535fd2cbad

x-ms-ags-diagnostic : {"ServerInfo":{"DataCenter":"Switzerland

North","Slice":"E","Ring":"3","ScaleUnit":"000","RoleInstance":"ZR1PEPF0000066C"}}

Date : Tue, 23 Jul 2024 20:28:51 GMT

In C:\Users\my_user\Desktop\MyScript.ps1:33 car:1

  • Update-MgUserPresence -UserId $userId -Availability $statusAvailabili ...
  • 
        + CategoryInfo          : InvalidOperation: ({ UserId = my_email_address...GraphPresence }:<>f__AnonymousType56`3) [Update-MgU
    
       serPresence_UpdateExpanded], Exception
    
        + FullyQualifiedErrorId : AuthenticationError,Microsoft.Graph.PowerShell.Cmdlets.UpdateMgUserPresence_UpdateExpand
    
       ed
    
    
    
# Imposta l'ID dell'utente
$userId = "my_email_address"

# Imposta lo stato di disponibilità (es. Available, Busy, DoNotDisturb, etc.)
$statusAvailability = "Busy"

# Autentica con Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Imposta l'ora a cui desideri che lo stato venga impostato
$targetTime = Get-Date -Hour 22 -Minute 39 -Second 0

# Controlla l'orario attuale
$currentDateTime = Get-Date
Bu
# Verifica se è già passata l'ora di impostazione
if ($currentDateTime -ge $targetTime) {
    Write-Host "È già passata l'ora di impostazione. Lo stato verrà impostato domani."
    $targetTime = $targetTime.AddDays(1)
}

# Calcola la differenza di tempo tra l'orario attuale e l'orario di impostazione
$timeDifference = $targetTime - $currentDateTime

# Mostra un conto alla rovescia
$totalSeconds = [math]::Ceiling($timeDifference.TotalSeconds)
for ($i = $totalSeconds; $i -gt 0; $i--) {
    Write-Host -NoNewline "Attesa: $i secondi rimanenti `r"
    Start-Sleep -Seconds 1
}

# Imposta lo stato di disponibilità
Update-MgUserPresence -UserId $userId -Availability $statusAvailability
Write-Host "Stato di disponibilità impostato correttamente per l'utente con ID $userId alle 22:32."

# Disconnette la sessione di Microsoft Graph
Disconnect-MgGraph
$userId = "my_email_address"

# Imposta lo stato di disponibilità (es. Available, Busy, DoNotDisturb, etc.)
$statusAvailability = "Busy"

# Autentica con Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Imposta l'ora a cui desideri che lo stato venga impostato
$targetTime = Get-Date -Hour 22 -Minute 39 -Second 0

# Controlla l'orario attuale
$currentDateTime = Get-Date

# Verifica se è già passata l'ora di impostazione
if ($currentDateTime -ge $targetTime) {
    Write-Host "È già passata l'ora di impostazione. Lo stato verrà impostato domani."
    $targetTime = $targetTime.AddDays(1)
}

# Calcola la differenza di tempo tra l'orario attuale e l'orario di impostazione
$timeDifference = $targetTime - $currentDateTime

# Mostra un conto alla rovescia
$totalSeconds = [math]::Ceiling($timeDifference.TotalSeconds)
for ($i = $totalSeconds; $i -gt 0; $i--) {
    Write-Host -NoNewline "Attesa: $i secondi rimanenti `r"
    Start-Sleep -Seconds 1
}

# Imposta lo stato di disponibilità
Update-MgUserPresence -UserId $userId -Availability $statusAvailability

Write-Host "Stato di disponibilità impostato correttamente per l'utente con ID $userId alle 22:32."

# Disconnette la sessione di Microsoft Graph
Disconnect-MgGraph

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,771 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yakun Huang-MSFT 3,765 Reputation points Microsoft Vendor
    2024-07-24T06:54:07.44+00:00

    Hi @Roberto G

    This error may be due to the fact that you are not logged in, and you need to log in interactively when you connect to Graph, "Welcome To Microsoft Graph!" Is displayed, the login is successful.

    User's image

    See this link for more details:
    https://video2.skills-academy.com/en-us/powershell/microsoftgraph/get-started?toc=%2Fgraph%2Ftoc.json&view=graph-powershell-1.0#sign-in

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Vasil Michev 103.3K Reputation points MVP
    2024-07-24T06:57:03.09+00:00

    To update the presence/status value, you need to run the script with Presence.ReadWrite.All permissions, try adding that.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.