Author Posts

December 13, 2016 at 8:39 pm

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
[1] Create new user
[2] Delete a user
[3] Add user to calender
[0] Exit menu

It created 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
[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, but didn't number it, it numbered 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
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

December 13, 2016 at 10:06 pm

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.

December 14, 2016 at 6:48 am

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 to import ini file and work with your data as with hashtables but not strings and then export it back as you like.>

December 15, 2016 at 4:19 pm

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

December 15, 2016 at 10:11 pm

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.