Confused about text files

Hello,

I need help, I know I asked this before, but it doesn’t make any sense.

I tried putting everything inside the while loop, but I get the same result, so I have everything outside the while loop now.

I’m sure its something simple, but I can’t figure it out.

Issue 1: Line 14, it displays the users with the one@one.com I added, this is good, but when I do a .count, it gives me 8 instead of 9 which is the correct count.

1=mkelley@company.com
2=jbruce@company.com
3=oreynolds@company.com
1438=alippard@company.com
1439=klaw@company.com
1440=dmarsh@company.com
1441=jonathanj@company.com
1442=aralls@company.com
one@one.com

$INI = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini'
$Read_INI = Get-Content $INI
$Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber
$Membersof_Line_Num = ($Read_INI | Select-String -CaseSensitive "[MembersOf-1]" -SimpleMatch).LineNumber - 2
$Users = $Read_INI[$Users_Line_Num..$Membersof_Line_Num]

$Continue = $null

function Add_User
{
    $User = Read-Host "Enter the new email address to add"
    $Read_INI[$Membersof_Line_Num] += "`r`n$User"
    $Read_INI | Set-Content $INI
    $Read_INI[$Users_Line_Num..$Membersof_Line_Num]
}

While($Continue -ne 0)
{
    switch($Continue)
    {
        1{Add_User}
    }

    Write-Host "[1]`t`tAdd new user"
    Write-Host "[0]`t`tExit menu"

    $Continue = Read-Host "`nPlease select an option and press ENTER"
}

Issue 2:

Line 14, I have $Users, which is $Read_INI[$Users_Line_Num…$Membersof_Line_Num], but it doesn’t show one@one.com, and when I do a .count, it gives me 8 instead of 9 which is the correct count.

1=mkelley@company.com
2=jbruce@company.com
3=oreynolds@company.com
1438=alippard@company.com
1439=klaw@company.com
1440=dmarsh@company.com
1441=jonathanj@company.com
1442=aralls@company.com

$INI = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini'
$Read_INI = Get-Content $INI
$Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber
$Membersof_Line_Num = ($Read_INI | Select-String -CaseSensitive "[MembersOf-1]" -SimpleMatch).LineNumber - 2
$Users = $Read_INI[$Users_Line_Num..$Membersof_Line_Num]

$Continue = $null

function Add_User
{
    $User = Read-Host "Enter the new email address to add"
    $Read_INI[$Membersof_Line_Num] += "`r`n$User"
    $Read_INI | Set-Content $INI
    $Users
}

While($Continue -ne 0)
{
    switch($Continue)
    {
        1{Add_User}
    }

    Write-Host "[1]`t`tAdd new user"
    Write-Host "[0]`t`tExit menu"

    $Continue = Read-Host "`nPlease select an option and press ENTER"
}

Thank you,

Tony

You need to read the file again after you make your update
try this

$Continue = $null

function Add_User
{
    $INI = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini'
    $Read_INI = Get-Content $INI
    $Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber
    $Membersof_Line_Num = ($Read_INI | Select-String -CaseSensitive "[MembersOf-1]" -SimpleMatch).LineNumber - 2
    $Users = $Read_INI[$Users_Line_Num..$Membersof_Line_Num]
    $User = Read-Host "Enter the new email address to add"
    $Read_INI[$Membersof_Line_Num] += "`r`n$User"
    $Read_INI | Set-Content $INI
    $Read_INI = Get-Content $INI
    $Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber
    $Membersof_Line_Num = ($Read_INI | Select-String -CaseSensitive "[MembersOf-1]" -SimpleMatch).LineNumber - 2
    $Users = $Read_INI[$Users_Line_Num..$Membersof_Line_Num]
    $Read_INI[$Users_Line_Num..$Membersof_Line_Num]
}

While($Continue -ne 0)
{
    switch($Continue)
    {
        1{Add_User}
    }

    Write-Host "[1]`t`tAdd new user"
    Write-Host "[0]`t`tExit menu"

    $Continue = Read-Host "`nPlease select an option and press ENTER"
}

Thank you Jonathan I’ll try that, one more question.

The reason I set my variables outside of loop is because I have to use the same variables again later such as when I delete a user.

Is there a way to make the variables “public”? Because right now, the variables can only be used within the Add_User function correct?

You can set some of them like the ini path out side of the function like you had. A better option would be to use a function to read the file and return the properties that you want so you reduce the code repetition