Saturday, February 6, 2021

Create PowerShell Multi-Menu Tool: Part I

 

Scenario

You want to create a PowerShell tool that allows users to select functions from a menu. Additionally, you want to have multiple menus.

Big picture - We create a folder structure that allows us to run a single script to launch or menus, update our modules, and set our global variables.

File Structure

To begin, I recommend setting up a file structure like the following:

Configs - Files that your scripts / modules look to in order to know what to do.
Logs - A place for you to put created log files
Modules - A place for your modules to go
Reports - A place for created reports
Third Party Tools - A place for tools that add functionality to your modules / scripts. For example, I put the SysInternals tools in this folder so that I can reference them in my scripts.

Start Tool


Below is the only script I keep in the root of my folder - StartTool.ps1. It's the one I run to get the tool going. The contents of that file look like this:

<#
Type: Script
Name: Start Tool (Launches Powershell Tool)
Description: This script starts creates global variables, installs modules in modules folder and starts main menu
Author: Matt Graham
Date:
Version: 1.0.0.0
#>


# Set Base Directory Path
$BaseDir = Split-Path -Parent $MyInvocation.MyCommand.Definition


# Set Global Variables
$ConfigPath = "$BaseDir\Configs"
$LogPath = "$BaseDir\Logs"
$ModulePath = "$BaseDir\Modules"
$ReportPath = "$BaseDir\Reports"
$ThirdPartyPath = "$BaseDir\ThirdPartyTools"


# Import Modules
$Modules = Get-ChildItem "$ModulePath" -Filter *.psm1
foreach ($Module in $Modules.Name) {Import-Module "$ModulePath\$Module" -Force}


# Launch Main Menu
MainMenu


As you can see, I want to be able to copy the parent folder of these folders anywhere and run this tool wherever it's run from. So I set the basepath and then paths to the folders relative to that basepath. Then I import any module with a .psm1 extension so it's ready to use when I run this script. Finally I launch the MainMenu - which we haven't created yet. It is a function that is called.

NOTE: I create all my menus as modules so that anytime I update them all I have to do is run the above script and all my menus are updated.


Main Menu Module


Now let's create the Main Menu so that when we launch the above script, it launches a menu. This will be saved in the "Modules" folder and will have a .psm1 extension rather than a .ps1 extension.

<#
    Type: Module
    Name: Main Menu
    Description: This function builds the main menu for our tool.
    Author: Matt Graham
    Date:
    Version: 1.0.0.0
#>

# Create Main Menu Function
    function MainMenu
        {
            # Build Menu
            param([string]$Title="Main Menu")
            do
                {
                    Start-Sleep -Seconds 1
                    Clear-Host

                    Write-Host ""
                    Write-Host "======================$Title============================" -ForegroundColor Cyan
                    Write-Host " 1. Get Information"
                    Write-Host " 2. Change Settings"
                    Write-Host " 3. Run Diagnostics"
                    Write-Host " 4. Create Reports" 
                    Write-Host "======================$Title============================" -ForegroundColor Cyan
                    Write-Host ""
                    $Selection = Read-Host "Select option and press enter"


            # Switch for Selecting functions (Menus)
                Switch ($Selection)
                    {
                        '1' {GetInfo}
                        '2' {Change}
                        '3' {Diagnostic}
                        '4' {Reports}
                        'Q' {Write-Host "Exiting Tool..." -ForegroundColor Yellow}
                    }

                }

            until ($Selection -eq '1' -or
                   $Selection -eq '2' -or
                   $Selection -eq '3' -or
                   $Selection -eq '4' -or
                   $Selection -eq 'Q'
                   )

        }

Additional Menu

Ok finally, let's create an additional menu so that when we select "1" we are taken to another menu. Remember to give this file a .psm1 extension and save in the "Modules" folder. This menu will look like this:



<#

    Type: Module

    Name: Get Info Menu

    Description: This function builds the get info menu for our tool.

    Author: Matt Graham

    Date:

    Version: 1.0.0.0

#>


# Create Main Menu Function

    function GetInfo

        {

            # Build Menu

            param([string]$Title="Get Info Menu")

            do

                {

                    Start-Sleep -Seconds 1

                    Clear-Host


                    Write-Host ""

                    Write-Host "======================$Title============================" -ForegroundColor Green

                    Write-Host " 1. Get Computer System Info"

                    Write-Host " 2. Get Network Settings"

                    Write-Host " 3. Get Disk Information"

                    Write-Host " 4. Get HotFix Information" 

                    Write-Host "======================$Title============================" -ForegroundColor Green

                    Write-Host ""

                    $Selection = Read-Host "Select option and press enter"



            # Switch for Selecting functions (Menus)

                Switch ($Selection)

                    {

                        '1' {Get-ComputerInfo}

                        '2' {Get-NetworkSettings}

                        '3' {Get-DiskInformation}

                        '4' {Get-HotfixInformation}

                        'Q' {MainMenu}

                    }


                }

            until ($Selection -eq 'Q')

        }

Keep in mind that I have populated the "Switch" section with some fake functions "Get-ComputerInfo", etc. You would need to build those functions and enter them in here. This article only shows you how to create the main folder structure, the main menu, and one additional menu. Now you can start building out more menus and creating your own functions so that you can have ready access to them that is menu driven.

1 comment:

  1. How To Play Baccarat - Fastest Baccarat Games
    Learn the rules of Baccarat to learn 바카라사이트 the rules, strategies, and more หาเงินออนไลน์ at our guide. Learn how to kadangpintar play Baccarat or Baccarat online here.

    ReplyDelete