Author Posts

December 20, 2016 at 4:42 pm

Hello,

I have an INI files which contains some email addresses. Among them, I have tantony@company.com and tony@company.com

How would I modify the below code so that it's "case sensitive"? I have it selected, but if I look up tony@company.com, it also finds tantony@company.com

$Del_User = $Read_INI | Select-String -CaseSensitive "$Del_User" -SimpleMatch

This is the current output I'm getting, notice it's showing 2 emails. I only want it to search for tony@company.com

Please select an option and press ENTER: 2
Enter the email you want to delete: tony@company.com
Deleted tantony@company.com tony@company.com

Thank you,

Tony

December 20, 2016 at 5:09 pm

Case sensitivity relates to UPPERCASE or lowercase. You have all lowercase for the e-mail addresses so that's not relevant here. The problem is that your pattern tony@company.com matches both the exact string tony@company.com and the substring tantony@company.com.

What you need to do is modify the pattern so that it only returns exact matches. Try using the start and end anchors to get an exact match "^Del_user$".

PS E:\> $del_user = 'tony@company.com'
PS E:\> 'tony@company.com' -match $del_user
True
PS E:\> 'tantony@company.com' -match $del_user
True
PS E:\> 'tantony@company.com' -match "^$del_user$"
False
PS E:\>

Get-Help about_regular_expressions for more.

December 20, 2016 at 5:17 pm

Thank you, I figured it was a regex, but I couldn't figure that out.

is this correct?

$Del_User = $Read_INI | Select-String -AllMatches "^$Del_User" -SimpleMatch

December 20, 2016 at 5:35 pm

No, it should be:

$Del_User = $Read_INI | Select-String -AllMatches "^Del_User$" -SimpleMatch

Note: this will work for a list of e-mail addresses but you'll need a more complex expression for a document that contains other data.

December 20, 2016 at 5:40 pm

When I add that, it's not showing tantony@company.com or tony@company.com

$Del_User = $Read_INI | Select-String -AllMatches "^Del_User$" -SimpleMatch

It looks like this, it should say Deleted tony@company.com

Please select an option and press ENTER: 2
Enter the email you want to delete: tony@company.com
Deleted

December 20, 2016 at 6:06 pm

I'll try again 🙂

$Del_User = $Read_INI | Select-String -AllMatches "^$Del_User$"

I owed you a dollar. -SimpleMatch doesn't use regular expressions so should be omitted.

December 20, 2016 at 7:57 pm

I'm sorry that didn't work either. I'll post my whole code here (I know it's not the best formatted), I'm having issues with line 28. This is my original line, line 29 is your code.

I have other issues also, but this is causing most of the problem.

$Continue = $null

$INI = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini'

function Add_User
{    
    $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 = $Read_INI | Where-Object {$_ -ne $User} | Set-Content $INI -Force
    
    $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]
    Write-Host "Added" $User -ForegroundColor Green
    Reindex
}

function Delete_User
{    
    $Read_INI = Get-Content $INI
    $Del_User = Read-Host "Enter the email you want to delete"        
    #$Del_User = $Read_INI | Select-String -CaseSensitive "$Del_User" -SimpleMatch
    $Del_User = $Read_INI | Select-String -AllMatches "^$Del_User$"
    #$Del_User = $Read_INI | Select-String -AllMatches "^$Del_User$"
    $Read_INI | where {$_ -ne $Del_User} | Set-Content $INI -Force  
    $Del_User = $Del_User -creplace '^[^=]*=', ''
    Write-Host "Deleted" $Del_User -ForegroundColor Red
    Reindex            
}

function Add_To_Calender
{    
    $Read_INI = Get-Content $INI
    $Calenders= $Read_INI | Select-String -CaseSensitive "MembersOf" -SimpleMatch
    $GroupCalendars= ($Read_INI | Select-String -CaseSensitive "[GroupCalendars]" -SimpleMatch).LineNumber
    $Users= ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber - 2
    $Calender_List = $Read_INI[$GroupCalendars..$Users]    
    $Calender_Index = $Read_INI | Select-String -CaseSensitive "[MembersOf" -SimpleMatch    
    
    $Calender_List
    $Get_Calender = Read-Host "Which calender number do you want to add user to?"     
    $Get_User = Read-Host "Which email do you want to add to" $Calender_List[$Get_Calender-1]
    
    $Calender_Index | Select-Object -Index 2

    Write-Host "Added" $Get_User "to" $Calender_List[$Get_Calender-1] -ForegroundColor Green
}

