PowerCLI Menu

For some time now I’ve wanted to put together a PowerCLI menu system, just to make running common commands a little faster. Let’s start with vCenter, unfortunately, this is the only part that hardcoded so you’re going to need to amend this to include your vCenters FQDN

###############################File Location ###############################
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$utilsDir = Join-Path -Path $scriptDir -ChildPath \utils
###############################DEFINE FUNCTIONS ###############################
#Define vCenters
function VP-Select-vCenter
{
param (
[string]$SCLUTitle = 'Select vCenter'
)
Clear-Host
Write-Host
Write-Host " ================ $SCLUTitle ================" -ForegroundColor green
Write-Host
Write-Host " 1:
vCenter Server Name"
Write-Host " 2: Specify FQDN"
Write-Host " 3: N/A"
Write-Host
}
clear
Select & Connect to vCenter
function VP-Choice-Select-vCenter
{
VP-Select-vCenter –Title 'Select vCenter'
$selection = Read-Host " Please make a selection"
switch ($selection)
{
'1' {
clear
$VCChoice = "
vCenter Server Name"
write-host
write-host " You chose $VCChoice" -ForegroundColor yellow
Connect-VIServer $VCChoice
clear
VP-Select-Datacenter
} '2' {
Write-host
$selection = Read-Host " Please FQDN of VCSA or ESXi Host"
$VCChoice = $selection
Connect-VIServer $VCChoice
clear
VP-Select-Datacenter
} '3' {
$VCChoice = "N/A"
write-host " You chose N/A, Goodbye!" -ForegroundColor red
Start-Sleep -Second 2
Exit
}
}
}
clear

Now we have chosen a vCenter and connected, we need to select which datacenter & cluster we want to run commands on. Both of these menus offer the choice to “Select all”.

#Select Datacenter
function VP-Select-Datacenter
{
$datacenters = Get-datacenter
Write-Host
Write-Host " ================ Select Datacenter ================" -ForegroundColor green
Write-Host
$i = 1
$datacenters | ForEach-Object -Process {
Write-Host " $i $($_.Name)"
$i++
}
Write-Host
Write-Host " A Select All Datacenters"
Write-Host
$selection = Read-Host " Please make a selection (Q to Quit)"
if($selection -eq 'Q'){
Disconnect-VIServer -Server * -Force
clear
VP-Choice-Select-vCenter
}
if($selection -eq 'A'){
$global:DCChoice = "*"
Write-Host " You chose Datacenter $DCChoice"
Clear
VP-Select-Cluster
}
else{
$global:DCChoice = $datacenters[$selection -1].Name
Write-Host " You chose Datacenter $DCChoice"
Clear
VP-Select-Cluster
}
}
#Select Cluster
function VP-Select-Cluster
{
$clusters = Get-datacenter $DCChoice | Get-Cluster
Write-Host
Write-Host " ================ Select $DCChoice Cluster ================" -ForegroundColor green
Write-Host
$i = 1
$clusters | ForEach-Object -Process {
Write-Host " $i $($_.Name)"
$i++
}
Write-Host
Write-Host " A Select All Clusters"
Write-Host
$selection = Read-Host " Please make a selection (Q to Quit)"
if($selection -eq 'Q'){
clear
VP-Select-Datacenter
}
if($selection -eq 'A'){
$global:CLUChoice = "*"
Write-Host
Write-Host -NoNewLine " CAUTION! All further commands will be run against ALL clusters in DC $Global:DCChoice… Press any key to continue…" -ForegroundColor red;
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
Clear
&
$utilsDir\ClusterCommands.ps1
}
else{
$global:CLUChoice = $clusters[$selection -1].Name
Write-Host " You chose cluster $CLUChoice"
Start-Sleep -Second 1
&
$utilsDir\ClusterCommands.ps1
}
}

Having run these, it leaves you with the variables below:

$global:DCChoice The DC you chose
$global:CLUChoice The Cluster you chose

Using my script above, once you have chosen the cluster it then runs $utilsDir\ClusterCommands.ps1 which is a separate script created to bring up a list of cluster specific commands.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.