Search code examples
powershellmicrosoft-graph-apimicrosoft-graph-teams

Failing Script to keep teams user available


I have a (hopefully) small Problem with a script I wrote. I have 2 dummy users in Teams, which aren't related to anyone and noone can access them since they are just used to forward calls. Now we need them to be available so they can be called by a queue. The script I wrote know tells me that its missing a "=" at a hashliteral on three different lines.

# Graph API Details
$tenantId = "***"
$clientId = "***"
$clientSecret = "***"
 
# Function to get OAuth token
function Get-OAuthToken {
    param (
        [string]$tenantId,
        [string]$clientId,
        [string]$clientSecret
    )
 
    $body = @{
        grant_type    = "client_credentials"
        scope         = "https://graph.microsoft.com/.default"
        client_id     = $clientId
        client_secret = $clientSecret
    }
 
    $response = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -ContentType "application/x-www-form-urlencoded" -Body $body
    return $response.access_token
}
 
# Get the OAuth token
$token = Get-OAuthToken -tenantId $tenantId -clientId $clientId -clientSecret $clientSecret
 
# Users to keep online
$users = @("")
 
# Function to set user presence status to available
function Set-UserPresence {
    param (
        [string]$user,
        [string]$token
    )
 
    $uri = "https://graph.microsoft.com/v1.0/users/$user/presence/setPresence"
    $headers = @{
        Authorization = "Bearer $token"
        Content-Type  = "application/json"
        }
    $body = @{
        sessionId     = [guid]::NewGuid().ToString()
        availability  = "Available"
        activity      = "Available"
        expirationDuration = "PT1H"
    }
 
    $jsonBody = $body | ConvertTo-Json
    Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body $jsonBody
}
 
# Loop to keep users online
while ($true) {
    foreach ($user in $users) {
        Set-UserPresence -user $user -token $token
        Write-Output "Set presence to Available for $user at $(Get-Date)"
    }
 
    # Wait for 5 minutes before setting the presence again
    Start-Sleep -Seconds 300
}

I tried counting brackets and looked for any missing commas or something like that, but I didnt find anything. Maybe someone on here has better eyes than me or an idea what problem it could be if not some missing bracket

Thanks in advance


Solution

  • Hash table keys that are strings that must be quoted if they contain a HYPHEN-MINUS (-) character. Content-Type must be quoted to be a key for the $headers hash.

    Change:

    Content-Type = "application/json"
    

    to:

    'Content-Type' = "application/json"
    

    Experimentation at the console command line.

    PS C:\src\t\pscoretest> $h = @{a=2;b=3;c-c=2}
    ParserError: 
    Line |
       1 |  $h = @{a=2;b=3;c-c=2}
         |                  ~
         | Missing '=' operator after key in hash literal.
    PS C:\src\t\pscoretest> $h = @{a=2;b=3;'c-c'=2}
    PS C:\src\t\pscoretest> $h
    
    Name                           Value
    ----                           -----
    b                              3
    a                              2
    c-c                            2
    

    Output from the PSScriptAnalyzer module.

    PS C:\src\t> Invoke-ScriptAnalyzer -Path .\afile.ps1
    
    RuleName                            Severity     ScriptName Line  Message
    --------                            --------     ---------- ----  -------
    MissingEqualsInHashLiteral          ParseError   afile.ps1  41    Missing '=' operator after key in hash literal.
    PSUseShouldProcessForStateChangingF Warning      afile.ps1  32    Function 'Set-UserPresence' has verb that could change
    unctions                                                          system state. Therefore, the function has to support
                                                                      'ShouldProcess'.