INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Powershell script with menu\console - struggling with the code

Powershell script with menu\console - struggling with the code

Powershell script with menu\console - struggling with the code

(OP)
Hi,
Im trying to build a menu system within powershell to do various functions and Ive hit a bit of a stumbling block. I was wondering if someone could assist me with it and say where Im going wrong please?

CODE --> powershell

$MainMenu=
wrte-host  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
write-host          "Office 365 and Exchange Online Management"                               
write-host                                                            
write-host          "Press 1: To Manage Office 365 Functions"              
write-host          "Press 2: To Manage Exchange Online Functions"         
write-host          "Press Q: To Quit"                                     
write-host
write-host
write-host ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
$Selection = Read-Host $MainMenu

Switch ($Selection) {
"1" {
    Write-Host "Connecting To Office 365" -ForegroundColor Green
    #Needs thinking about - Tasks etc
    }
"2" {
    Write-Host "Connecting To Exchange Online" -ForegroundColor Green
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    Import-PSSession $Session
    cls
    $ExchangeMenu=
    wrte-host  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    write-host          "Exchange Online Management"                                 
    write-host                                                            
    write-host          "Press 1: To Do Stuff"              
    write-host          "Press 2: To Do More Stuff"         
    write-host          "Press Q To Exit"                                     
    write-host
    write-host
    write-host ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
    }
"Q" {
    Write-Host "Exiting Console" -ForegroundColor Green
    }
    } 

RE: Powershell script with menu\console - struggling with the code

I've done something similar. I do mine with multiple scripts. We're in a hybrid environment, so I've tried to remove anything not in Office 365. I hope it doesn't cause any errors.

I don't know if this is the easiest, but it works.
Although the main menu and sub-menus are almost identical I'll include both.

Main Script

CODE -->

## Main.ps1
##	This script creates a master list of completed PowerShell scripts.
##	It is color-coded for access.  Dark Green is for full access (admins) and light green is for limited
##	access (SAs and Help Desk)

## Options array
##	[0]	= Title displayed on the menu
##	[1]	= Display color for the title
##	[2]	= Name of the script
#########################################################################################################


##-----------------------------------------------------------------------------------------------------##
##  MAIN SCRIPT  
##-----------------------------------------------------------------------------------------------------##

## Initialize variables
$user_color = "Green"
$admin_color = "DarkCyan"
$quit_color = "Gray"
$submenu_color = "Cyan"

$script_name = ""
$run_script = $TRUE
$item_size = 10
$options_size = 3


## Create Array of scripts and associated access color
$options = New-Object 'object[,]' $item_size,$options_size
$options[0,0] = "Quit"
$options[0,1] = $quit_color
$options[0,2] = $null

# Sub-menus
$options[1,0] = "Exchange Online Submenu"
$options[1,1] = $submenu_color
$options[1,2] = "Menu_Email.ps1"

$options[2,0] = "Office 365 Submenu"
$options[2,1] = $submenu_color
$options[2,2] = "Menu_O365.ps1"

$options[3,0] = "Submenu 3"
$options[3,1] = $submenu_color
$options[3,2] = "Submenu_3.ps1"


# Scripts not under a sub-menu
$options[4,0] = "Script 1"
$options[4,1] = $admin_color
$options[4,2] = "script_1.ps1"

$options[5,0] = "Script 2"
$options[5,1] = $admin_color
$options[5,2] = "Script_2.ps1"

$options[6,0] = "Script 3"
$options[6,1] = $admin_color
$options[6,2] = "Script_3.ps1"

$options[7,0] = "Script 4"
$options[7,1] = $user_color
$options[7,2] = "Script_4.ps1"

$options[8,0] = "Script 5"
$options[8,1] = $user_color
$options[8,2] = "Script_5.ps1"

$options[9,0] = "Script 6"
$options[9,1] = $admin_color
$options[9,2] = "Script_6.ps1"

## Yes-No-Prompt variables
$yes = new-object System.Management.Automation.Host.ChoiceDescription "&Yes"
$no = new-object System.Management.Automation.Host.ChoiceDescription "&No"
$ny_choices = [System.Management.Automation.Host.ChoiceDescription[]] ($no, $yes)
$ny_title = ""

