Search code examples
powershellmicrosoft-teamsazure-powershell

Powershell Loop through .csv. Find cell and apply certain policies in script to a matching field


I'm trying to write a script in Powershell, that will loop through a .csv file that contains data for Microsoft Teams. I need to apply calling policies based on the user's location. So, Look at the UPN in the first cell, if Valid, then check the location and then apply the policies based on that location. Please see the picture attached. Also, there is a piece of code I have attached with previous help as I have limited knowledge in PowerShell but I still can't get it right. Please help.

Steps for Changes:

  1. After successful Login into MS Teams PowerShell:

  2. Apply Policy 1 to all Users in the .csv File

    • Look at the UPN and assign the Telephone Number
    • Enable Enterprise Voice
  3. Look at the Location of each UPN and apply the matching Policy

E.g.1 ironman is in the USA, therefore he gets assigned Policy 2 – USA Attributes

E.g.2 superboy is in the UK, therefore he gets assigned Policy 3 – UK Attributes

And so forth

If there is no policy for the location, write a message: Policy doesn’t exist.

Changes Required

Connect-MicrosoftTeams

$policyMappings = @{
USA  =  {Set-CsUser -Identity $upn -EnterpriseVoiceEnabled $false
        Set-CsPhoneNumberAssignment -Identity $upn -PhoneNumber $phone -PhoneNumberType 
         DirectRouting 
         Grant-CsOnlineVoiceRoutingPolicy -PolicyName Standard-Lab-User-VRP -Identity $upn }


UK   = {Set-CsUser -Identity $upn -EnterpriseVoiceEnabled $false
        Set-CsPhoneNumberAssignment -Identity $upn -PhoneNumber $phone -PhoneNumberType 
        DirectRouting 
        Grant-CsOnlineVoiceRoutingPolicy -PolicyName Restricted-Lab-User-PSTN-Usage -Identity 
        $upn }

Asia =  {Set-CsUser -Identity $upn -EnterpriseVoiceEnabled $true
        Set-CsPhoneNumberAssignment -Identity $upn -PhoneNumber $phone -PhoneNumberType 
        DirectRouting 
        Grant-CsOnlineVoiceRoutingPolicy -PolicyName All-Inclusive-Lab-User-VRP -Identity $upn 
   }
        
 }

   foreach ($user in Import-Csv -Path 
   C:\Users\admcdg\Desktop\users_all_info_export.csv)
  {

  $upn = $user.userprincipalname
  $phone = $user.Phonenumber


  if($policyMappings.ContainsKey($user.Location)){
      $policy = $policyMappings[$user.Location]
  }
  else {
      # default to the global policy if no mapping is found
   

Solution

  • I would create a nested mapping Hashtable to store the different attributes for each location like

    # create mappings as hash-of-Hashtables.
    $policyMappings = @{
        USA  = @{EnterpriseVoiceEnabled = $false; PolicyName = 'Standard-Lab-User-VRP'}
        UK   = @{EnterpriseVoiceEnabled = $false; PolicyName = 'Restricted-Lab-User-PSTN-Usage'}
        Asia = @{EnterpriseVoiceEnabled = $true;  PolicyName = 'All-Inclusive-Lab-User-VRP'}
    }
    

    Then you can apply the policies for each user like this:

    foreach ($user in (Import-Csv -Path 'C:\Users\admcdg\Desktop\users_all_info_export.csv')) {
        $upn = $user.userprincipalname
        $phone = $user.Phonenumber
        $location = $user.Location
        if ($policyMappings.ContainsKey($location)) {
            $policy = $policyMappings[$location]
            Set-CsUser -Identity $upn -EnterpriseVoiceEnabled $policy.EnterpriseVoiceEnabled
            Set-CsPhoneNumberAssignment -Identity $upn -PhoneNumber $phone -PhoneNumberType DirectRouting
            Grant-CsOnlineVoiceRoutingPolicy -PolicyName $policy.PolicyName -Identity $upn
        }
        else {
            # default to the global policy if no mapping is found
            Set-CsUser -Identity $upn -LineUri $phone -EnterpriseVoiceEnabled $true
        }
    }