function Reindex
{    
    $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]
    $Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber - 1
    
    For($Index=0; $Index -lt $Users.length; $Index++)
        {
            $Index_Plus_One = $Index + 1;            
            $Index_Minus_One = $Index - 1;
            
            if(($Users[$Index].Contains("@") -eq $false))
                {               
                    $Read_INI = $Read_INI | Where-Object {$_ -ne $Users[$Index]} | Set-Content $INI -Force
                    $Users                 
                }

            if(($Users[$Index]).Contains("=") -eq $true)
                {
                    $Email = ($Users[$Index]) -creplace '^[^=]*=', ''     
                }
            else
                {
                    $Email = $User
                }  

            #$Re_Index = "$Index_Plus_One" + "=" + "$Email"         

            #$Read_INI = $Read_INI | Where-Object {$_ -ne $Users[$Index]}
            #$Read_INI[$Users_Line_Num] += "`r`n$Re_Index"
            #$Read_INI | Set-Content $INI -Force
        } 
            $Re_Index = "$Index_Plus_One" + "=" + "$Email"         
            $Read_INI = $Read_INI | Where-Object {$_ -ne $Users[$Index]}
            #$Read_INI[$Membersof_Line_Num] += "`r`n$Re_Index"
            $Read_INI[$Membersof_Line_Num] = $Re_Index
            $Read_INI | Set-Content $INI -Force
}

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

While($Continue -ne 0)
{
    switch($Continue)
    {
        1{Add_User}
        2{Delete_User}
        3{Add_To_Calender}
        default {"Please choose correct manu option!"}
    }

    Write-Host "[1]`tAdd new user"
    Write-Host "[2]`tDelete user"
    Write-Host "[3]`tAdd user to calender group"
    Write-Host "[0]`tExit menu"

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

Thank you,

December 20, 2016 at 11:00 pm

Can you post example data for the ini file as well please. The line I posted in my last post works fine when tested with a basic list of e-mail addresses. I'm guessing your INI file is more complex?

December 21, 2016 at 7:17 am

btw, if you email list contains both
jim.morrison@mail.com
jim_morrison@mail.com

you delete both jims if enter 'jim.morrison@mail.com'

'.' – regex symbol

December 21, 2016 at 1:47 pm

Here's a list of users in my test INI file

3=oreynolds@company.com
4=JSTACK@company.com
5=Dwu@company.com
6=dsmith@company.com
1026=rglass@company.com
1027=rheacock@company.com
1028=RJohnson@company.com
1029=rkahrig@company.com
1030=rkaye@company.com
1066=sbarnby@company.com
1067=sbenoit@company.com
1068=sbraxton@company.com
1113=slindemon@company.com
1114=sloeber@company.com
1122=snoel@company.com
1442=gps@company.com

The real file have 1400+ people, and I had it going through the For Loop, and it was taking about 2 minutes to create a new person because it's reading and writing it 1400 times.

What I'm trying to do is put the list in order so that's its'

1=oreynolds@company.com
2=JSTACK@company.com
3=Dwu@company.com
4=dsmith@company.com
5=rglass@company.com
6=rheacock@company.com
7=RJohnson@company.com
8=rkahrig@company.com
9=rkaye@company.com
10=sbarnby@company.com
11=sbenoit@company.com
12=sbraxton@company.com
13=slindemon@company.com
14=sloeber@company.com
15=snoel@company.com
16=gps@company.com

and so on. Same thing if I delete a person, delete that person and reorder.

December 21, 2016 at 2:06 pm

OK, so you have some digits and an equals sign before the e-mail address. This should work for the search:

$Del_User = $Read_INI | Select-String -AllMatches "^\d+=$Del_User$"

The plain English regex is 'search for a line starting with (^) one or more digits (\d+) followed by an equals sign, followed by the e-mail address'.

I added our previous test user to the file so the output at this point would be, for example, 7=tony@company.com

If you just want the e-mail address, split on the = and select the second element:

$Del_User = (($Read_INI | Select-String -AllMatches "^\d+=$Del_User$") -split '=')[1]

December 21, 2016 at 2:10 pm

Thank you, I'll try that.

December 21, 2016 at 2:54 pm

Thank you,

I tested it with tantony and tony and it works

$Del_User = $Read_INI | Select-String -AllMatches "^\d+=$Del_User$"