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.Start Tool
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
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
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.
How To Play Baccarat - Fastest Baccarat Games
ReplyDeleteLearn the rules of Baccarat to learn 바카라사이트 the rules, strategies, and more หาเงินออนไลน์ at our guide. Learn how to kadangpintar play Baccarat or Baccarat online here.