Author Posts

December 12, 2016 at 4:16 pm

Hello,

I'm writing a script where I can enter an email address and it adds it to a list, and also choose an option to remove a user.

The adding a user is working fine, but when I delete someone, I have to run the script 2-3 times to remove them from the text file. It seems there's a delay.

$INI_Location = "\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini"
$Read_INI = Get-Content $INI_Location

$MenuChoice = $null

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

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

While($MenuChoice -ne 'q')
{
    switch ($MenuChoice)
    {
        1 {Create_User}
        2 {Delete_User}
    }
    
    Write-Host "[1]`tCreate new user"
    Write-Host "[2]`tDelete a user"
    Write-Host "[Q]`tExit menu"

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

Thank you,

Tony

December 12, 2016 at 4:28 pm

It worked when I tired it using it all locally. I'd add a this to line 30

$Read_INI = Get-Content $INI_Location

or make it an advanced function.

or maybe add start-sleep in there to it has a delay.

December 12, 2016 at 4:32 pm

I think it's a delay issue also because it's working when I wait a while before running the delete command, but why would the Create_User functions works fine without issues? I can create as many users without any delay.

Should I be using

$Read_INI | where {$_ -ne $Del_User} | Set-Content $INI_Location

for Delete_User

December 12, 2016 at 4:52 pm

The first issue I see if that your reading of the INI file happens outside of your loop, so changes would be unknown unless you relaunched your script.

While($MenuChoice -ne 'q')
{

    $Read_INI = Get-Content $INI_Location

    switch ($MenuChoice)
    {
        1 {Create_User}
        2 {Delete_User}
    }
    
    Write-Host "[1]`tCreate new user"
    Write-Host "[2]`tDelete a user"
    Write-Host "[Q]`tExit menu"

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

or preferably in the function scope versus the script\global scope:

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

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

I would also recommend using parameters in your functions and naming your functions like a Powershell function, like:

function Set-CGUser {
    param (
        $IniPath,
        [ValidateSet('New', 'Delete')]
        $Type 
    )
    begin{
        $Read_INI = Get-Content $IniPath
    }
    process {    
        switch ($Type) {
            "New" {
                #What if person already exists? :)
                $New_User = Read-Host "Enter new employee's account name"
                $Read_INI[$Calender_Groups] += "`r`n$New_User"
                $Read_INI | Set-Content $INI_Location
            }
            "Delete" {
                $Del_User = Read-Host "Enter the email you want to delete"
                $Read_INI | where {$_ -ne $Del_User} | Out-File $INI_Location -Force
            }
    }
    end{}
}

Both of your functions are performing a Set, so they can be consolidated into a single function and leverage switch to perform different types of Set.

December 12, 2016 at 5:08 pm

Thank you, I went with the first option to put everything in the while loop, it's working now.