如何使用PowerShell Azure函数和Graph API创建Microsoft Teams团队?

问题描述

我的最终目标是创建一个具有应用程序通道和选项卡的MS Teams团队。 但是首先,我需要正确格式化请求。我不知道我在做什么错。 显然,我发现this topic (https://docs.microsoft.com/en-us/graph/api/team-post?view=graph-rest-1.0) Example n°3 看起来很有希望,但我不知道如何使用它。我从下面的代码开始:

$password = "stackexchange"
$login = "stackexchange@stackexchange.onmicrosoft.com"
$ownerEmail = "stackexchange@stackexchange.onmicrosoft.com"
$url = "https://graph.microsoft.com/v1.0/teams"   
$securedPassword = convertto-securestring -String $password -AsPlainText -Force
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $login,$securedPassword   
$GraphAppId = "stackexchange-guid"
$GraphAppSecret = "stackexchange"
$AADDomain = "stackexchange.onmicrosoft.com"

Connect-AzureAD -Credential $creds
$userId = (Get-AzureADUser -ObjectId $ownerEmail).ObjectId
write-output $userId # Here the userId is actually displayed

Connect-PnPOnline -ClientId $GraphAppId -ClientSecret $GraphAppSecret -AADDomain $AADDomain 
$accesstoken = Get-PnPGraphAccesstoken

$header = @{
    "Content-Type" = "application/json"
    Authorization = "Bearer $accesstoken"
}    

$body = @{  
    displayName = "Test"
    "owners@odata.bind" = "https://graph.microsoft.com/v1.0/users('$userId')"
    "template@odata.bind" = "https://graph.microsoft.com/v1.0/teamstemplates('standard')"
    memberSettings = @{
    allowCreateUpdateChannels = $true
    }
    messagingSettings = @{
    allowUserEditMessages = $true
    allowUserDeleteMessages = $true
    }
    funSettings = @{
    allowGiphy = $true
    giphyContentrating = "strict"
    }
}
$Body = ConvertTo-Json -InputObject $body    
Invoke-RestMethod -Uri $url -Body $Body -Method 'Post' -Headers $header -UseBasicParsing -Credential $creds

我在PowerShell终端中收到以下消息:

Invoke-RestMethod : {
    "error": {
        "code": "BadRequest","message": "Invalid bind property name owners in request.","innerError": {
        "date": "2020-09-03T15:40:53","request-id": "fef8bd7e-3143-4ea9-bcf6-a87702a488b8"
        }
    }
}
At character Line:36 : 5
+ Invoke-RestMethod -Uri $url -Body $Body -Method 'Post' -Headers $ ...
+ CategoryInfo          : InvalidOperation : (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod],WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand   

解决方法

我建议不要着眼于Graph SDK for PowerShell,而要手工完成所有操作。它仍然是Beta版的“官方”,但这当然是PowerShell PowerShell,而不是Graph本身。

,

您还可以使用PS nuget“ MicrosoftTeams”

例如:

# ===========================================
# this Script creates a new project environment containing:
# - a new TEAMs channel
# ===========================================

Install-Module MicrosoftTeams -Force # -AllowClobber


## parameters
$TeamDisplayName='contoso'
$ProjectName='Contoso-Reporting'

$TEAMS_ChannelName=$ProjectName

## connect to TEAMS
Connect-MicrosoftTeams

## Get the Opslogix TEAM
$team = Get-Team | foreach {if ( $_.DisplayName -eq $TeamDisplayName ) { $_ }}

## create a new project channel
$team | new-TeamChannel -DisplayName $TEAMS_ChannelName
#$team | Get-TeamChannel

## disconnect TEAMS
Disconnect-MicrosoftTeams
,

尝试更改:

"owners@odata.bind" = "https://graph.microsoft.com/v1.0/users('$userId')"

收件人:

members = @(
    @{
      '@odata.type' = "#microsoft.graph.aadUserConversationMember"
      roles = @(
        'owner'
      )
      'user@odata.bind' = "https://graph.microsoft.com/v1.0/users('$userId')"
    }
)