PowerShell 脚本示例 - 创建和分配消息传递策略PowerShell script sample - Create and assign a messaging policy

使用此 PowerShell 脚本在 Microsoft 团队中创建邮件策略,并将其分配给用户。Use this PowerShell script to create a messaging policy in Microsoft Teams and assign it to users.

有关使用此 PowerShell 脚本的详细信息,请参阅快速入门-团队教育版。For more information about using this PowerShell script, see Quick start - Teams for Education.

此脚本使用 Skype for Business Online PowerShell 模块中的CsTeamsMessagingPolicy cmdlet。This script uses the Grant-CsTeamsMessagingPolicy cmdlet which is in the Skype for Business Online PowerShell module. 请参阅团队 PowerShell 概述以了解有关使用 PowerShell 管理团队的详细信息。See Teams PowerShell overview to learn more about managing Teams using PowerShell.

开始前Before you start

下载并安装Skype For Business Online PowerShell 模块,如果出现提示,请重新启动计算机。Download and install the Skype for Business Online PowerShell module, and then restart your computer if prompted.

若要了解详细信息,请参阅管理 Office 365 PowerShell 的 Skype For Business OnlineTo lean more, see Manage Skype for Business Online with Office 365 PowerShell.

示例脚本Sample script

<#
.SYNOPSIS
This script creates a messaging policy in Teams and assigns it to users.
.DESCRIPTION
Use this script to create a messaging policy and assign it to users in your organization.
#>

$dataSetFilePath = "<csv file with user ids for newly provisioned students> "
 $dataSet = Import-Csv "$($dataSetFilePath)" -Header UserId –delimiter ","
 foreach($line in $dataSet)
 {
    $userId = $line.UserId
    Write-Host $userId
    Grant-CsTeamsMessagingPolicy -PolicyName "<<PolicyName for a policy created with Chat Off>>" -Identity $userId

 }

备注

你还可以通过批处理策略分配将消息策略直接分配给用户,或者将消息策略分配给用户是其成员的组。You can also assign a messaging policy directly to users at scale through a batch policy assignment or to a group that the users are members of. 有关详细信息,请参阅为你的学校中的大型用户分配策略,并为团队中的用户分配策略For more information see Assign policies to large sets of users in your school and Assign policies to your users in Teams.