#write-host "";""
$separator = 4

while ($run_script)
	{
	 write-host "";""
	 write-host "*******************************************"
	 write-host "                 Main Menu"
	 write-host "*******************************************"
	 write-host "";""

	 ## Display menu list
	 for ($list=1; $list -lt $item_size; $list++)
	    {
		  if ($list -eq $separator)
			{write-host ""}
		  $line = "["+$list+"] " + $options[$list,0]
		  $color = $options[$list,1]
		  write-host $line -foregroundcolor $color
		}
	 ## Display Quit option
	 write-host ""
	 write-host "[0] Quit" -foregroundcolor "Gray"
	 write-host "";""
	 [int]$script_to_run = Read-Host "Enter the number of the script you want to run "
	 
	 ## Validate selection
     if ($script_to_run -ge 0 -And $script_to_run -lt $item_size)
	    {
		 $script_descr = $options[$script_to_run,0]
		 if ($script_to_run -eq 0)
		    {
			 $run_script = $FALSE
			 write-host "Quitting."
			}
	    }
	 else
	    {
		 write-host "Valid selection not chosen!"
		 $run_script = $FALSE
		 $null
		}
	
	 ## Verify and run script
	 if ($run_script)
		{
		 write-host ""
		 ## Prompt for script confirmation
		 $verify_message = "You have chosen to $script_descr.  Is this the script you want to run?"
		 $verity_answer = $host.ui.PromptForChoice($ny_title,$verify_message,$ny_choices,1)
		 switch ($verity_answer)
			{
			 ## "No" code - do not run script
			 0  {$null; break} 
			 ## "Yes" code - run script
			 1	{
				 $script_name = $options[$script_to_run,2]
				 invoke-expression $script_name
				}
			}
		}	
	 write-host "";""
	 ## Request if user wants to run another script
	 $rerun_message = "Do you want to run another script from the Main Menu?"
	 $rerun_answer = $host.ui.PromptForChoice($ny_title,$rerun_message,$ny_choices,1)
	 if ($rerun_answer)
		{
		 write-host "";"";""
		 [int]$script_to_run = ""
		 $run_script = $TRUE
		}
	 else
		{$run_script = $FALSE}
	}
write-host "";""
write-host "End of Main.ps1 Script!" -foregroundcolor "yellow"
write-host ""
#End of Main.ps1 Script 

Sub-Menu

CODE -->

## Menu_Email.ps1
## It is color-coded for access.  Dark Green is for full access (write/modify) and light green is for
## read access.

## Options array
##	[0]	= Title displayed on the menu
##	[1]	= Display color for the title
##	[2]	= Name of the script (if email, this is the exchange version


#########################################################################################################


##-----------------------------------------------------------------------------------------------------##
##  MAIN SCRIPT  
##-----------------------------------------------------------------------------------------------------##

Clear-host

## Initialize variables
$user_color = "Green"
$admin_color = "DarkCyan"
$quit_color = "Gray"

$script_name = ""
$run_script = $TRUE
$exit_to_main = $FALSE
$item_size = 6
$options_size = 3

write-host ""
write-host "********************************************"
write-host "      Exchange Online Scripts"
write-host "********************************************"
write-host ""

## Create Array of scripts and associated access color
$options = New-Object 'object[,]' $item_size,$options_size
$options[0,0] = "Quit"
$options[0,1] = $quit_color
$options[0,2] = $null

$options[1,0] = "Exchange Script 1"
$options[1,1] = $admin_color
$options[1,2] = "ex_script_1.ps1"

$options[2,0] = "Exchange Script 2"
$options[2,1] = $user_color
$options[2,2] = "ex_script_2.ps1"

$options[3,0] = "Exchange Script 3"
$options[3,1] = $admin_color
$options[3,2] = "ex_script_3.ps1"

$options[4,0] = "Exchange Script 4"
$options[4,1] = $admin_color
$options[4,2] = "ex_script_4.ps1"

