question

MS-5605 avatar image
0 Votes"
MS-5605 asked MS-5605 commented

Powershell for Teams to grant policy through input file

Hi Experts,

I need a script that will take as input a file containing UPNS and pass each UPN to a cmdlet used to apply (grant) a specific meeting policy. We also need a script to export the current meeting policy config for the same input file. The script that applies the new meeting policy must include logging confirming that the policy was applied successfully.

Please help me with the above requirements,

Thanks,
MS

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered MS-5605 edited

Hi,

You can manage the policies via the Skype for Business module.
https://docs.microsoft.com/en-us/microsoftteams/teams-powershell-managing-teams#manage-policies-via-powershell
https://docs.microsoft.com/en-us/microsoft-365/enterprise/manage-skype-for-business-online-with-microsoft-365-powershell

The Grant-CsTeamsMessagingPolicy cmdlet assigns a teams meeting policy at the per-user scope.

 Import-Csv C:\temp\teams.csv | ForEach-Object {
     Grant-CsTeamsMeetingPolicy -identity $_.UPN -PolicyName "Policy1"
 }

To get the information about the meeting configuration settings you can use the Get-CsMeetingConfiguration cmdlet.
https://docs.microsoft.com/en-us/powershell/module/skype/grant-csteamsmeetingpolicy
https://docs.microsoft.com/en-us/powershell/module/skype/get-csmeetingconfiguration

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Lan,

Thank you, but I need few more changes to the request.

  1. after reading the input file, the script should ask which policy should apply /grant. once we key in the policy name, it should grant.

  2. once the script grants the policy, an output file should be confirming that the policy was applied successfully or not.

Maybe we can use New-CsBatchPolicyAssignmentOperation (link given below) and build the script. sorry, I am new to PowerShell.

https://office365itpros.com/2020/07/08/bulk-policy-assignment-teams-users/amp/

2nd Script:
We also need a script to export the current meeting policy config for the same input file. (2nd Script )


Thank you much.

Regards,
MS








0 Votes 0 ·
KartheekRaparthy-6860 avatar image
0 Votes"
KartheekRaparthy-6860 answered MS-5605 commented

As per your requirement this script ask the policy for each user.

$logfile = ".\UserPolicyAssignmentlog_$(get-date -format "yyyyMMdd_hhmmsstt").txt"
$start = [system.datetime]::Now

Import-Module SkypeOnlineConnector
$sfbSession = New-CsOnlineSession
Import-PSSession $sfbSession -AllowClobber

$UserPrincipalNames = import-csv -Path .\Input.csv
$UserPricipleNames = $Input.UserPricipleName
$count = $Input.Count
write-host "Running the script for users:" $count

foreach($UserPrincipalName in $UserPrincipalNames.UserPrincipalName)
{
Write-Host "To change the Applied Policy to user" $UserPrincipalName


     function Get-Result() {
     write-host           "1- TeamsAppSetupPolicy 
                   2- TeamsMeetingPolicy 
                   3- TeamsCallingPolicy
                   4- TeamsMessagingPolicy 
                   5- BroadcastMeetingPolicy
                   6- TeamsCallParkPolicy
                   7- CallerIdPolicy 
                   8- TeamsEmergencyCallingPolicy 
                   9- TeamsEmergencyCallRoutingPolicy
                   10-VoiceRoutingPolicy 
                   11-TeamsAppPermissionPolicy 
                   12-TeamsDailPlan"

$proceed = Read-host "Please provide the policy number to Grant and proceed further"


if ($proceed -eq '1')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl TeamsAppSetupPolicy
Get-CSTeamsAppsetuppolicy |fl Identity
$PolicyName=Read-Host "Please provide the Policy Name"
Grant-CsTeamsAppSetupPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$_.Exception.Message | out-file -Filepath $logfile -append
}
}


elseif ($proceed -eq '2')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl TeamsMeetingPolicy
Get-CsTeamsMeetingPolicy |fl Identity
Grant-CsTeamsMeetingPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$_.Exception.Message | out-file -Filepath $logfile -append
}
}

elseif ($proceed -eq '3')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl TeamsCallingPolicy
Get-CsTeamsCallingPolicy |fl Identity
$PolicyName= Read-Host "Please provide the Policy Name"
Grant-CsTeamsCallingPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$_.Exception.Message | out-file -Filepath $logfile -append
}
}

elseif ($proceed -eq '4')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl TeamsMessagingPolicy
Get-CsTeamsMessagingPolicy |fl Identity
$PolicyName = Read-Host "Please provide the Policy Name"
Grant-CsTeamsMessagingPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$_.Exception.Message | out-file -Filepath $logfile -append
}
}


elseif ($proceed -eq '5')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl BroadcastMeetingPolicy
Get-CsBroadcastMeetingPolicy |fl Identity
$PolicyName=Read-Host "Please provide the Policy Name"
Grant-CsBroadcastMeetingPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$_.Exception.Message | out-file -Filepath $logfile -append
}
}

