使用Powershell创建团队、通道和创建文件

ygya80vv  于 2022-11-29  发布在  Shell
关注(0)|答案(2)|浏览(139)

我正在尝试创建一个团队,添加频道,然后使用Powershell创建一些文件夹。我已经把代码放在下面(我已经注解了添加用户,因为这是目前产生的另一个问题)。

#Setup named parameters
param ($TeamName, $TeamDescription, $headteacheremail, $hrcontactemail)

#Sign in to Microsoft Teams with mfa.

Connect-MicrosoftTeams -AccountId user@domain.com

Connect-ExchangeOnline -UserPrincipalName user@domain.com

$team = New-Team -MailNickname $TeamName -displayname $TeamName -Visibility "private"
Write-Host "Team Created: $TeamName"
Write-Host "Team GUID: $team.GroupID"

New-TeamChannel -GroupId $team.GroupId -DisplayName "2021 - 2022 Academic Year"
New-TeamChannel -GroupId $team.GroupId -DisplayName "Induction"
New-TeamChannel -GroupId $team.GroupId -DisplayName "Probation (Support Staff Only)"
New-TeamChannel -GroupId $team.GroupId -DisplayName "Employee Documents" -MembershipType "private"
Write-Host "Channels Created"

#Add the headteacher to the team as a owner
Add-TeamUser -GroupId $team.GroupId -Role Owner -User $headteacheremail
#Add the hr contact to the team as a owner
Add-TeamUser -GroupId $team.GroupId -Role Owner -User $hrcontactemail
Write-Host "Users added to channels"

Write-Host "Sleeping for 10 seconds"
#Give time for MS servers to create the team and channels before trying to interact with them. 10 seconds
Start-Sleep -s 10

#Add-TeamChannelUser -GroupId $team.GroupId -DisplayName "Employee Documents" -User $headteacheremail
#Add-TeamChannelUser -GroupId $team.GroupId -DisplayName "Employee Documents" -User $hrcontactemail
#Write-Host "Users added to channels"

$SiteURL = Get-UnifiedGroup -Identity $TeamName | Select -ExpandProperty SharePointSiteURL

Connect-PnPOnline -Url $SiteURL -UseWebLogin

Add-PnPFolder -Name "Bench marking documents" -Folder "Shared Documents/2021 - 2022 Academic Year"
Add-PnPFolder -Name "Career Development Plan - current" -Folder "Shared Documents/2021 - 2022 Academic Year"
Add-PnPFolder -Name "Flick Training Certificates" -Folder "Shared Documents/2021 - 2022 Academic Year"
Add-PnPFolder -Name "Professional Growth Plan - current" -Folder "Shared Documents/2021 - 2022 Academic Year"

Add-PnPFolder -Name "Flick Induction Training Certificate" -Folder "Shared Documents/Induction"
Add-PnPFolder -Name "Induction Policies" -Folder "Shared Documents/Induction"

Add-PnPFolder -Name "3 Month Review" -Folder "Shared Documents/Induction"
Add-PnPFolder -Name "6 Month Review" -Folder "Shared Documents/Induction"

团队已创建,频道和用户已添加,但在添加文件夹时出现以下错误。

Connect-PnPOnline : Cannot bind argument to parameter 'Url' because it is null.

如果我使用Team名称单独运行命令,它会返回sharepoint URL ok。
我不知道这是怎么回事。

ulydmbyx

ulydmbyx1#

奇怪,但我把它改回了下面,它现在工作了。不知道为什么或如何。

$SiteURL = Get-UnifiedGroup -Identity $TeamName | Select -ExpandProperty SharePointSiteURL

现在我遇到了一个障碍,微软似乎不会自动为各个频道创建sharepoint文件夹,只有在团队中打开相应的文件部分时才会出现。这意味着我不能在团队创建时创建文件夹:(

f0ofjuux

f0ofjuux2#

我可能在这里重提一个老问题,但如果有人正在寻找OP提到的“路障”的解决方案,如果你使用图形API创建通道,你可以发送一个“Invoke-RestMethod”来“初始化"SPO站点上的文件夹。
您将需要创建一个应用程序注册,并按照许多其他指南设置标题,然后您可以使用下面的创建频道和文件夹:

function New-TeamChannel {
        Param (
        [Parameter(Mandatory=$true)]$TeamID,
        [Parameter(Mandatory=$true)]$ChannelName
        )
        # https://learn.microsoft.com/en-us/graph/api/channel-post?view=graph-rest-1.0&tabs=http
        $CreateChannelURL = "https://graph.microsoft.com/v1.0/teams/$TeamID/channels"
        $CreateChannelBody = '{
            "displayName": "'+$ChannelName+'",
            "description": "'+$ChannelName+'",
            "membershipType": "standard"
        }'
        try {
            $channelCreated = Invoke-RestMethod -Uri $CreateChannelURL -Method Post -body $CreateChannelBody -Headers $M365headerParams_MSG -ContentType Application/JSON
            write-host "Channel created:" $channelCreated.displayName -NoNewline -ForegroundColor Cyan
            #need to send another call in order to initialise the folder structure in SPO
            $initialiseFolderURL = "https://graph.microsoft.com/v1.0/teams/$TeamID/channels/"+$channelcreated.id+"/filesfolder"
            $initialisefolder = $null
            $initialisefolder = Invoke-RestMethod -Uri $initialiseFolderURL -Method Get -Headers $M365headerParams_MSG 
            if ($initialisefolder) {write-host "Channel initialised in SPO" -ForegroundColor Yellow}
        }
        catch {
            write-host "Error creating channel named $ChannelName" -ForegroundColor Red
        }
    }

这可以在foreach循环中调用以创建多个团队,您只需每次提供参数。“$newteam”是刚刚创建的团队的GUID。

foreach ($ChannelName in $listofchannels) {
        New-TeamChannel -TeamID $newTeam -ChannelName $channelName
    }

相关问题