Dillon
Dillon

Reputation: 61

Powershell find matching cell in .csv and apply commands for the the matching cell

enter image description hereRight up front, I want to say I have 0 knowledge of Powershell. I'm taking a chance to see if someone out there could help me.

I've been tasked to create a script that will look into a CSV file, run through each row, and based on the location of a User apply certain policies.

Example

CSV Contains

Name      Number           Location
ironman    +123456789        USA
superboy   +456987654        UK
Blackwidow 00789456798       Asia
  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.

enter image description here

Upvotes: 0

Views: 251

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174845

Use a hashtable to create a "mapping" between different locations and their relevant policy names. Then all you need to do is check whether the location in question has an associated mapping and apply that:

$policyMappings = @{
  'USA'  = 'Policy 1'
  'UK'   = 'Policy 2'
  'Asia' = 'Policy 3'
}

foreach($user in Import-Csv .\path\to\users.csv){
    # fetch the identifying information already in the CSV row
    $upn = $user.userprincipalname
    $phone = $user.Phonenumber

    # resolve the policy name
    if($policyMappings.ContainsKey($user.Location)){
        $policy = $policyMappings[$user.Location]
    }
    else {
        # default to the global policy if no mapping is found
        $policy = 'Global'
    }
    
    Set-CsUser -Identity $upn -EnterpriseVoiceEnabled $true
    Set-CsPhoneNumberAssignment -Identity $upn -PhoneNumber $phone -PhoneNumberType DirectRouting 
    Grant-CsOnlineVoiceRoutingPolicy -PolicyName $policy -Identity $upn 
    Grant-CsTeamsCallingPolicy -PolicyName $policy -Identity $upn
}

Upvotes: 2

Related Questions