# Function wrong?

**URL:** <https://forums.powershell.org/t/function-wrong/7708>\
**Category:** PowerShell Help\
**Created:** [December 13, 2016, 3:09pm UTC](https://forums.powershell.org/t/function-wrong/7708 "2016-12-13T15:09:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony-antony](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tony-antony/32/192_2.png) [@tony-antony](https://forums.powershell.org/u/tony-antony)\
**Post date:** [December 13, 2016, 3:09pm UTC](https://forums.powershell.org/t/function-wrong/7708/1 "2016-12-13T15:09:03Z")

</div>

Hello,

I asked this question yesterday, and I thought I got it working but it’s not. My first issue is that, when I enter a new email address, it adds it to the list, but it doesn’t number it. If I enter another email, it numbers the previous email correctly, but not the new one. Basically, it doesn’t number the latest email address.

I’m almost certain my function calls are not in the correct order, but I can’t figure it out.

I want it to number the email addresses when they’re created, not the next time a new email is added.

Example:

[1] Create new user  
[2] Delete a user  
[3] Add user to calender  
[0] Exit menu

Please select an option and press ENTER: 1

Enter new employee’s account name: [gps@company.com](mailto:gps@company.com)  
[1] Create new user  
[2] Delete a user  
[3] Add user to calender  
[0] Exit menu

**It created [gps@company.com](mailto:gps@company.com), but didn’t number it**

Please select an option and press ENTER: 1

Enter new employee’s account name: [wcs@company.com](mailto:wcs@company.com)  
[1] Create new user  
[2] Delete a user  
[3] Add user to calender  
[0] Exit menu

Please select an option and press ENTER: 0

**It created [wcss@company.com](mailto:wcss@company.com), but didn’t number it, it numbered [gps@company.com](mailto:gps@company.com)**

This is what this looks like now. Notice the number before gps, and no number before wcs. It added 1443 after I entered wcs.

[1443=gps@company.com](mailto:1443=gps@company.com)  
[wcs@company.com](mailto:wcs@company.com)

```
$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 -Pattern "[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
            $Read_INI | Set-Content $INI_Location -Force                     
        }

    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 -Pattern "[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 -AllMatches "$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++)
                {            
                    $Email = ($List_of_Users[$Index]) -creplace '^[^=]*=', ''
                    $Index_Plus_One = $Index + 1
                    $Re_Index = "$Index_Plus_One=$Email"  
                    $Read_INI[$Index+$Users] = ""           
                    $Read_INI[$Index+$Users] += $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"
}
```

Thank you,

Tony

---

<div class="post-metadata">

**Author:** ![tony-antony](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tony-antony/32/192_2.png) [@tony-antony](https://forums.powershell.org/u/tony-antony)\
**Post date:** [December 13, 2016, 4:36pm UTC](https://forums.powershell.org/t/function-wrong/7708/2 "2016-12-13T16:36:26Z")

</div>

Not a function issue, but a logic / syntax issue. I know what the problem is, just trying to figure out how to properly code it now.

---

<div class="post-metadata">

**Author:** ![max-kozlov](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@max-kozlov](https://forums.powershell.org/u/max-kozlov)\
**Post date:** [December 14, 2016, 1:18am UTC](https://forums.powershell.org/t/function-wrong/7708/3 "2016-12-14T01:18:17Z")

</div>

You have a logic error when you add new user like this `$Read_INI[$Calender_Groups] += “`r`n$New_User”`  
and when you reindex your data, you have last user like “123=OldUser`r`nNewUser”. but not two different lines as you await.

but… you overcomplicate things when you try to work with different objects based on line numbers and strings. Powershell is object oriented language, try to use objects

for example you may try to use function like this [https://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91](https://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91) to import ini file and work with your data as with hashtables but not strings and then export it back as you like.\>

---

<div class="post-metadata">

**Author:** ![tony-antony](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tony-antony/32/192_2.png) [@tony-antony](https://forums.powershell.org/u/tony-antony)\
**Post date:** [December 15, 2016, 10:49am UTC](https://forums.powershell.org/t/function-wrong/7708/4 "2016-12-15T10:49:44Z")

</div>

I actually have it working now the way I want, except one thing.

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"
}
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 15, 2016, 4:41pm UTC](https://forums.powershell.org/t/function-wrong/7708/5 "2016-12-15T16:41:25Z")

</div>

You have a ReIndex\_Users function. The only place the function is called is Create\_User, which is what is “working”. If you make any change to the file, it has to be re-indexed. So, it’s preferable to call the function after your switch statement, but you can call it in each function if you want.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/function-wrong/7708/6 "2024-05-16T20:39:42Z")

</div>