$options[5,0] = "Exchange Script 5"
$options[5,1] = $user_color
$options[5,2] = "ex_script_5.ps1"


## Yes-No-Prompt variables
$yes = new-object System.Management.Automation.Host.ChoiceDescription "&Yes"
$no = new-object System.Management.Automation.Host.ChoiceDescription "&No"
$ny_choices = [System.Management.Automation.Host.ChoiceDescription[]] ($no, $yes)
$ny_title = ""

while ($run_script)
	{
	 write-host ""
	 ## Display menu list
	 for ($list=1; $list -lt $item_size; $list++)
	    {
		  $line = "["+$list+"] " + $options[$list,0]
		  $color = $options[$list,1]
		  write-host $line -foregroundcolor $color
		}
	 ## Display Quit option
	 write-host ""
	 write-host "[0] Quit" -foregroundcolor "Gray"
	 write-host "";""

	 [int]$script_to_run = Read-Host "Enter the number of the script you want to run "
	 
	 ## Validate selection
     if ($script_to_run -ge 0 -And $script_to_run -lt $item_size)
	    {
		 $script_descr = $options[$script_to_run,0]
		 if ($script_to_run -eq 0)
		    {
			 $run_script = $FALSE
			 write-host "Quitting."
			}
	    }
	 else
	    {
		 write-host "Valid selection not chosen!"
		 $run_script = $FALSE
		 $null
		}
	
	 ## Verify and run script
	 if ($run_script)
		{
		 write-host ""
		 ## Prompt for script confirmation
		 $verify_message = "You have chosen to $script_descr.  Is this the script you want to run?"
		 $verity_answer = $host.ui.PromptForChoice($ny_title,$verify_message,$ny_choices,1)
		 switch ($verity_answer)
			{
			 ## "No" code - do not run script
			 0  {$null; break} 
			 ## "Yes" code - run script
			 1	{
				 $script_name = $options[$script_to_run,2]
				 invoke-expression $script_name
				}
			}
		}	
	 write-host "";""
	 ## Request if user wants to run another script
	 $rerun_message = "Do you want to run another script from this group?"
	 $rerun_answer = $host.ui.PromptForChoice($ny_title,$rerun_message,$ny_choices,0)

	 if ($rerun_answer)
		{
		 write-host "";"";""
		 [int]$script_to_run = ""
		 $run_script = $TRUE
		}
	 else
		{$run_script = $FALSE}
	}

write-host "";""
write-host "End of Menu_Email.ps1 Script!" -foregroundcolor "yellow"
write-host "";""
#End of Menu_Email.ps1 Script 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Powershell script with menu\console - struggling with the code

(OP)
Hi,
Im not sure if this is what Im after - but I am interested to know how you call 1 script from another? I have tried the following:

CODE --> powershell

$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition 

CODE --> powershell

$PSScriptRoot+"\UserCreation.ps1" 

Both scripts are in the same folder but it doesn't call the second script?

Thanks

RE: Powershell script with menu\console - struggling with the code

Sorry about that. We don't have our subscripts in the same folder as the main script. So when I removed our pathing, I didn't add ".\" (in red in menu list) to specify the scripts are in the current folder. You'll have to add it to the second script, as well, or add the path if those scripts are in different folder(s).

Here is the updated main script. This should work for you. Again, my apologies for not testing this better before posting.

But to answer your question, it's the "invoke-expression $script_name" (in red in the script) that calls the second script.

CODE -->

## Main.ps1
##	This script creates a master list of completed PowerShell scripts.
##	It is color-coded for access.  Dark Green is for full access (admins) and light green is for limited
##	access (SAs and Help Desk)

## Options array
##	[0]	= Title displayed on the menu
##	[1]	= Display color for the title
##	[2]	= Name of the script
#########################################################################################################


##-----------------------------------------------------------------------------------------------------##
##  MAIN SCRIPT  
##-----------------------------------------------------------------------------------------------------##

## Initialize variables
$user_color = "Green"
$admin_color = "DarkCyan"
$quit_color = "Gray"
$submenu_color = "Cyan"

$script_name = ""
$run_script = $TRUE
$item_size = 10
$options_size = 3


