Christian Hans
asked on
Help building a Powershell Menu with options
How would I create an interactive menu when opening ISE?
I've created a new ISE profile under C:\Users\heyitsme\Document s\WindowsP owerShell\ Microsoft. PowerShell ISE_profil e.ps1
I am basically wanting to get prompted for 3 options...
Option 1 - Connect to Exchange Online (which points to another PS1 file that contains the Exchange Online connection info c:\scripts\ExOL.ps1)
Option 2 - Connect to Exchange On-Prem (which points to another PS1 file that contains the Exchange On-Prem connection info c:\scripts\ExOP.ps1)
Option 3 - Quit
Does anyone have any pointers on how to set something like this up?
Im trying to put together a bunch of things but am not having any luck...
Example: https://www.petri.com/building-a-powershell-console-menu-revisited-part-1
I've created a new ISE profile under C:\Users\heyitsme\Document
I am basically wanting to get prompted for 3 options...
Option 1 - Connect to Exchange Online (which points to another PS1 file that contains the Exchange Online connection info c:\scripts\ExOL.ps1)
Option 2 - Connect to Exchange On-Prem (which points to another PS1 file that contains the Exchange On-Prem connection info c:\scripts\ExOP.ps1)
Option 3 - Quit
Does anyone have any pointers on how to set something like this up?
Im trying to put together a bunch of things but am not having any luck...
Example: https://www.petri.com/building-a-powershell-console-menu-revisited-part-1
Here is something I was playing around with. Perhaps this can help. Can't remember where I got it from or whether or not I modified it:
#Areyousure function. Allows user to select y or n when asked to exit. Y exits and N returns to main menu.
function areyousure {$areyousure = read-host "Are you sure you want to exit? (y/n)"
if ($areyousure -eq "y"){exit}
if ($areyousure -eq "n"){mainmenu}
else {write-host -foregroundcolor red "Invalid Selection"
areyousure
}
}
#Mainmenu function. Contains the screen output for the menu and waits for and handles user input.
function mainmenu{
cls
echo "---------------------------------------------------------"
echo ""
echo ""
echo " 1. Open Notepad"
echo " 2. Open Calculator"
echo " 3. Exit"
echo ""
echo ""
echo "---------------------------------------------------------"
$answer = read-host "Please Make a Selection"
if ($answer -eq 1){notepad}
elseif ($answer -eq 2){calc}
elseif ($answer -eq 3){areyousure}
else {write-host -ForegroundColor red "Invalid Selection"
sleep 5
mainmenu
}
}
mainmenu
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Another approach would be to add entries to the Add-Ons menu structure so that you can simply select the external scripts to run from the menu. This can be achieved by adding some code to the ISE profile:
While the structure for adding menus implies that you can use multiple menus, you can only use the Add-Ons.
function Add-ISEMenuItem([String]$MenuName,[String]$Name,[ScriptBlock]$Script,[String]$Shortcut)
{
try{
if ( [String]::IsNullOrWhiteSpace($Shortcut ) )
{
$Menus[$MenuName].Submenus.Add($Name, $Script,$null) | Out-Null
}
else
{
$Menus[$MenuName].Submenus.Add($Name, $Script, $Shortcut) | Out-Null
}
}
catch
{
Write-Warning $PSItem
}
}
Add-ISEMenuItem -MenuName "AddOns" -Name "Online Exchange" -Script { c:\scripts\ExOL.ps1 } -Shortcut 'Alt+Shift+L'
Add-ISEMenuItem -MenuName "AddOns" -Name "On prem Exchange" -Script { c:\scripts\ExOP.ps1 } -Shortcut 'Alt+Shift+P'
While the structure for adding menus implies that you can use multiple menus, you can only use the Add-Ons.
Open in new window
connect-office365.ps1Open in new window
you will need the modules use install-module modulename