Logic error help

Hello,

Sorry if this is a double post, but once I add a new email address, it won’t index it correctly unless I add a new email address.

$MenuChoice = $null

While($MenuChoice -ne 0)
{
    $INI_Location = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini'
    $Read_INI = Get-Content $INI_Location

    $GroupCalendars = ($Read_INI | Select-String -CaseSensitive "[GroupCalendars]" -SimpleMatch).LineNumber
    $Users = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber
    $Calender_Groups = ($Read_INI | Select-String -CaseSensitive "[MembersOf-1]" -SimpleMatch).LineNumber - 2
    $Calender_Option = $Read_INI[$GroupCalendars..$Users]

    function Create_User
        {
            $New_User = Read-Host "Enter new employee's account name"
            $Read_INI[$Calender_Groups] += "`r`n$New_User"         
            Reindex_Users                                                
        }

    function Delete_User
        {
            $Del_User = Read-Host "Enter the email you want to delete"
            $Read_INI | where {$_ -ne $Del_User} | Set-Content $INI_Location -Force              
        }

    function Find_User_Index ([string]$email)
        {
            $E = ($Read_INI | Select-String -AllMatches "$email" -SimpleMatch)
            $E.ToString().Trim("=$email")
        }

    function Add_User_To_Calender
        {
            $Get_User = Read-Host "Enter email address of the employee to add"
            $Calender_Option
            $Get_Calender = Read-Host "Which calenders do you want to add" $Get_User "to?" 
            $Calender =  ($Calender_Option[$Get_Calender - 1])
            $Start = ($Read_INI | Select-String -CaseSensitive "[MembersOf-$Get_Calender]" -SimpleMatch).LineNumber
            $Next_Group = ($Read_INI[$Start..$Read_INI.Count] | Select-String -AllMatches "[Membersof" -SimpleMatch) | Select-Object -First 1
            $End = ($Read_INI | Select-String -CaseSensitive "$Next_Group" -SimpleMatch).LineNumber - 2
            $Line = $End - $Start
            $User = Find_User_Index $Get_User  
            $Write = "$Line=$User"
            $Read_INI[$End] += "`r`n$Write"
            $Read_INI | Set-Content $INI_Location -Force                                      
        }

    function Reindex_Users
        { 
            $List_of_Users = $Read_INI[$Users..$Calender_Groups]

            For($Index=0; $Index -lt $List_of_Users.length; $Index++)
            {
                $Index_Plus_One = $Index + 1;
                $Email = ($List_of_Users[$Index]) -creplace '^[^=]*=', ''                    
                $Re_Index = "$Index_Plus_One" + "=" +$Email
                $Read_INI = $Read_INI | Where-Object {$_ -ne $List_of_Users[$Index]}
                $Read_INI[$Users - 1] += "`r`n$Re_Index"
                $Read_INI | Set-Content $INI_Location -Force
            }        
        }

    switch ($MenuChoice)
    {
        1 {Create_User}
        2 {Delete_User}
        3 {Add_User_To_Calender}
    }
    
    Write-Host "[1]`tCreate new user"
    Write-Host "[2]`tDelete a user"
    Write-Host "[3]`tAdd user to calender"
    Write-Host "[0]`tExit menu"

    $MenuChoice = Read-Host "`nPlease select an option and press ENTER" 
    
    Write-Host "`n"
}

Once again :slight_smile:
You read your INI in the beginning
for example you have simple file

[Users]
1=user1

you read it in memory. (2 lines)
and when you add new user. you have in memory

[Users]
1=user1\r\nuser2

it’s again 2 lines. not 3 as you think.
and after reindexing you write it back and only then you get 3-lines file which you read again on cycle start

It seems you have not even tried to use object oriented code. but it simple as 123

  1. read data into hash of hashes # as in function which I linked
  2. on any command modify hash values # add new value and automatically get it reindexed $hash[$hash.Count+1] = $NewUsername
  3. write it back # here you need other function in place of set-content

Thank you,