top of page

PowerShell - Menu Driven

  • Writer: Damian Scoles
    Damian Scoles
  • Dec 17, 2018
  • 2 min read


Depending on the purpose of your script, a menu might be an otherwise extraneous. Yet there are quite a few uses where having a menu is worthwhile. The biggest reason is the concept of repeatability. If the script needs to be executed multiple times, then a menu will help drive that.


When creating a menu, there a few components that need to be built. One is the visual part of the menu, what is displayed so help know what choices are available. Next is the Do { } While set of code that executes which menu option is chosen. Finally, there are the functions that are called from the previous step. Combined, a menu driven script can be a powerful yet simple way to perform a repeatable task.


Sample ideas for a menu drive script:


(1) Installing software or configuring prerequisites for a particular software or update.

(2) Script that performs tasks - mailbox searches, content removal, etc.

(3) Discovery tasks - Active Directory, Exchange, Office 365, etc.


Sample Menu (practical example - real script):


$Menu = {

Write-Host " *****************************************************" -ForegroundColor Cyan

Write-Host " Exchange Server 2019 (Core OS) Prerequisites Script" -ForegroundColor Cyan

Write-Host " *****************************************************" -ForegroundColor Cyan

Write-Host " "

Write-Host " Install NEW Server" -ForegroundColor Cyan

Write-Host " ------------------" -ForegroundColor Cyan

Write-Host " 1) Install Mailbox Role Prerequisites" -ForegroundColor White

Write-Host " 2) Install Edge Transport Prerequisites" -ForegroundColor White

Write-Host " "

Write-Host " Exit Script or Reboot" -ForegroundColor Cyan

Write-Host " -------------------------" -ForegroundColor Cyan

Write-Host " 98) Restart the Server" -ForegroundColor Red

Write-Host " 99) Exit" -ForegroundColor Cyan

Write-Host " "

Write-Host " Select an option.. [1-99]? " -ForegroundColor White -nonewline

}


A menu requires a second piece to function.


This second part is the Do {} While code. This code waits for the end user to enter a # that is valid in the meu. The end user enters a valid number, which is stored in the $Choice variable. This is then processed by the 'Switch' function that looks for a matching entry. If a matching entry is found, code in the block, {}, is executed.


Do {

Invoke-Command -ScriptBlock $Menu

$Choice = Read-Host

Switch ($Choice) {

1 {

If ($Core) {

Install-WindowsFeature RSAT-ADDS

Install-WindowsFeature Web-Client-Auth,Web-Dir-Browsing,Web-Http-Errors,Web-Http-Logging,Web-Http-Redirect,Web-Metabase,Web-WMI,Web-Basic-Auth,Web-Digest-Auth,Web-Dyn-Compression,Web-Stat-Compression,Web-Windows-Auth,Web-ISAPI-Filter,NET-WCF-HTTP-Activation45,Web-Request-Monitor,RPC-over-HTTP-proxy,RSAT-Clustering,RSAT-Clustering-CmdInterface,RSAT-Clustering-PowerShell,Web-Static-Content,Web-Http-Tracing,Web-Asp-Net45,Web-ISAPI-Ext,Web-Mgmt-Service,Web-Net-Ext45,WAS-Process-Model,Web-Server,Server-Media-Foundation,RSAT-ADDS,NET-Framework-45-Features

UCMACore

CPlusPlus

}

}

2 {

Install-windowsfeature ADLDS

CPlusPlus2012

}

}

} While ($Choice -ne 99)


For the third and final part:


Functions that are called from the menu choice are now executed. From the above menu, a sample functions called 'CPlusPlus' is called. A Sample working function is provided below:


Function CPlusPlus {

CLS

$Val = Get-ItemProperty -Path "HKLM:\SOFTWARE\Classes\Installer\Products\6E8D947A316B3EB3F8F540C548BE2AB9" -ErrorAction Silentlycontinue

If($Val.ProductName -eq $Null){

Set-Location $DownloadFolder

[string]$expression = ".\vcredist_x64.exe /quiet /norestart /l* $targetfolder\cPlusPlus.log"

Write-Host "File: vcredist_x64.exe installing..." -NoNewLine

Invoke-Expression $expression

Start-Sleep -Seconds 20

$val = Get-ItemProperty -Path "HKLM:\SOFTWARE\Classes\Installer\Products\6E8D947A316B3EB3F8F540C548BE2AB9" -ErrorAction Silentlycontinue

if($val.DisplayVersion -ne $Null){

Write-Host "`nMicrosoft Visual C++ 2013 x64 Minimum Runtime - 12.0.21005 is now installed." -ForegroundColor Green

}

} Else {

Write-Host "`nMicrosoft Visual C++ 2013 x64 Minimum Runtime - 12.0.21005 is already" -ForegroundColor white -NoNewline

Write-host " installed." -ForegroundColor Green

Start-Sleep 2

}

Write-Host " "

} # End CPlusPlus


** Menus and more are covered in our books **

Appendix B (Exchange Online)

Chapter 18 (Exchange 2016).

 
 
 

Recent Posts

See All
Null. Empty. Space? How to Detect?

** Disclaimer ** I do not know everything about PowerShell and I am sure I will miss some methods of detection of empty values. However,...

 
 
 

Comments


© 2016-2020 by Damian Scoles and Dave Stork.

bottom of page