elseif ($proceed -eq '6')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl TeamsCallParkPolicy
Get-CsTeamsCallParkPolicy|fl Identity
$PolicyName=Read-Host "Please provide the Policy Name"
Grant-CsTeamsCallParkPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$_.Exception.Message | out-file -Filepath $logfile -append
}
}

elseif ($proceed -eq '7')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl CallerIdPolicy
Get-CsTeamsCallerIdPolicy|fl Identity
$PolicyName=Read-Host "Please provide the Policy Name"
Grant-CsTeamsCallerIdPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$.Exception.Message | out-file -Filepath $logfile -append
}
}
elseif ($proceed -eq '8')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl TeamsEmergencyCallingPolicy
Get-CsTeamsEmergencyCallingPolicy |fl Identity
$PolicyName=Read-Host "Please provide the Policy Name"
Grant-CsTeamsEmergencyCallingPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$
.Exception.Message | out-file -Filepath $logfile -append
}
}
elseif ($proceed -eq '9')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl TeamsEmergencyCallRoutingPolicy
Get-CsTeamsEmergencyCallRoutingPolicy |fl Identity
$PolicyName=Read-Host "Please provide the Policy Name"
Grant-CsTeamsEmergencyCallRoutingPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$.Exception.Message | out-file -Filepath $logfile -append
}
}
elseif ($proceed -eq '10')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl VoiceRoutingPolicy
Get-CsVoiceRoutingPolicy | fl Identity
$PolicyName=Read-Host "Please provide the Policy Name"
Grant-CsVoiceRoutingPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$
.Exception.Message | out-file -Filepath $logfile -append
}
}
elseif ($proceed -eq '11')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | fl TeamsAppPermissionPolicy
Get-CsTeamsAppPermissionPolicy |fl Identity
$PolicyName=Read-Host "Please provide the Policy Name"
Grant-CsTeamsAppPermissionPolicy -identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$_.Exception.Message | out-file -Filepath $logfile -append
}
}

elseif($proceed -eq '12')
{
try{
Get-CsOnlineUser -Identity "$UserPrincipalName" | FL DialPlan
Get-CsDialPlan|fl Identity
$PolicyName=Read-Host "Please provide the Policy Name"
Grant-CsDialPlan -Identity "$UserPrincipalName" -PolicyName "$PolicyName"
}
catch{
$_.Exception.Message | out-file -Filepath $logfile -append
}
}
}

do
{

$ProceedNext = Read-host "Do you want to apply policy,press Y to continue"
if ($ProceedNext -eq "Y" )
{
Get-Result
}
else
{
break
}
}
while($true);
}

$end = [system.datetime]::Now
$resultTime = $end - $start
Write-Host "Execution took : $($resultTime.TotalSeconds) seconds." -ForegroundColor Cyan
$resultTime.TotalSeconds

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so much. Much appreciated

I ran the script and it's working perfectly, Could you please modify your script to accept only one option after we select from 1 to 12 and then
grant the policy name and policy type for all input users and save the final results in the output file.

Thanks in advance.

Regards,
MS

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

You can add a Read-Host line to get the policy name

 $policy = Read-Host "Input the policy name:"
 Import-Csv C:\temp\teams.csv | ForEach-Object {
     Grant-CsTeamsMeetingPolicy -identity $_.UPN -PolicyName $policy
 }

If you want to write to a file once the script grants the policy, you can use the Out-File cmdlet like this

 "Policy granted successfully." | Out-File C:\temp\output.txt

To export the policy config to a file you can still use Out-File.

 Get-CsMeetingConfiguration | ConvertTo-Json | Out-File C:\temp\config.json 

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MS-5605 avatar image
0 Votes"
MS-5605 answered

Thank you so much for your inputs. The below script is working for my requirements.

Could you please help me with a script to export the user's teams policies from an input CSV file (UPNs). This will solve my 2nd request.



PS Script:

$PolicyType = Read-Host "Please enter Team Policy Type"
$PolicyName = Read-Host "Please enter Team PolicyName"
$TargetUsers = Import-CSV c:\temp\teams.CSV
$PolicyUsers = [System.Collections.Generic.List[Object]]::new()
ForEach ($U in $TargetUsers) { $PolicyUsers.Add($U.UserPrincipalName) }

New-CsBatchPolicyAssignmentOperation -PolicyType $PolicyType -PolicyName $PolicyName -Identity $PolicyUsers -OperationName "Teams Messaging Policy Assignment"



MS

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

Please see if this works.

  Import-Csv -Path C:\temp\teams.CSV | ForEach-Object{
     Get-CsUserPolicyAssignment -Identity  $_.UserPrincipalName
  } | Export-Csv -Path C:\temp\userpolicies.csv


Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.