## Create Array of scripts and associated access color
$options = New-Object 'object[,]' $item_size,$options_size
$options[0,0] = "Quit"
$options[0,1] = $quit_color
$options[0,2] = $null

# Sub-menus
$options[1,0] = "Exchange Online Submenu"
$options[1,1] = $submenu_color
$options[1,2] = ".\Menu_Email.ps1"

$options[2,0] = "Office 365 Submenu"
$options[2,1] = $submenu_color
$options[2,2] = ".\Menu_O365.ps1"

$options[3,0] = "Submenu 3"
$options[3,1] = $submenu_color
$options[3,2] = ".\Submenu_3.ps1"


# Scripts not under a sub-menu
$options[4,0] = "Script 1"
$options[4,1] = $admin_color
$options[4,2] = ".\script_1.ps1"

$options[5,0] = "Script 2"
$options[5,1] = $admin_color
$options[5,2] = ".\Script_2.ps1"

$options[6,0] = "Script 3"
$options[6,1] = $admin_color
$options[6,2] = ".\Script_3.ps1"

$options[7,0] = "Script 4"
$options[7,1] = $user_color
$options[7,2] = ".\Script_4.ps1"

$options[8,0] = "Script 5"
$options[8,1] = $user_color
$options[8,2] = ".\Script_5.ps1"

$options[9,0] = "Script 6"
$options[9,1] = $admin_color
$options[9,2] = ".\Script_6.ps1"

## Yes-No-Prompt variables
$yes = new-object System.Management.Automation.Host.ChoiceDescription "&Yes"
$no = new-object System.Management.Automation.Host.ChoiceDescription "&No"
$ny_choices = [System.Management.Automation.Host.ChoiceDescription[]] ($no, $yes)
$ny_title = ""

#write-host "";""
$separator = 4

while ($run_script)
	{
	 write-host "";""
	 write-host "*******************************************"
	 write-host "                 Main Menu"
	 write-host "*******************************************"
	 write-host "";""

	 ## Display menu list
	 for ($list=1; $list -lt $item_size; $list++)
	    {
		  if ($list -eq $separator)
			{write-host ""}
		  $line = "["+$list+"] " + $options[$list,0]
		  $color = $options[$list,1]
		  write-host $line -foregroundcolor $color
		}
	 ## Display Quit option
	 write-host ""
	 write-host "[0] Quit" -foregroundcolor "Gray"
	 write-host "";""
	 [int]$script_to_run = Read-Host "Enter the number of the script you want to run "
	 
	 ## Validate selection
     if ($script_to_run -ge 0 -And $script_to_run -lt $item_size)
	    {
		 $script_descr = $options[$script_to_run,0]
		 if ($script_to_run -eq 0)
		    {
			 $run_script = $FALSE
			 write-host "Quitting."
			}
	    }
	 else
	    {
		 write-host "Valid selection not chosen!"
		 $run_script = $FALSE
		 $null
		}
	
	 ## Verify and run script
	 if ($run_script)
		{
		 write-host ""
		 ## Prompt for script confirmation
		 $verify_message = "You have chosen to $script_descr.  Is this the script you want to run?"
		 $verity_answer = $host.ui.PromptForChoice($ny_title,$verify_message,$ny_choices,1)
		 switch ($verity_answer)
			{
			 ## "No" code - do not run script
			 0  {$null; break} 
			 ## "Yes" code - run script
			 1	{
				 $script_name = $options[$script_to_run,2]
				 invoke-expression $script_name
				}
			}
		}	
	 write-host "";""
	 ## Request if user wants to run another script
	 $rerun_message = "Do you want to run another script from the Main Menu?"
	 $rerun_answer = $host.ui.PromptForChoice($ny_title,$rerun_message,$ny_choices,1)
	 if ($rerun_answer)
		{
		 write-host "";"";""
		 [int]$script_to_run = ""
		 $run_script = $TRUE
		}
	 else
		{$run_script = $FALSE}
	}
write-host "";""
write-host "End of Main.ps1 Script!" -foregroundcolor "yellow"
write-host ""
#End of Main.ps1 Script 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close