Forum Discussion
TomWechsler
Aug 16, 2021MVP
This is why we need PowerShell in Microsoft Teams!
Hi Microsoft Teams friends,
In Microsoft Teams, policies can be created as known from other Microsoft cloud services. So far, nothing new or unusual. Now the following scenario: An employee (who no longer works in the company) has set up a messaging policy, for example. Let's have a look at the following print screen:
Here we do not find out if a user has been added to this policy. If the employee has worked with group assignment, we see this in the following print screen (please whenever possible work with groups not with individual users when assigning policies):
No groups were used in this policy. But what about the users? Now you may be saying that can be checked in the Teams Admin Center at the users! Yes that is correct, but at which user? What if your organization has several hundred users? Let's look at the one user assigned to the exact policy we just examined. Lo and behold, here we are shown the policy.
Now how did I know that? Well, I am writing this article ;-). Now the PowerShell comes into play!
I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):
#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE
Set-Location c:\
Clear-Host
#Install the teams module
Install-Module MicrosoftTeams -AllowClobber -Force -Verbose
#Import the teams module
Import-Module -Name MicrosoftTeams
#Install the MSOnline module
Install-Module MSOnline -AllowClobber -Force -Verbose
#Credentials
$cred = Get-Credential
#Connect to Teams
Connect-MicrosoftTeams –Credential $cred
#Connect to Microsoft 365
Connect-MsolService –Credential $cred
#Did it work
Get-MsolUser
#Did it work
Get-Team
#Let's first examine the policy we have discussed so far
Get-CsOnlineUser -Filter {TeamsMessagingPolicy -eq 'Tom'} | Select UserPrincipalName
#We need the ObjectId of a user
Get-MsolUser -UserPrincipalName "tom@tomscloud.ch" | Select-Object ObjectId
#Check the assigment for a user
Get-CsUserPolicyAssignment -Identity ba36d788-31a5-4f72-9709-7d586056762c
#Check all users for some policies
Get-CsOnlineUser | Format-Table UserPrincipalName, TeamsMessagingPolicy, TeamsMeetingPolicy, TeamsAppSetupPolicy
#The details of the policy
Get-CsTeamsMessagingPolicy -Identity "Tom"
#To grant a single user a Messaging Policy (What actually should not be done)
Grant-CsTeamsMessagingPolicy -Identity fred.jonas@tomscloud.ch -PolicyName "Tom"
This customer (in this example) still had the requirement to assign a policy to specific people in a department.
#Search all users in the department "Administration"
Get-CsOnlineUser -Filter {Department -eq 'Administration'} | Select UserPrincipalName
#Set a Policy for the specific users
Get-CsOnlineUser -Filter {Department -eq 'Administration'} | Grant-CsTeamsMessagingPolicy -PolicyName "Tom"
#Let's check
Get-CsOnlineUser -Filter {TeamsMessagingPolicy -eq 'Tom'} | Select UserPrincipalName
#an other way
Get-CsOnlineUser | Format-Table UserPrincipalName, TeamsMessagingPolicy, TeamsMeetingPolicy, TeamsAppSetupPolicy
#Undo
Get-CsOnlineUser -Filter {Department -eq 'Administration'} | Grant-CsTeamsMessagingPolicy -PolicyName $Null
#Other example
Get-CsOnlineUser -Filter {Department -eq 'Administration'} | Grant-CsTeamsMeetingPolicy -PolicyName $Null
#Other example
Get-CsOnlineUser -Filter {Department -eq 'Administration'} | Grant-CsTeamsAppSetupPolicy -PolicyName $Null
#Remove a policy from a user
Grant-CsTeamsMessagingPolicy -Identity fred.jonas@tomscloud.ch -PolicyName $null
I am aware that this was not necessarily spectacular, but I wanted to share this information with you. Thank you for taking the time to read this article.
I hope this article was useful. Best regards, Tom Wechsler
P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github! https://github.com/tomwechsler
- ThereseSolimeno
Microsoft
Thanks for sharing with the community.- It is a great pleasure for me!