Powershell script help request - Powershell menu

So the premise here is that I want to allow certain users to modify the distribution groups they are the Manager of (ManagedBy).  And since they're not that adept I figured a menu would be easiest.

So what I would like to happen is, they can choose !. which returns a listing of the groups they manage, but then I want it to return to the menu after it processes the command that 1. does.  Currently the script terminates.

Also I need a way to allow the user to input a username when they choose option 2 or 3


$username = Read-host "Enter username"
$user = Get-ADUser $username

[int]$xMenuChoiceA = 0
while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 4 ){

Write-host "1. List groups I manage"
Write-host "2. Add members to a group"
Write-host "3. Delete members from a group"
Write-host "4. Quit and exit"

$xMenuChoiceA = read-host "Please enter an option 1 to 4..."

}

Switch( $xMenuChoiceA ){
  1{Get-ADGroup -Filter "managedby -eq '$($user.DistinguishedName)'" |fl samaccountname}
  2{$newuser = read-host = "Enter username to add"
    $group = read-host = "Enter the group name you wish to modify"
   add-adgroupmember -identity $group -members $newuser
   get-adgroupmember -identity $group |fl name }
  3{$olduser = read-host = "Enter the username you wish to remove"
    $group1 = read-host = "Enter the group name"
   remove-adgroupmember -identity $group1 -members $olduser -confirm:$false }
default{<#run a default action or call a function here #>}
}

Open in new window

LVL 14
Ben HartAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
create a function for each item to process.
function list_groups 
    {
    Get-ADGroup -Filter "managedby -eq '$($user.DistinguishedName)'" |fl samaccountname
    }
function add_member 
    {
    $newuser = read-host = "Enter username to add"
    $group = read-host = "Enter the group name you wish to modify"
   add-adgroupmember -identity $group -members $newuser
   get-adgroupmember -identity $group |fl name 
    }
function remove_member
    {
    $olduser = read-host = "Enter the username you wish to remove"
    $group1 = read-host = "Enter the group name"
   remove-adgroupmember -identity $group1 -members $olduser -confirm:$false 
    }

$username = Read-host "Enter username"
$user = Get-ADUser $username

[int]$xMenuChoiceA = 0
do {

Write-host "1. List groups I manage"
Write-host "2. Add members to a group"
Write-host "3. Delete members from a group"
Write-host "4. Quit and exit"

$xMenuChoiceA = read-host "Please enter an option 1 to 4..."

Switch( $xMenuChoiceA ){
  1
    { 
    get-groups
    }
  2
    { 
    add_member
    }
  
  3
    {
    remove_member
    }
default
    {
    write-host "Valid responses are 1,2,3,4"
    }
    }
}while  ( $xMenuChoiceA -le 3 )

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ben HartAuthor Commented:
Hmm ran into a syntax issue i think:

Enter your username: bhart
1. List groups I manage
2. Add members to a group
3. Delete members from a group
4. Quit and exit
Please enter an option 1 to 4...: 1
list-groups : The term 'list-groups' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:35 char:5
+     list-groups
+     ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (list-groups:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Ben HartAuthor Commented:
Wait nevermind.  I got it.  line 35 needed to be list_groups not list-groups
Ben HartAuthor Commented:
BAM thanks man.  Worked perfectly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.