Home > Software engineering >  Remove 'True' and replace with object in a ForLoop in Powershell
Remove 'True' and replace with object in a ForLoop in Powershell

Time:08-25

I have a small script that loops through a .csv file and applies a policy based on their location. It works fine except that when it runs its prints out "True" for every Line. How do you remove the word True and replace it with the username or something else or even nothing?

Error

 Connect-MicrosoftTeams
  $userList = Import-Csv -Path "C:\Users\locationbasedpolicy1.csv"

 Write-host "Logging to Microsoft Tenant to Execute Policies....." -ForegroundColor Cyan
 Write-Host "There are $($userList.Count) Users in the List" -ForegroundColor Magenta

 $TotalItems=$userList.Count
 $CurrentItem = 1

 # List Policies for Different Locations

  $policyMappings = @{
    USA  =  @{
    VRP = 'VRP-USA-International';
    CPP = 'CPP-USA-All-Users';
    ECRP = 'ECRP-USA-All-Users';
    ECP = 'ECP-USA-All-Users';
    CHP = 'CHP-Global-MOH';
    CIPD = 'CIDP-Global-User-Policy'
  
  }
    UK  =  @{
    VRP = 'VRP-UK-National';
    CPP = 'CPP-UK-All-Users';
    ECRP = 'ECRP-UK-All-Users';
    ECP = 'ECP-UK-All-Users';
    CHP = 'CHP-Standard-MOH';
    CIDP = 'CIDP-Block-Number'
   }     
  }

 # Loop Through the csv and identify Users Information
 foreach ($Users in $userList){
  Try {
    $upn = $Users.UserPrincipalName
    #$phone = $Users.TelephoneNumber
    $Location = $Users.Location

 # Search for Users Location
    $policyMappings.ContainsKey($Location)
    $policy = $policyMappings[$Location]

 # Apply Policies to User based on their Location    
    Grant-CsOnlineVoiceRoutingPolicy -Identity $upn -PolicyName $policy.VRP
    Grant-CSTeamsCallParkPolicy -Identity $upn -PolicyName $policy.CPP
    Grant-CSTeamsEmergencyCallRoutingPolicy -Identity $upn -PolicyName $policy.ECRP
    Grant-CsTeamsCallHoldPolicy -Identity $upn -PolicyName $policy.CHP
    Grant-CSTeamsEmergencyCallingPolicy -Identity  $upn -policyname $policy.ECP
    Grant-CsCallingLineIdentity -Identity $upn -PolicyName $policy.CIDP
          
        $PercentCompleted = [int](($CurrentItem / $TotalItems) * 100)
        Write-progress -Activity "Processing User $CurrentItem of $TotalItems : $upn"  -Status 
        "Total percent completed is: $PercentCompleted%:" -PercentComplete $PercentCompleted
        $CurrentItem  
        Start-Sleep -Milliseconds 3000

 } Catch { Write-Host "Failed to assign policies to : $($upn)" -ForegroundColor Yellow }
 }

CodePudding user response:

Adding following changes and it worked.

$null = $($policyMappings.ContainsKey($Location))

  • Related