Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powershell script with menu\console - struggling with the code

Status
Not open for further replies.

shabbarankers

Technical User
Jan 7, 2011
19
GB
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:
$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 [URL unfurl="true"]https://outlook.office365.com/powershell-liveid/[/URL] -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
    }
    }
 
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.
 
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:
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition

Code:
$PSScriptRoot+"\UserCreation.ps1"

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

Thanks
 
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] = "[b][COLOR=#EF2929].\[/color][/b]Menu_Email.ps1"

$options[2,0] = "Office 365 Submenu"
$options[2,1] = $submenu_color
$options[2,2] = "[b][COLOR=#EF2929].\[/color][/b]Menu_O365.ps1"

$options[3,0] = "Submenu 3"
$options[3,1] = $submenu_color
$options[3,2] = "[b][COLOR=#EF2929].\[/color][/b]Submenu_3.ps1"


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

$options[5,0] = "Script 2"
$options[5,1] = $admin_color
$options[5,2] = "[b][COLOR=#EF2929].\[/color][/b]Script_2.ps1"

$options[6,0] = "Script 3"
$options[6,1] = $admin_color
$options[6,2] = "[b][COLOR=#EF2929].\[/color][/b]Script_3.ps1"

$options[7,0] = "Script 4"
$options[7,1] = $user_color
$options[7,2] = "[b][COLOR=#EF2929].\[/color][/b]Script_4.ps1"

$options[8,0] = "Script 5"
$options[8,1] = $user_color
$options[8,2] = "[b][COLOR=#EF2929].\[/color][/b]Script_5.ps1"

$options[9,0] = "Script 6"
$options[9,1] = $admin_color
$options[9,2] = "[b][COLOR=#EF2929].\[/color][/b]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]
				 [COLOR=#EF2929][b]invoke-expression $script_name[/b][/color]
				}
			}
		}	
	 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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top